查找实体时休眠意外删除

我们有一个在 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;


@Embedded

CampaignConditionsEntity campaignConditions;


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

@OrderBy

private List<CodeEntity> campaignCodes;


public CampaignEntity() {

}

以及以下 CampaignConditionsEntity:


@Embeddable

public 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;


繁星淼淼
浏览 186回答 2
2回答

慕后森

我有一些建议:)请注意什么是Persistence Context(EntityManagerJPA 术语中的实例/&nbsp;SessionHibernate 中的实例)、实体生命周期和事务范围(工作单元)如果您不希望更改反映在数据库中,请不要更改实体状态,或者至少在更改实体之前将其分离。readOnly如果您只获取相关工作单元中的数据,请将您的交易标记为“&nbsp;”。(请注意,如果您有许多“事务”方法加入同一个物理事务,则该标志由周围的一个设置,并且不能被内部逻辑事务覆盖)。这样EntityManager就不会在事务结束时刷新,挂起的更改也不会持久化到数据库中。您可以使用EntityListener相关实体上的an 跟踪触发意外删除的方法并在方法中打印当前 strackTrace (&nbsp;new Throwable().printStackTrace()/&nbsp;log(new Throwable())&nbsp;PreRemove
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java