6.1.2 foreign key-based one-way 1-> 1 Association

Source: Internet
Author: User

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:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.