0

我有模型。有这部分:在此处输入图像描述

模型由 jpa 注释映射。我使用 fetchType = EAGER 的任何地方。如果我从数据库加载空缺,我有 2 个重复的 status_for_vacancy 对象。我使用属性 hbm2ddl.auto = update。如果我创建数据库的新模式并填充数据,我没有重复 status_for_vacancy 对象。它真的?

代码: 空缺

@Entity
@Table(name = "vacancy")
@XmlRootElement(name="vacancy")

public class Vacancy {

    private List<VacancyStatus> statusList = new LinkedList<VacancyStatus>();

    @OneToMany(mappedBy = "vacancy", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    public List<VacancyStatus> getStatusList() {
        return statusList;
    }

    public void setStatusList(List<VacancyStatus> statusList) {
        this.statusList = statusList;
    }

}

status_for_vacancy

@Entity
@Table(name = "status_for_vacancy")
public class StatusForVacancy extends AbstractStatus {

    public StatusForVacancy() {
        super();
    }

    public StatusForVacancy(Integer id, String name) {
        super(id, name);
    }

}


@MappedSuperclass
@XmlRootElement
public abstract class AbstractStatus {

    private Integer id;
    private String name;

    public AbstractStatus() {
        super();
    }

    public AbstractStatus(String name) {
        super();
        this.name = name;
    }

    public AbstractStatus(Integer id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column (name ="id")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "name")
    @NotEmpty
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

空缺状态

@Entity
@Table(name = "vacancy_status")
public class VacancyStatus extends AbstractHistoryStatus {

    private Vacancy vacancy;
    private StatusForVacancy status;

    public VacancyStatus() {
        super();
    }

    public VacancyStatus(Integer id, User author, Date date, 
            Vacancy vacancy, StatusForVacancy status) {
        super(id, author, date);
        this.vacancy = vacancy;
        this.status = status;
    }


    @ManyToOne
    @JoinColumn(name = "vacancy_id")
    public Vacancy getVacancy() {
        return vacancy;
    }

    public void setVacancy(Vacancy vacancy) {
        this.vacancy = vacancy;
    }

    @ManyToOne
    @JoinColumn(name = "status_id")
    public StatusForVacancy getStatus() {
        return status;
    }

    public void setStatus(StatusForVacancy status) {
        this.status = status;
    }
}


@MappedSuperclass
public abstract class AbstractHistoryStatus {

    private Integer id;
    private User author;
    private Date date;


    public AbstractHistoryStatus() {
    }

    public AbstractHistoryStatus(Integer id, User author, Date date) {
        super();
        this.id = id;
        this.author = author;
        this.date = date;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @ManyToOne
    public User getAuthor() {
        return author;
    }

    public void setAuthor(User author) {
        this.author = author;
    }

    @Column(name="creation_date")
    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

}

都是这些实体的映射代码。

在调试器中: 在此处输入图像描述

id==500 ==> hibernate 都明白,它是相同的对象。

我尝试将旧数据库中的所有数据添加到新数据库 - 我得到旧错误(

我修复了出现此问题的原因。如果我将记录添加到注释表,它就会出现: 在此处输入图像描述

4

1 回答 1

0

我强烈建议您编写 equals() 和 hashCode() 方法。标准的 equals()/hashCode() 实现了引用相等(两个对象是否引用相同的内存位置)。因此,如果休眠在内存中有 2 个“相同”对象,但它们没有引用相同的内存位置,那么您将看到该对象出现两次。但是如果你基于主键相等来实现equals(),那么即使内存中有同一个对象的两个副本,Hibernate 也不会给你重复。

请参阅 JPA 规范:

2.4 主键和实体身份

每个实体都必须有一个主键。...其主键的值唯一地标识持久性上下文中的实体实例和 EntityManager 操作

另请参阅此 SO帖子

于 2013-08-21T08:01:38.030 回答