简体   繁体   中英

Use the external key as part of primary key with Hibernate annotation

I have a one-to-many relationship between Car and DatatableProgress , so one car may have more DatatableProgress but one DatatableProgress element is of one car . Car has idCar as primary key, datatableProgress has datatableName and sheet as primary key, but I have to add idCar (the foreign key) as part of primary key. I tryed with this code but I received mappedBy reference an unknown target entity property: com.domain.DatatableProgress.car in com.domain.Car.datatablesProgress . I need this because I can have a datatableProgress with the same name and sheet but different car. :

DatatableProgress :

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

DatatableProgressKey

@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 

Car

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

UPDATE: Is it possible to make this change without modify Car entity (so without CarKey class)?

To be a part of composite key entity should be @Embedable. You can create a separate key entity for a Car class just with one Integer field and then use this key class for your composite key. See there Hibernate docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM