Unique Key
An entity can used a simple key made of a unique field. The only requirement is to annotate this field with @Key. The set of allowed types for unique keys is:
boolean Boolean
byte Byte
char Character
int Integer
long Long
short Short
String
BigInteger
BigDecimal
@Temporal(TemporalType.DATE)
java.util.Date
java.sql.Date
java.math.BigDecimal
java.math.BigDecimal
java.math.BigInteger
// Don't - Not recommended
double Double
float Float Generated Value Strategy
The @GeneratedValue annotation can be used to specify an id generation strategy via a GenerationType. A sequence name can be specified using the proper annotation property.
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
Composite Key
Here is an example of a composite key:/** * Composite key must implement Serializable * and must be public. */ @AutoProperty public class CompositeKey implements Serializable { // Must be public or protected public String s; public long l; // Default public parameter-less // constructor is required public CompositeKey() { } // equals() must be implemented @Override public boolean equals(Object o) { return Pojomatic.equals(this, o); } // hashCode() must be implemented @Override public int hashCode() { return Pojomatic.hashCode(this); } @Override public String toString() { return Pojomatic.toString(this); } }
@Entity public class WithEmbeddedId { @EmbeddedId private CompositeKey id; private String data; // Setter & Getter }
Using @IdClass:
@Entity @IdClass(CompositeKey.class) public class WithIdClass { @Id String s; @Id long l; private String someData; public WithIdClass() { } // Setter & Getter }
No comments:
Post a Comment