开发者

Persist multiple objects at one go

My current project uses JPA and HSQLDB.

I would like to persist multiple related objcts at one go, is that by any means possible in JPA?

Ex: Suppose there are two entities like Person and ContactInfo, where Person has List<ContactInfo> entities.

If I want to persist Person entity along with ContactInfos also, what I am doing is set the list in Person and call persist. Will doing that take care of persisting List<ContactInfo> also? (With foreign key reference to Person ID in database table)

Else kindly let mek n开发者_开发知识库ow how would I achieve this in JPA.

Regards,

Satya


It will, if you set @*ToMany(cascade=CascadeType.PERSIST)


You could do as Bozho suggested, but if you would also like them to be updated, deleted, etc. when it's done with Person, I would suggest to cascade like that:

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)

Note: orphanRemoval will only work with JPA 2.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜