我想了解一個現有的應用程序使用休眠,我有一些知識。在這個應用程序中,已經創建了一個數據庫模式,並且類已經從數據庫模式中自動生成。瞭解/設置休眠配置
讓我們在DB 2代表的例子:
PERSON(id, firstname, surname)
OBJECT(id, owner_id, description)
在表OBJECT
,owner_id
將是一個外鍵PERSON.id
。
生成的類看起來大致這樣:
class Person {
// ok for the following
int id; // with getters and setters
String firstname; // with getters and setters
String surname; // with getters and setters
// dunno why the following ?
Set objects = new HashSet(0);
Set getObjects() { return this.objects; }
void setObjects(Set objects) this.objects = objects; }
}
class Object {
int id; // with getters and setters
Person owner; // with getters and setters
String description; // with getters and setters
}
首先,我不知道爲什麼會出現在集生成的Person
類擁有的對象的引用。第二,如果通過HQL查詢得到Object
或List<Object>
,則此Object
的Person
成員是否完全加載?我應該如何設置hibernate以確保它是?
我在問,因爲在我的應用程序中,我將hibernate會話作爲單獨的可執行文件運行,另一個與hibernate應用程序通信的遞歸應用程序,並要求它發送一些持久對象。我可以得到一個Object
,但如果這個對象上我做object.getPerson().getFirstname()
,我碰上一個org.hibernate.LazyInitializationException: could not initialize proxy - no Session
'Set'用於雙向參考。一個'Object'通過所有者引用知道它的所有者,一個'Person'通過'Set'知道它的所有'Object's。 – 2013-03-13 13:42:02