這裏是我的代碼片段:如何獲得休眠層次懶惰對象
public class Object1 implements Serializable {
@Id
@Column(length = 36)
protected String id;
@Column(length = 36, insertable = false, updatable = false)
protected String parentID;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "object2ID", referencedColumnName = "parentID")
protected List<Object2> parents = new ArrayList<>();
public List<Object2> getParents() {
return parents;
}
}
public class Object2 implements Serializable {
@Id
@Column(length = 36)
protected String id;
@Column(length = 36, insertable = false, updatable = false)
protected String object2ID;
@Column(length = 36, insertable = false, updatable = false)
protected String parentID;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "object2ID", referencedColumnName = "parentID")
protected List<Object2> parents = new ArrayList<>();
public List<Object2> getParents() {
return parents;
}
}
與應用類:
public class Application {
public static Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
Path HIBERNATE_CONFIGURATION = Paths.get("");
Configuration configuration = new Configuration().configure(HIBERNATE_CONFIGURATION.toFile());
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
Object1 object1 = (Object1) session.get(Object1.class, "1");
logger.info(object1.toString());
Object2 object2 = object1.getParents().get(0);
logger.info(object2.toString());
while (!object2.getParents().isEmpty()) {
object2 = object2.getParents().get(0);
logger.info(object2.toString());
}
session.close();
}
}
我越來越Object1符合市場預期,但對象2拋出異常org.hibernate.LazyInitializationException: could not initialize proxy - no Session
會話未關閉,爲什麼我收到此錯誤?
我使用Hibernate的核心:4.3.7.Final
解決:
嗨。謝謝大家。我發現解決我的問題。我試圖得到OneToMany,但在數據庫引用中的實數是ManyToMany類型。我爲db和model創建了一個小的改變。
我重命名對象。 這裏是新的代碼片段:
@Entity
@Table(name = "Houses")
public class House implements Serializable {
@Id
@Column(length = 36)
protected String id;
@Column(length = 36)
protected String parentGUID;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parentguid", referencedColumnName = "guid")
protected AddressObject address;
public AddressObject getAddress() {
return address;
}
@Override
public String toString() {
return "Object1{" +
"id='" + id + '\'' +
", parentGUID='" + parentGUID + '\'' +
'}';
}
}
@Entity
@Table(name = "AddressObjects")
public class AddressObject implements Serializable {
@Id
@Column(length = 36)
protected String id;
@Column(length = 36, unique = true)
protected String guid;
@Column(length = 36, nullable = true)
protected String parentGUID;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "address")
protected List<House> houses = new ArrayList<>();
@OneToMany(fetch = FetchType.LAZY, mappedBy = "upHierarchicObject")
protected List<AddressObject> downHierarchicObject = new ArrayList<>();
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parentguid", referencedColumnName = "guid")
protected AddressObject upHierarchicObject;
public List<House> getHouses() {
return houses;
}
public List<AddressObject> getDownHierarchicObject() {
return downHierarchicObject;
}
public AddressObject getUpHierarchicObject() {
return upHierarchicObject;
}
@Override
public String toString() {
return "Object2{" +
"id='" + id + '\'' +
", guid='" + guid + '\'' +
", parentGUID='" + parentGUID + '\'' +
'}';
}
}
與應用類:
public class Application {
public static Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
ArrayList<Object> objects = new ArrayList<>();
Path HIBERNATE_CONFIGURATION = Paths.get("config/hibernate.test.cfg.xml");
Configuration configuration = new Configuration().configure(HIBERNATE_CONFIGURATION.toFile());
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
House house = (House) session.get(House.class, "1");
objects.add(house);
AddressObject addressObject = house.getAddress();
objects.add(addressObject);
while (addressObject.getUpHierarchicObject() != null) {
addressObject = addressObject.getUpHierarchicObject();
objects.add(addressObject);
}
for (Object obj : objects) {
logger.info("Object: {}", obj);
}
session.close();
}
}
但是,我不爲什麼我除了LazyInitializationException中。這是休眠的錯誤?
你必須創建一個事務,以及... – 2014-11-24 10:25:25
如果我加入交易,即不工作 – ArtOfShine 2014-11-24 13:14:18
我沒有看到你的代碼進行任何交易。你也會遇到一個無限循環,因爲'object2.getParents()。get(0)'不會從集合中刪除元素,因此無限運行。 – 2014-11-24 13:42:46