See code below for my 2 entity classes - when I call the findAll() method from my OrigRepository class, it joins these two tables using both primary keys. I want the join to be between the primary key of the Orig table and the foreign key entry in the MsgResponse table ("OrigID") - any sugggestions?
Orig Entity
@Entity
@Table(name = "originator")
public class Orig {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "OrigID")
private int OrigID;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "OrigID")
private MsgResponse responseInfo;
}
MsgResponse Entity
@Entity
@Table(name = "message_response")
public class MsgResponse {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private int responseId;
@Column(name = "OrigID")
private int OrigId;
@OneToOne(mappedBy="responseInfo")
private Orig OrigInfo;
}
I suggest you to see the jpa documentation here .
Example 1 should be your case
Try to swap relation ownership, that is:
@Entity
@Table(name = "originator")
public class Orig {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "OrigID")
private int OrigID;
@OneToOne(mappedBy="origInfo")
private MsgResponse responseInfo;
}
@Entity
@Table(name = "message_response")
public class MsgResponse {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private int responseId;
// @Column(name = "OrigID")
// private int origId;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "OrigID")
private Orig origInfo;
}
Note that the @JoinColum
annotation is in now in the MsgResponse entity. This is because in a @OneToOne
the join column refers to the source entity (see here ).
Hope this could help.
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.