Hibernate annotations map three main methods of joint primary keys, hibernate primary keys
During project creation today, an intermediate table has no primary key and no components are added when an object is created. The following error is returned:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateSessionFactory' defined in class path resource [spring-config/ac-cif-srv-config.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: XXX
It can be seen that it points out that an object is an entity without a specified identifier. The main reason is that hibernate finds its primary key identifier when scanning the object. Therefore, the primary key ID is added to the class. Because my class is special, you need to add a joint primary key.
There are three main ways to associate primary keys with Hibernate annotation ing:
1. Place fields of the federated primary key in a separate class. This class must implement java. io. serializable interface, rewrite equals and hascode, annotate the class as @ Embeddable, and save a reference to the Union primary key class in the main class (this class does not contain fields in the Union primary key class, generate the set and get methods, and annotate the reference as @ Id
@Entity @Table(name="Test01") public class Test01 implements Serializable{ private static final long serialVersionUID = 3524215936351012384L; private String address ; private int age ; private String email ; private String phone ; @Id private TestKey01 testKey ;}
Primary Key class:
@Embeddable public class Testkey01 implements Serializable{ private static final long serialVersionUID = -3304319243957837925L; private long id ; private String name ; /** * @return the id */ public long getId() { return id; } /** * @param id the id to set */ public void setId(long id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } @Override public boolean equals(Object o) { if(o instanceof Testkey0101){ Testkey01 key = (TestKey01)o ; if(this.id == key.getId() && this.name.equals(key.getName())){ return true ; } } return false ; } @Override public int hashCode() { return this.name.hashCode(); } }
2. Place fields of the federated primary key in a separate class. This class must implement java. io. serializable interface and rewrite equals and hascode. Finally, save a reference to the federated primary key class in the main class (this class does not contain fields in the federated primary key class) and generate the set and get methods, and annotate the reference as @ EmbeddedId
@Entity @Table(name="Test02") public class Test02 { private String address ; private int age ; private String email ; private String phone ; @EmbeddedId private TestKey02 testKey ;}
Testkey02 is a common Java class.
3. separate fields of the federated primary key into a class. This class must implement java. io. serializable interface and rewrite equals and hashcode. finally, in the main class (this class contains fields in the Union primary key class), the Union primary key fields are annotated as @ Id, and the annotation above the class is as follows: @ IdClass (Union primary key class. class)
@Entity @Table(name="Test03") @IdClass(TestKey03.class) public class Test03 { @Id private long id ; @Id private String name ; }
Testkey03 is a common Java class.