JPA / Hibernate @Embeddable和@EmbeddedId - 不必要的列

时间:2017-05-24 08:48:53

标签: hibernate jpa

伙计们! 我正在使用JPA / Hibernate创建具有复合PK的JoinTable。除了数据库中不必要的列外,一切正常。

package com.exhibitors.model;

import java.io.Serializable;

import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;

import org.hibernate.annotations.Type;
import org.joda.time.DateTime;

@Entity
@Table(name = "distributor_samples")
@AssociationOverrides({
    @AssociationOverride(name="samplesPK.distributor", joinColumns=@JoinColumn(name="distributor_id")),
    @AssociationOverride(name="samplesPK.tile", joinColumns=@JoinColumn(name="tile_id"))
})
public class Samples implements Serializable{

    @EmbeddedId
    SamplesPK samplesPK;

    @Column(name="created_date", nullable = false, updatable = false)
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    private DateTime createdDate = new DateTime();



    public DateTime getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(DateTime createdDate) {
        this.createdDate = createdDate;
    }

    public SamplesPK getSamplesPK() {
        return samplesPK;
    }

    public void setSamplesPK(SamplesPK samplesPK) {
        this.samplesPK = samplesPK;
    }

}

和Embbedeable类。

package com.exhibitors.model;

import java.io.Serializable;

import javax.persistence.Embeddable;
import javax.persistence.ManyToOne;


@Embeddable
public class SamplesPK implements Serializable{

    @ManyToOne
    private Distributor distributor;

    @ManyToOne
    private Tile tile;

    public Distributor getDistributor() {
        return distributor;
    }

    public void setDistributor(Distributor distributor) {
        this.distributor = distributor;
    }

    public Tile getTile() {
        return tile;
    }

    public void setTile(Tile tile) {
        this.tile = tile;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((distributor == null) ? 0 : distributor.hashCode());
        result = prime * result + ((tile == null) ? 0 : tile.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        SamplesPK other = (SamplesPK) obj;
        if (distributor == null) {
            if (other.distributor != null)
                return false;
        } else if (!distributor.equals(other.distributor))
            return false;
        if (tile == null) {
            if (other.tile != null)
                return false;
        } else if (!tile.equals(other.tile))
            return false;
        return true;
    }

}

我的我的表distributor_samples包含一个额外的列,即 samples_id 每当我删除列时,重新启动后,休眠再次创建它。 此列的来源以及如何删除? enter image description here

0 个答案:

没有答案