2013-11-03 33 views
0

我試圖檢索並打印所有參與者的事件(對於給定的eventId),但參與者集在運行代碼後爲空。這是我的Class和HBM文件。如何使用Hibernate檢索雙向關聯數據

類人:

public class Person { 
private long personId; 
private String firstName; 
private String lastName; 
private int age; 

private Set<Event> myEvents=new HashSet<Event>(); 

public Person() { 
} 

/** 
* @return the personId 
*/ 
public long getPersonId() { 
    return personId; 
} 

/** 
* @param personId the personId to set 
*/ 
public void setPersonId(long personId) { 
    this.personId = personId; 
} 

/** 
* @return the firstName 
*/ 
public String getFirstName() { 
    return firstName; 
} 

/** 
* @param firstName the firstName to set 
*/ 
public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

/** 
* @return the lastName 
*/ 
public String getLastName() { 
    return lastName; 
} 

/** 
* @param lastName the lastName to set 
*/ 
public void setLastName(String lastName) { 
    this.lastName = lastName; 
} 

/** 
* @return the age 
*/ 
public int getAge() { 
    return age; 
} 

/** 
* @param age the age to set 
*/ 
public void setAge(int age) { 
    this.age = age; 
} 

/** 
* @return the myEvents 
*/ 
public Set getMyEvents() { 
    return myEvents; 
} 

/** 
* @param myEvents the myEvents to set 
*/ 
public void setMyEvents(Set myEvents) { 
    this.myEvents = myEvents; 
} 


} 

HBM

<hibernate-mapping package="com.lc.learn.hibernate.sample.beans"> 

<class name="Person" table="person"> 
    <id name="personId" column="person_id" type="long"> 
     <generator class="increment"/> 
    </id> 
    <property name="firstName" column="first_name" type="string"/> 
    <property name="lastName" column="last_name" type="string"/> 
    <property name="age" column="age" type="integer"/> 

    <set name="myEvents" table="person_events" inverse="false" lazy="false" fetch="select" cascade="all"> 
     <key column="personId"/> 
     <many-to-many column="eventId" class="Event"/> 
    </set> 
</class> 

</hibernate-mapping> 

事件類

public class Event { 
private long eventId; 
private String eventTitle; 
private Date eventDate; 

private Set<Person> personList=new HashSet<Person>(); 

public Event() { 
} 

public Event(String eventTitle, Date eventDate) {   
    this.eventTitle = eventTitle; 
    this.eventDate = eventDate; 
} 


/** 
* @return the eventId 
*/ 
public long getEventId() { 
    return eventId; 
} 

/** 
* @param eventId the eventId to set 
*/ 
public void setEventId(long eventId) { 
    this.eventId = eventId; 
} 

/** 
* @return the eventTitle 
*/ 
public String getEventTitle() { 
    return eventTitle; 
} 

/** 
* @param eventTitle the eventTitle to set 
*/ 
public void setEventTitle(String eventTitle) { 
    this.eventTitle = eventTitle; 
} 

/** 
* @return the eventDate 
*/ 
public Date getEventDate() { 
    return eventDate; 
} 

/** 
* @param eventDate the eventDate to set 
*/ 
public void setEventDate(Date eventDate) { 
    this.eventDate = eventDate; 
} 

/** 
* @return the personList 
*/ 
public Set getPersonList() { 
    return personList; 
} 

/** 
* @param personList the personList to set 
*/ 
public void setPersonList(Set personList) { 
    this.personList = personList; 
} 


} 

HBM:

<hibernate-mapping package="com.lc.learn.hibernate.sample.beans"> 

<class name="Event" table="event"> 
    <id name="eventId" column="event_id" type="long"> 
     <generator class="increment"/> 
    </id> 
    <property name="eventTitle" column="event_title" type="string"/> 
    <property name="eventDate" column="event_date" type="timestamp"/> 

    <set name="personList" table="person_events" inverse="true" lazy="false" fetch="select"> 
     <key column="event_id"/> 
     <many-to-many column="person_id" class="Person"/> 
    </set> 
</class> 

</hibernate-mapping> 

DAL:

public class EventManager { 

public Event getEventById(long eventId){ 
    Event eObj=null; 
    Session session=HibernateUtil.getSessionFactory().getCurrentSession(); 
    Transaction tx=session.getTransaction(); 
    tx.begin(); 
     eObj=(Event)session.get(Event.class, eventId); 
     eObj.setPersonList(eObj.getPersonList()); 
     //Hibernate.initialize(eObj.getPersonList()); 
    tx.commit(); 
    return eObj; 
} 
} 

主類:

public class MyHibernateSample { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    EventManager eManager=new EventManager();  
    Event eObj= eManager.getEventById(1); 

    Iterator it=eObj.getPersonList().iterator(); 
    while(it.hasNext()){ 
     Person pObj=(Person)it.next(); 
     System.out.println("First Name : "+pObj.getFirstName()+" Last Name : "+pObj.getLastName()+" Age : "+pObj.getAge()); 
    } 
} 
} 

任何一個請幫我解決這個問題。 感謝 李

+0

你能向我們展示表格的DDL嗎?執行期間是否記錄了任何錯誤? –

+0

沒有錯誤。但數據沒有取得.. – Lee

回答

0

我注意到,在您的映射Person,你駝峯名指定的連接表中的列:

<key column="personId"/> 
    <many-to-many column="eventId" class="Event"/> 

但在你的映射Event,他們已經underscore_separated名稱:

<key column="event_id"/> 
    <many-to-many column="person_id" class="Person"/> 

這些應該是相同的,它們應該與數據庫中的列名相同。我希望這個映射失敗,但是會大聲失敗,數據庫異常報告沒有找到某個列或其他列。

+0

我改變了現在正在工作的event.hbm文件中的字段名稱。謝謝你的幫助。 – Lee

+0

很高興聽到它。奇怪的是,這失敗了,而不是拋出異常。一個例外會告訴你到底是什麼問題馬上就來了! –