gpt4 book ai didi

java - Hibernate 查找实体时意外删除

转载 作者:行者123 更新时间:2023-11-30 02:02:43 26 4
gpt4 key购买 nike

我们有一个 Java ee 应用程序在 JBoss 6.4 GA 上运行,使用 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;

以及以下 CodeEntity:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@SequenceGenerator(name = "sequence", sequenceName = "SEQ_CODES_ID", allocationSize = 1)
public abstract class CodeEntity implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence")
@Column(name = "ID", nullable = false)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CAMPAIGN_ID")
private CampaignEntity campaign;

@OneToOne(mappedBy = "code", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false, orphanRemoval = true)
private DiscountEntity discount;

@Column(name = "MAX_USAGES", nullable = false)
private Integer maxUsages;

@Column(name = "UNLIMITED_USAGES", nullable = false)
private boolean unlimitedUsages;

@Column(name = "NEGATIVE_SH", nullable = false)
private boolean negativeSH;

@Column(name = "UNIQUE_BUYER", nullable = false)
private boolean uniqueBuyer;

@Column(name = "START_DATE")
private Date startDate;

@Column(name = "END_DATE")
private Date endDate;

@Embedded
private CodeConditionsEntity codeConditions;

public CodeEntity() {
}

这是 CodeConditionsEntity:

@Embeddable
public class CodeConditionsEntity implements Serializable {

private static final String CODE_ID = "CODE_ID";

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "CODE_COND_TRIP_TYPE", joinColumns = @JoinColumn(name = CODE_ID))
private Set<TripTypeConditionEntity> tripTypeConditions;

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "CODE_COND_CARRIERS", joinColumns = @JoinColumn(name = CODE_ID))
private Set<CarrierConditionEntity> carrierConditions;

这是 CarrierConditionEntity:

@Embeddable
public class CarrierConditionEntity implements Serializable {

@Column(name = "CARRIER", nullable = false, length = 3)
private String carrierCode;

@Column(name = "IS_INCLUDED", nullable = false)
private boolean included;

问题在于,当我们所做的唯一操作是查找特定的 Activity 实体时,我们在日志中发现了意外删除。

在生产日志中,我们发现以下删除

Hibernate: delete from CODE_COND_CARRIERS where CODE_ID=? and CARRIER=? and IS_INCLUDED=? 

你有什么建议吗?

谢谢

最佳答案

我有一些建议:)

  • 了解什么是 Persistence Context (JPA 术语中的 EntityManager 实例/Hibernate 中的 Session 实例),entity lifecycletransaction scope (工作单元)

  • 如果您不希望更改反射(reflect)在数据库中,请不要改变实体状态,或者至少在改变实体之前分离实体。

  • 如果您仅提取相关工作单元中的数据,则将您的事务标记为“readOnly”。 (请注意,如果您有许多“事务”方法加入同一物理事务,则该标志由周围的方法设置,并且不能被内部逻辑事务覆盖)。这样,EntityManager 就不会在事务结束时刷新,并且挂起的更改也不会保留到数据库中。

  • 您可以使用 EntityListener 来跟踪触发意外删除的方法。在相关实体上并在 PreRemove 中打印当前的 strackTrace (new Throwable().printStackTrace()/log(new Throwable())方法

关于java - Hibernate 查找实体时意外删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52309504/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com