开发者

EntityManager.persist problem on load related entity

i have a problem with EntityManager.persist (i use tooplink of glassfish).

My code is:

MyEntity e = new MyEntity();
MyEntityPK ePK = new MyEntityPK();
ePK.setField1("aaa");
ePK.setField2("bbb");

e.setMyEntityPK(ePK);
e.setFiled3(ccc);

entityManager.persist(e);

//HERE NOT PROBLEM
//If a i开发者_如何转开发 want a related entity, i get null

AnotherEntity anotherEntity = e.getAnotherEntity();
//a is null

Why "anotherEntity" is null? After a persist, the object begin attached to PersistenceContext. Why the related entity are not loaded if i call getAnotherEntity()

Tanks!

Luigi


persist() persists (inserts) the object. It persists what you pass it, the object will remain the same before and after the persist, except for a possible assign generated id.

You seem to want to find an existing entity not persist a new one. For this you should use find() not persist.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜