使用外部键作为主键的一部分与Hibernate注释

时间:2017-11-30 15:49:48

标签: java mysql hibernate jpa

我在one-to-manyCar之间存在DatatableProgress关系,因此一个car可能包含更多DatatableProgressDatatableProgress个元素属于car一个CaridCardatatableProgress为主键,datatableNamesheetidCar为主键,但我必须添加mappedBy reference an unknown target entity property: com.domain.DatatableProgress.car in com.domain.Car.datatablesProgress(外键) )作为主键的一部分。 我尝试使用此代码,但收到了@Entity @Table(name = "datatableprogress") public class DatatableProgress implements Serializable { /** * */ private static final long serialVersionUID = 1L; private DatatableProgressKeys datatableProgressKeys; private Integer actualStoredRow; @MapsId("car") @JoinColumns({ @JoinColumn(name="idCar_fk", referencedColumnName="idCar"), }) @ManyToOne(fetch = FetchType.LAZY) private Car car; public DatatableProgress() { } public DatatableProgress(DatatableProgressKeys datatableProgressKeys, Integer actualStoredRow) { this.datatableProgressKeys = datatableProgressKeys; this.actualStoredRow = actualStoredRow; } /** * @return the datatableKeys */ @EmbeddedId public DatatableProgressKeys getDatatableProgressKeys() { return datatableProgressKeys; } /** * @param datatableKeys the datatableKeys to set */ public void setDatatableProgressKeys(DatatableProgressKeys datatableProgressKeys) { this.datatableProgressKeys = datatableProgressKeys; } @Column(name = "actualStoredRow", nullable = false) public Integer getActualStoredRow() { return this.actualStoredRow; } public void setActualStoredRow(Integer actualStoredRow) { this.actualStoredRow = actualStoredRow; } } 。我需要这个,因为我可以拥有一个具有相同名称和表格但不同汽车的datatableProgress。 :

DatatableProgress:

@Embeddable
public class DatatableProgressKeys implements Serializable {
    private static final long serialVersionUID = 1L;
    private String datatableName;
    private Integer sheet;
    private Car car;


    public DatatableProgressKeys() {}

    public DatatableProgressKeys(String datatableName, Integer sheet, Car car) {
        this.datatableName = datatableName;
        this.sheet = sheet;
        this.setCar(car);
    }


    @Column(name = "datatableName", nullable = false)
    public String getDatatableName() {
        return this.datatableName;
    }

    public void setDatatableName(String datatableName) {
        this.datatableName = datatableName;
    }


    @Column(name = "sheet", nullable = false)
    public Integer getSheet() {
        return this.sheet;
    }

    public void setSheet(Integer sheet) {
        this.sheet = sheet;
    }

    /**
     * @return the car
     */
    @Column(name = "id_car", nullable = false)
    public Car getCar() {
        return car;
    }

    /**
     * @param car the car to set
     */
    public void setCar(Car car) {
        this.car = car;
    }

//hashCode and equals 

DatatableProgressKey

@Entity
@Table(name = "car")
public class Car implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Integer idCar;
    private Integer initialKm;

    @JsonIgnore
    private Set<DatatableProgress> datatablesProgress = new HashSet<DatatableProgress>(0);

    public Car() {
    }

    public Car(Integer initialKm) {
        this.initialKm = initialKm;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id_car", unique = true, nullable = false)
    public Integer getIdCar() {
        return this.idCar;
    }

    public void setIdCar(Integer idCar) {
        this.idCar = idCar;
    }

    @Column(name = "initialKm", nullable = false)
    public Integer getInitialKm() {
        return this.initialKm;
    }

    public void setInitialKm(Integer initialKm) {
        this.initialKm = initialKm;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "car")
    public Set<DatatableProgress> getDatatablesProgress() {
        return this.datatablesProgress;
    }

    public void setDatatablesProgress(Set<DatatableProgress> datatablesProgress) {
        this.datatablesProgress = datatablesProgress;
    }
}

汽车

$str = '2017-12-02';
$time = strtotime($str);
echo date('d-m-Y', $time);

UPDATE:是否可以在不修改Car实体的情况下进行此更改(所以没有CarKey类)?

1 个答案:

答案 0 :(得分:0)

要成为复合键实体的一部分,应该是@Embedable。 您可以使用一个Integer字段为Car类创建单独的键实体,然后将此键类用于组合键。 见Hibernate docs