You can make use of @IdClass to make a composite key with those two fields. First, you need to define a new class that will hold the composite key fields:
public class ActivityKey implements Serializable {
protected Integer activity_id;
protected String type;
public ActivityKey () {}
public ActivityKey (Integer activity_id, String type) {
this.activity_id= activity_id;
this.type= type;
}
}
And then, your Activity class must look like this:
@Entity
@IdClass(ActivityKey.class)
class Activity {
@Id
private Integer activity_id;
@Id
private String type;
//Other fields
}
There are restrictions to apply when defining the primary key class (ActivityKey in this example). This class must be serializable and must define both hashCode() and equals() methods. Also, it must be public and have a public no-arg constructor.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…