在三元关系中休眠多对多关系

时间:2018-04-06 13:18:34

标签: java mysql spring hibernate jpa

两个表UserReservation之间的

to-Many关系,所以我有了这个新表InternalParticipant

@Entity
@Table(name = "user_has_reservation")
@AssociationOverrides({
    @AssociationOverride(name = "pk.user", 
        joinColumns = @JoinColumn(name = "uhr_idUser")),
    @AssociationOverride(name = "pk.reservation", 
        joinColumns = @JoinColumn(name = "uhr_idReservation")) })
public class InternalParticipant implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private InternalParticipantId pk = new InternalParticipantId();

    @JsonIgnore
    private Set<UserAccessReservation> userAccessReservations = new HashSet<UserAccessReservation>(0);

    @EmbeddedId
    public InternalParticipantId getPk() {
        return this.pk;
    }

    public void setPk(InternalParticipantId pk) {
        this.pk = pk;
    }

    @Transient
    public User getUser() {
        return getPk().getUser();
    }

    public void setUser(User user) {
        getPk().setUser(user);
    }

    @Transient
    public Reservation getReservation() {
        return getPk().getReservation();
    }

    public void setReservation(Reservation reservation) {
        getPk().setReservation(reservation);
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.internalParticipant")
    public Set<UserAccessReservation> getUserAccessReservations() {
        return this.userAccessReservations;
    }

    public void setUserAccessReservations(Set<UserAccessReservation> userAccessReservations) {
        this.userAccessReservations = userAccessReservations;
    }
}

和Id:

@Embeddable
public class InternalParticipantId implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private User user;
    private Reservation reservation;

    @ManyToOne
    public User getUser() {
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @ManyToOne
    public Reservation getReservation() {
        return this.reservation;
    }

    public void setReservation(Reservation reservation) {
        this.reservation = reservation;
    }
}

现在我必须以DayTime的三元关系链接此表,因此我创建了一个包含4个主键的新表:

@Entity
@Table(name = "user_access_reservation")
@AssociationOverrides({
    @AssociationOverride(name = "pk.internalParticipant.pk.user", joinColumns = @JoinColumn(name = "uar_idUser")),
    @AssociationOverride(name = "pk.internalParticipant.pk.reservation", joinColumns = @JoinColumn(name = "uar_idReservation")),
    @AssociationOverride(name = "pk.time", joinColumns = @JoinColumn(name = "uar_idTime")),
    @AssociationOverride(name = "pk.day", joinColumns = @JoinColumn(name = "uar_idDay"))})
public class UserAccessReservation implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private UserAccessReservationId pk = new UserAccessReservationId();

    @EmbeddedId
    public UserAccessReservationId getPk() {
        return this.pk;
    }

    public void setPk(UserAccessReservationId pk) {
        this.pk = pk;
    }

    @Transient
    public InternalParticipant getInternalParticipant() {
        return getPk().getInternalParticipant();
    }

    public void setInternalParticipant(InternalParticipant internalParticipant) {
        getPk().setInternalParticipant(internalParticipant);
    }

    @Transient
    public Time getTime() {
        return getPk().getTime();
    }

    public void setTime(Time time) {
        getPk().setTime(time);
    }

    @Transient
    public Day getDay() {
        return getPk().getDay();
    }

    public void setDay(Day day) {
        getPk().setDay(day);
    }

}

和ID

@Embeddable
public class UserAccessReservationId implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private InternalParticipant internalParticipant;
    private Time time;
    private Day day;

    @ManyToOne
    public InternalParticipant getInternalParticipant() {
        return this.internalParticipant;
    }

    public void setInternalParticipant(InternalParticipant internalParticipant) {
        this.internalParticipant = internalParticipant;
    }

    @ManyToOne
    public Time getTime() {
        return this.time;
    }

    public void setTime(Time time) {
        this.time = time;
    }

    @ManyToOne
    public Day getDay() {
        return day;
    }

    public void setDay(Day day) {
        this.day = day;
    }
}

但是当我创建UserAccessReservation时,我收到此异常

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'useraccess0_.internalParticipant_uhr_idReservation' in 'field list'
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.Util.getInstance(Util.java:408)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2490)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1966)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:71)
    ... 178 more

你知道错误在哪里吗?谢谢 partial schema

0 个答案:

没有答案