Hibernate Chapter 2: Association ing

Source: Internet
Author: User
Document directory
  • 1. Copying-to-one
  • 2. ont-to-memory
1. Copying-to-one

I was going to write it, but I can only write one-to-one, one-to-one, and one-to-one. Sorry.

In chapter 1, we have a perceptual knowledge of hibernate. Understand the lifecycle of an object.

Http://blog.csdn.net/wiksys/article/details/7934380

In this chapter, we will learn the associations between objects.

First, recall. How do we deal with associations in the database? The foreign key )! By the way, it is the foreign key. So how can we establish a relationship between the object-oriented model and the database relational model? With this problem in mind, let's start this chapter.

Before learning associations, let's first understand the relationships between entities?

Association: Implemented by an instance where one object holds another object

Generalized relationship: this is achieved through inheritance between objects.

Small experience: Association is an important feature of object relationship ing, but it may cause lower performance. So pay attention to the design.

 

Direction-to-one: one-to-many Association

 

Case: Students and dormitories

One dormitory for multiple students: Multiple to one.

Class Diagram

 

We practice inserting a dormitory record into the table and then adding a student record to the dormitory record.

SQL script

create table dorm(    id number primary key,    name varchar2(20) not null);Create table student(    id number primary key,    dorm_id number,    name varchar2(20) not null,    foreign key(dorm_id) references dorm(id));create sequence hibernate_sequence start with 1;insert into dorm values(hibernate_sequence.nextval,'101');insert into dorm values(hibernate_sequence.nextval,'102');insert into student values(hibernate_sequence.nextval,21,'wiksys');commit;

Entity class: dormitory :( dorm. Java)

Package Org. wiksys. chap02.bean;/*** dormitory table * @ author wiksys **/public class dorm implements Java. io. serializable {private long ID; private string name; public long GETID () {return ID;}/*** setter and getter .... */}

Ing file: dorm. HBM. xml

<? XML version = "1.0"?> <! Doctype hibernate-mapping public "-// hibernate/hibernate DTD ing DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

Entity: Student (student. Java)

Package Org. wiksys. chap02.bean;/*** student class ** @ author wiksys **/public class student implements Java. io. serializable {private long ID; private dorm; private string name;/*** setter and getter */}

Ing file: Student. HBM. xml

<? XML version = "1.0"?> <! Doctype hibernate-mapping public "-// hibernate/hibernate DTD ing DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

 

Common attributes of sequence-to-one elements:

Attribute Meaning and function Required? Default Value
Name Name of the ing Class Property Y
Class Fully Qualified name of the associated class N
Column Associated Fields N
Not-Null Set whether the associated field is empty N False
Lazy Load Policy N Proxy
Fetcj Capture Policy N Select

Test: insert data:

Package Org. wiksys. chap02.test; import Org. hibernate. hibernateexception; import Org. hibernate. session; import Org. hibernate. sessionfactory; import Org. hibernate. transaction; import Org. hibernate. cfg. configuration; import Org. wiksys. chap02.bean. dorm; import Org. wiksys. chap02.bean. student; public class hibernatetest {/*** @ author wiksys * @ return test * @ Param ARGs */public static void main (string [] ARGs) {student = new student (); dorm = new dorm (); dorm. setname ("104"); student. setname ("Jerry"); student. setdorm (dorm); sessionfactory Sf = NULL; Session session = NULL; transaction Tx = NULL; try {Sf = new configuration (). configure (). buildsessionfactory (); Session = SF. opensession (); Tx = session. begintransaction (); Session. save (dorm); Session. save (student); Tx. commit ();} catch (hibernateexception e) {Tx. rollback (); E. printstacktrace ();} finally {session. close (); SF. close ();}}}

 

2. ont-to-memory

One-to-one association: one-way multi-to-one Association

Case: dormitory and students

Multiple students in one dormitory

Is multiple-to-one. Add a record for the same exercise

The SQL script is the same as the preceding one.

Dormitory class: dorm. Java

Package Org. wiksys. chap02.bean2; import Java. util. hashset; import Java. util. set;/*** dormitory table * @ author wiksys **/public class dorm implements Java. io. serializable {private long ID; private string name; // set to save private set <student> students = new hashset <student> ();/*** setter and getter */}

Ing file: dorm. HBM. xml

<? XML version = "1.0"?> <! Doctype hibernate-mapping public "-// hibernate/hibernate DTD ing DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

Set attribute in One-to-pair

Attribute Meaning and definition Required Default Value
Name Name of the ing file Property Y
Table Target database table of association class Y
Lazy Specify whether the associated object uses a delayed loading policy N Proxy
Fetch Capture Policy N Select

 

Student: Student. Java

Package Org. wiksys. chap02.bean2;/*** student table * @ author wiksys **/public class student implements Java. io. serializable {private long ID; private long dormid; private string name;/*** setter and getter */}

Ing file: Student. 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">

By the way, you must register the ing file in hibernate. cfg. xml:

<! -- Chapter 2 many-to-one --> <! -- <Mapping Resource = "org/wiksys/chap02/bean/dorm. HBM. XML "/> <Mapping Resource =" org/wiksys/chap02/bean/student. HBM. XML "/> --> <! -- Chapter two: one-to-many --> <Mapping Resource = "org/wiksys/chap02/bean2/dorm. HBM. XML "/> <Mapping Resource =" org/wiksys/chap02/bean2/student. HBM. XML "/>

 

Test one to multiple:

Package Org. wiksys. chap02.test; import Org. hibernate. hibernateexception; import Org. hibernate. session; import Org. hibernate. sessionfactory; import Org. hibernate. transaction; import Org. hibernate. cfg. configuration; import Org. wiksys. chap02.bean2. dorm; import Org. wiksys. chap02.bean2. student; public class hibernatetest2 {/*** @ author wiksys * @ return test * @ Param ARGs */public static void main (string [] ARGs) {student = new student (); student. setname ("Jerry"); dorm = new dorm (); dorm. setname ("104"); dorm. getstudents (). add (student); sessionfactory Sf = NULL; Session session = NULL; transaction Tx = NULL; try {Sf = new configuration (). configure (). buildsessionfactory (); Session = SF. opensession (); Tx = session. begintransaction (); Session. save (dorm); Session. save (student); Tx. commit ();} catch (hibernateexception e) {Tx. rollback (); E. printstacktrace ();} finally {session. close (); SF. close ();}}}

 

Source code: http://download.csdn.net/detail/wiksys/4545505

Related jar files: http://download.csdn.net/detail/wiksys/4545524

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.