today, when doing a project, an intermediate table has no primary key, and all of the entities are created without components, resulting in the following error:
class for Entity:xxx
As you can see, it indicates that a certain class is an entity that does not specify an identifier, mainly because Hibernate discovers its primary key identity when it scans the Entity. therefore, The primary key identification is added on its class. Because my class is more special, I need to add a federated primary key.
There are three main ways of using Hibernate annotations for federated primary Keys:
first, the field of the Federated primary key is placed in a single class, the class needs to implement the Java.io.Serializable interface and override Equals and hascode, and then annotate the class as @embeddable, and finally in the main class (the class does not contain the fields in the Federated primary key Class) Save a reference to the Federated primary key class and generate the set and get methods, and annotate the reference as @id
@Entity @Table (name= "Test01") public classTest01Implementsserializable{Private Static Final LongSerialversionuid = 3524215936351012384L; PrivateString address; Private intage ; PrivateString email; PrivateString phone; @IdPrivateTestKey01 testKey;}
Primary KEY Class:
@Embeddable public classTestkey01Implementsserializable{Private Static Final LongSerialversionuid = -3304319243957837925l; Private Longid; PrivateString name; /** * @returnthe ID*/ public LonggetId () {returnid; } /** * @paramID The ID to set*/ public voidSetId (LongId) { this. ID =id; } /** * @returnthe name*/ publicString getName () {returnname; } /** * @paramname the name to set*/ public voidsetName (String Name) { this. Name =name; } @Override public Booleanequals (Object o) {if(oinstanceofTestkey0101) {Testkey01 Key=(TestKey01) o; if( this. id = = Key.getid () && this. Name.equals (key.getname ())) { return true ; } } return false ; } @Override public inthashcode () {return this. Name.hashcode (); } }
The field of the Federated primary key is placed in a single class, the class needs to implement the Java.io.Serializable interface and override Equals and hascode, and finally save a reference to the Federated primary key class in the main class (the class does not contain a field in the Federated primary key class). and generate the set and get methods, and annotate the reference as @embeddedid
@Entity @Table (name= "Test02") publicclass Test02 { Private String address; Private int age ; Private String email; Private String phone; @EmbeddedId private TestKey02 testKey;}
TESTKEY02 is a common Java class.
third, Place the field of the Federated primary Key in a single class, which requires implementing the Java.io.Serializable interface and overriding Equals and Hashcode. finally, in the main class, which contains the fields in the Federated primary key class, annotate the Federated primary key field as @id, And above this class will be such annotations: @IdClass (union primary key class. Class)
@Entity @Table (name= "Test03") @IdClass (TestKey03. class ) publicclass Test03 { @Id privatelong id; @Id private String name;}
TESTKEY03 is a common Java class.
Hibernate annotation three main ways to map federated primary keys