Reputation: 23529
I have a class like this...
@Entity
public class User{
private String userId;
@Id
public String getUserId(){
return userId;
}
public void setUserId(String userId){
this.userId = userId;
}
}
@Embeddible
public class RegPk{
private String serial;
private String userId;
....
}
@Entity
@IdClass(RegPk.class)
public class Registration {
private String userId, serial;
private User user
@Id
@Column(name="SRL_C")
public String getSerial() {return serial;}
public void setSerial(String serial) {this.serial = serial;}
@ManyToOne(cascade={CascadeType.REFRESH})
@JoinColumn(name="USERID", referencedColumnName="USERID", nullable = false)
public User getUser() {return user;}
public void setUser(User user) {this.user = user;}
@Id
@Column(name="USERID", nullable = false)
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
RegPk pk = new RegPk();
pk.setSerial(dr.getSerial());
pk.setUserId(dr.getUserId());
Registration userOld = em.find(Registration.class, pk);
But when I try to run it I get null back. I swear I thought I had it working so...
1.) is this kind of thing even possible? 2.) what am I doing wrong?
Upvotes: 0
Views: 2648
Reputation: 691795
Yes, it's possible, provided you use the MapsId annotation. Otherwise, you have two different fields mapped to the same column, which is invalid.
The javadoc provides an example which almost matches exactly with your situation.
Upvotes: 1