hibernate.hbm2ddl.auto = update不起作用

时间:2013-08-20 15:24:49

标签: java hibernate hbm2ddl

我有模特儿。这部分是:enter image description here

模型由jpa annotations映射。我使用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;
    }

}

vacancy_status

@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;
    }

}

这些实体都是映射代码。

在调试器中: enter image description here

id == 500 ==&gt; hibernate明白,它是同一个对象。

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

我解决了这个问题出现的原因。如果我在记事表中添加记录,则会出现: enter image description here

1 个答案:

答案 0 :(得分:0)

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

参见JPA规范:

  

2.4主要密钥和实体身份

     

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

另见SO post