The most common type of primary key is the single-field primary key, which uses two or more fields as the primary key, and is commonly used in a table where multiple fields can uniquely indicate a single record. For example, stock data sheets, stock codes, dates, and close prices as primary keys. Each stock, on a specific date, can have only one closing price. The database management system uses MySQL to create a table person with a combined primary key.
The CREATE TABLE person (name VARCHAR (255) is not NULL, an age of BIGINT UNSIGNED not null, adress VARCHAR (255), PRIMARY KEY (n Ame, age) ENGINE = InnoDB;
JPA Mapping @idclass Definition Tool class
Package Com.gxz.entities;import Java.io.serializable;public class Personcompositeid implements Serializable {private String name; Private long age; Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public long Getage () {return age;} public void Setage (long age) {this.age = age;}}
The properties of the tool class must exactly match the @id property of the entity class. Include number, name, and cannot have extra attributes. Defining entity Classes
Package Com.gxz.entities;import Javax.persistence.entity;import Javax.persistence.id;import Javax.persistence.idclass;import javax.persistence.Table; @Entity @table@idclass (Personcompositeid.class) public Class Person {private String name; Private long age; Private String adress; @Idpublic String GetName () {return name;} public void SetName (String name) {this.name = name;} @Idpublic long Getage () {return age;} public void Setage (long age) {this.age = age;} Public String getadress () {return adress;} public void setadress (String adress) {this.adress = adress;}}
The entity class has two attributes marked as @id, which indicates that the primary key is a combined primary key, and the primary key attribute is name, age. Accordingly, the tool class Personcompositeid must have two attributes, name, age, respectively. Note that this means that the tool class must have GetName, SetName, Getage, Setage, not the field name, age, and the distinction between attributes and fields. @IdClass (Personcompositeid.class): Represents a composite primary key defined using the tool class Personcompositeid. In addition, the tool class Personcompositeid must implement serialization serializable, otherwise, the following exception is reported.
[Persistenceunit:entitymappings] Unable to build Hibernate Sessionfactorycomposite-id class must implement Serializable: Com.gxz.entities.personcompositeidcomposite-id class must implement Serializable: Com.gxz.entities.PersonCompositeId
Persistence of
person person = new person (); Person.setname ("John Doe"); Person.setage (+); Person.setadress ("Guangzhou"); Manager.persist (person);
Find entities by ID
Personcompositeid Personcompositeid = new Personcompositeid (); Personcompositeid.setname ("Zhang San"); Personcompositeid.setage (+); Person person = Manager.find (Person.class, Personcompositeid); if (person! = null) { System.out.println ("Name:" + person.getname () + "Age:" + person.getage ());}
JPA Mapping @embeddedid Definition Tool class
Package Com.gxz.entities;import Java.io.serializable;import javax.persistence.Embeddable; @Embeddablepublic class Personcompositeid implements Serializable {private String name; Private long age; Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public long Getage () {return age;} public void Setage (long age) {this.age = age;}}
@Embeddable: Indicates that the tool class is used to combine primary keys, and properties are properties that combine primary keys.
Defining entity Classes
Package Com.gxz.entities;import Javax.persistence.embeddedid;import Javax.persistence.entity;import javax.persistence.Table; @Entity @tablepublic class Person {private Personcompositeid Personcompositeid; @EmbeddedId Public Personcompositeid Getpersoncompositeid () {return personcompositeid;} public void Setpersoncompositeid (Personcompositeid personcompositeid) {This.personcompositeid = PersonCompositeId;} Private String adress; Public String getadress () {return adress;} public void setadress (String adress) {this.adress = adress;}}
@EmbeddedId: Indicates that the property is a composite primary key, and the type is a composite primary key tool class.
Persistence of
Personcompositeid Personcompositeid = new Personcompositeid (); Personcompositeid.setname ("Search Karma"); Personcompositeid.setage (+); Person.setpersoncompositeid (Personcompositeid); Person.setadress ("Guangzhou"); Manager.persist (person); Transaction.commit ();
Comparison of the two mapping methods the first way, the tool class and the entity class have identical attributes, belong to redundancy, the second way is not without redundancy, appears more scientific. Therefore, the second method is more commonly used in practical work.
Combining primary keys and JPA mappings