Hi Wes,
Wes Wannemacher wrote:
Right now, I have two objects - User and Subscription. There is a
OneToMany relationship that one user can have many Subscriptions (or
none). A subscription should have a composite key based on two of it's
own string fields and the user object that it is linked to...
I had a similar requirement and found a "trick" that makes it work.
Basically you need to create a dummy field in the Subscription that is
the id of the User.
Change your Subscription class like so:
Subscription.java -
@Entity
@Table(name="reader_subscriptions")
@IdClass(Subscription.SubscriptionPK.class)
public class Subscription {
@Id
@Column(name="host", length=255)
private String host;
@Column(name="server_type", length=16)
private String type;
@Id
@Column(name="username", length=32)
private String username;
@Column(name="password", length=32)
private String password;
@Column(name="auto_connect")
private boolean autoConnect;
@Id
@Column(name="username", nullable = false, insertable = false,
updatable = false)
private String username;
@ManyToOne
@JoinColumn(name="username", nullable = false, updatable = false)
and ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
private User user;
public static class SubscriptionPK {
private String host;
private String username;
private User user;
@Override
public int hashCode() {
...
}
@Override
public boolean equals(Object obj) {
...
}
...
}
...
}
Regards,
Michael.