简体   繁体   English

保留另一个实体的JPA实体

[英]JPA Entity Persisting another entity

I have a "sale" entity, before the entity is persisted, I'd like to create an "Income" entity. 我有一个“待售”实体,在该实体保留之前,我想创建一个“收入”实体。 I could do this in a session bean but I'd like it better if it was encapsulated inside the Sale entity. 我可以在会话bean中执行此操作,但是如果将其封装在Sale实体中,我会更好。 So I could do: 所以我可以做:

em.persist(sale);

And it'd automatically create an Income entity and persist it as well. 并且它会自动创建一个Income实体并将其持久化。

I don't want Sale to have an association with Income because I could associate them and do a cascade persist. 我不希望Sale与Income有关联,因为我可以将它们关联起来并进行级联。

It's like if the sale entity would create an instance of the Income entity and then call the persist method on the EntityManager, since this is neither posible nor a good idea I am beginning to think that it should be performed by the EJB. 就像销售实体会创建一个Income实体的实例,然后在EntityManager上调用persist方法一样,因为这既不可能也不是一个好主意,我开始认为它应该由EJB执行。 Opinions? 意见?

Have you tried the @PrePersist annotation? 您是否尝试过@PrePersist批注?

JPA prepersist JPA预习者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM