One-on-one
One-to-one primary key Association
1.XML configuration
- Must be bi-directional
- The primary key generation strategy of the associated party is a foreign key generation strategy
- The associated party one-to-one needs to set constrained= "true" constraint
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.kaishengit.pojo"> <class name="Person" table="person"> <id column="id" name="id"> <generator class="native"/> </id> <property name="personName" column="person_name"/> <one-to-one name="card" cascade="all"/> </class> </hibernate-mapping> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.kaishengit.pojo"> <class name="Card" table="card"> <id column="id" name="id"> <generator class="foreign"> <param name="property">person</param> </generator> </id> <property name="cardNum" column="card_num"/> <one-to-one name="person" constrained="true"/> </class> </hibernate-mapping>
2.Java Annotation Configuration
- The main control party uses @PrimaryKey Join Column
- The accused should add (mappedBy = card) in @OneToOne.
//Annotation-based configuration @Entity @Table(name = "person") public class Person { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(name = "person_name") private String personName; //Sharing primary key associations using PrimaryKey Join Column @OneToOne(cascade = CascadeType.ALL) @PrimaryKeyJoinColumn private Card card; } @Entity @Table(name = "card") public class Card { @Id @GenericGenerator(name = "generator",strategy = "foreign", parameters = @org.hibernate.annotations.Parameter(name = "property",value = "person")) @GeneratedValue(generator = "generator") private Integer id; @Column(name = "card_num") private String cardNum; //You must set OneToOne,mappedBy, as your attribute name in the other party @OneToOne(mappedBy = "card") private Person person; }
One-to-one foreign key Association
database structure
database structure
1.XML configuration
- Each primary key generation strategy has no foreign key constraints
- The main control party (the party with foreign keys) uses many-to-one and adds unique="true" constraints.
- The accused uses one-to-one and adds property-ref="passageContent"
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.kaishengit.pojo"> <class name="Passage" table="passage"> <id name="id" column="id"> <generator class="native"/> </id> <property name="title" column="title"/> <many-to-one name="passageContent" column="content_id" class="PassageContent" unique="true"/> </class> </hibernate-mapping> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.kaishengit.pojo"> <class name="PassageContent" table="passage_content"> <id name="id" column="id"> <generator class="native"/> </id> <property name="content" column="content"/> <one-to-one name="passage" class="Passage" property-ref="passageContent"/> </class> </hibernate-mapping>
2.Java Annotation Configuration
- Unlike XML configuration, all of them use @OneToOne
- The main control party uses @JoinColumn (name = content_id)
- The accused uses @OneToOne (mappedBy = passageContent), which is equivalent to property-ref in XML and is a reverse declaration.
@Entity @Table(name = "passage") public class Passage { @Id @GeneratedValue private Integer id; private String title; @OneToOne @JoinColumn(name = "content_id") private PassageContent passageContent; } @Entity @Table(name = "passage_content") public class PassageContent { @Id @GeneratedValue private Integer id; private String content; @OneToOne(mappedBy = "passageContent") private Passage passage; }
One-to-many Association
-
Save one end before multiple ends
database structureOne-to-many Association1.XML configuration
- At one end of the set, the table and associated keys (i.e., table and key attributes) are specified, and relationship maintenance is abandoned.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.kaishengit.pojo"> <class name="Address" table="address"> <id name="id" column="id"> <generator class="native"/> </id> <property name="cityName" column="city_name"/> <property name="address" column="address"/> <many-to-one name="user" class="User" column="user_id" lazy="false" fetch="join"></many-to-one> </class> </hibernate-mapping> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.kaishengit.pojo"> <class name="User" table="user"> <cache usage="read-only"/> <id name="id" column="id"> <generator class="native"/> </id> <property name="username" column="username"/> <set name="addressSet" table="address" cascade="delete" inverse="true"> <key column="user_id"></key> <one-to-many class="Address"></one-to-many> </set> </class> </hibernate-mapping>
2.Java Annotation Configuration
- Join Column is added to ManyToOne, and mappedBy is added to OneToMany.
- Cascade is added to one end if cascade operation is required
@Entity @Table public class Address { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(name = "city_name") private String cityName; private String address; @ManyToOne @JoinColumn(name = "user_id") private User user; } @Entity @Table public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; private String username; @OneToMany(mappedBy = "user") @Cascade(CascadeType.DELETE) private Set<Address> addressSet; }
Many-to-many Association
database structure
Multi-pair Association
1.XML configuration
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.kaishengit.pojo"> <class name="Student" table="student"> <id name="id" column="id"> <generator class="native"/> </id> <property name="studentName" column="student_name"/> <set name="teacherSet" table="student_teacher_real"> <key column="student_id"></key> <many-to-many class="Teacher" column="teacher_id"/> </set> </class> </hibernate-mapping> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.kaishengit.pojo"> <class name="Teacher" table="teacher"> <id name="id" column="id"> <generator class="native"/> </id> <property name="teacherName" column="teacher_name"/> <set name="studentSet" table="student_teacher_real" inverse="true"> <key column="teacher_id"></key> <many-to-many class="Student" column="student_id"/> </set> </class> </hibernate-mapping>
2.Java Annotation Configuration
@Entity @Table public class Student { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(name = "student_name") private String studentName; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @ManyToMany @JoinTable(name = "student_teacher_real",joinColumns = {@JoinColumn(name = "student_id")},inverseJoinColumns = {@JoinColumn(name = "teacher_id")}) private Set<Teacher> teacherSet; } @Entity @Table public class Teacher { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(name = "teacher_name") private String teacherName; @ManyToMany(mappedBy = "teacherSet") private Set<Student> studentSet; }
Java Annotation Summary
- If it is a primary key association, then use @PrimaryKey JoinColumn, as long as it declares that @JoinColumn, @PrimaryKey JoinColumn, @JoinTable belongs to the main control, and foreign keys are usually stored in the main control, and the corresponding ones containing mappedBy are the accused.
- One-to-one associations, whether primary or foreign key associations, are OneToOne based on annotations
Summary of XML Configuration
- When one-to-one primary key Association occurs, because the primary key of the accused party is the foreign key of the accused party, constrained="true" constraint is required in the OneToOne tag of the accused party.
- The accused usually needs to set inverse="true" to abandon relationship maintenance.
- Labels like < one-to-many > < many-to-many > are placed inside the < set > collection