For unidirectional 1 ---> 1 associations, you need to add the setter and getter methods for the reference attribute of the correlated object in the persistence class. From the persistence class, there is no difference between 1 ---> 1 and N ---> 1. Because one end of N or one end of 1 directly accesses the correlated object, you only need to add the setter and getter methods of the correlated object attribute.
 
In fact, the ing configurations of one-way 1 ---> 1 and N ---> 1 are also very similar.<Example-to-one.../>Add ElementUnique = "true"Attribute to indicate that the end of N must be unique. Since one end of N adds a unique constraint, it becomes 1 ---> 1.
 
One-way join based on foreign key 1 ---> 1:
 
Delete the table in the test database first:
 
 
Create a new web project and write the code:
 
 
Person. Java:
 
 
public class Person {private int id;private String name;private int age;private Address address;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}} 
Address. Java:
 
public class Address {private int addressId;private String addressDetail;public int getAddressId() {return addressId;}public void setAddressId(int addressId) {this.addressId = addressId;}public String getAddressDetail() {return addressDetail;}public void setAddressDetail(String addressDetail) {this.addressDetail = addressDetail;}public Address(String addressDetail) {super();this.addressDetail = addressDetail;}} 
Person. HBM. xml:
 
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">    
Address. HBM. xml:
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">    
Test. Java:
Public class test {public static void main (string [] ARGs) {session = hibernatesessionfactory. getsession (); transaction TXT = session. begintransaction (); person P = new person (); p. setname ("Tom"); p. setage (20); Address a1 = new address ("Guangzhou Tianhe"); // ① p. setaddress (A1); Session. persist (p); // ③ address a2 = new address ("Shanghai Hongkou"); // ② p. setaddress (A2); TXT. commit (); hibernatesessionfactory. closesession ();}}
Run test. Java to view the database: