我们有一个在 JBoss 6.4 GA 上运行的 Java ee 应用程序,使用 JPA 和 Hibernate 和以下实体:@Entity@SequenceGenerator(name = "sequence", sequenceName="SEQ_CAMPAIGNS_ID",allocationSize = 1)@Table(name = "CAMPAIGN")public class CampaignEntity implements Serializable {@Id@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence")@Column(name = "ID")private Long id;@Column(name = "NAME")private String name;@Column(name = "IS_ACTIVE", nullable = false)private boolean active;@Column(name = "START_DATE", nullable = false)private Date startDate;@Column(name = "END_DATE", nullable = false)private Date endDate;@Column(name = "LEGAL_ENTITY_ID", nullable = false)private Integer legalEntityId;@Column(name = "DEPARTMENT", nullable = false)@Enumerated(value = EnumType.STRING)private Department department;@Column(name = "CATEGORY", nullable = false)@Enumerated(value = EnumType.STRING)private Category category;@EmbeddedCampaignConditionsEntity campaignConditions;@OneToMany(cascade = CascadeType.ALL, mappedBy = "campaign", orphanRemoval = true)@OrderByprivate List<CodeEntity> campaignCodes;public CampaignEntity() {}以及以下 CampaignConditionsEntity:@Embeddablepublic class CampaignConditionsEntity implements Serializable {private static final String CAMPAIGN_ID = "CAMPAIGN_ID";@ElementCollection(fetch = FetchType.EAGER)@CollectionTable(name = "CAMPAIGN_COND_TRIP_TYPE", joinColumns = @JoinColumn(name = CAMPAIGN_ID))private Set<TripTypeConditionEntity> tripTypeConditions;
2 回答
慕后森
TA贡献1802条经验 获得超5个赞
我有一些建议:)
请注意什么是
Persistence Context
(EntityManager
JPA 术语中的实例/Session
Hibernate 中的实例)、实体生命周期和事务范围(工作单元)如果您不希望更改反映在数据库中,请不要更改实体状态,或者至少在更改实体之前将其分离。
readOnly
如果您只获取相关工作单元中的数据,请将您的交易标记为“ ”。(请注意,如果您有许多“事务”方法加入同一个物理事务,则该标志由周围的一个设置,并且不能被内部逻辑事务覆盖)。这样EntityManager
就不会在事务结束时刷新,挂起的更改也不会持久化到数据库中。您可以使用
EntityListener
相关实体上的an 跟踪触发意外删除的方法并在方法中打印当前 strackTrace (new Throwable().printStackTrace()
/log(new Throwable()
)PreRemove
添加回答
举报
0/150
提交
取消