Hibernate Association ing and hibernate Association ing

Source: Internet
Author: User

Hibernate Association ing and hibernate Association ing

Relationship between entities
Association, aggregation, dependency, combination, inheritance

Link Type:
One-to-multiple
Many-to-one
One-to-one
Many-to-many

 

Student table and grade Table:

1 -- student table 2 create table student (3 stuno number (4) primary key, 4 name varchar2 (20) not null, 5 gradeid number (4) 6 7) 8 -- grade table 9 create table grade (10 gradeid number (4) primary key, 11 gradename varchar2 (20) not null12 13)

 

A one-to-many and one-to-one relationship combination is a bidirectional Association.

Therefore, the integration writes a cascade code for adding data and deleting foreign key tables.

1. Create an object

Student:

1 package entity; 2 3 import java. io. serializable; 4/** 5 * Student Entity class 6 * @ author Administrator 7*8 */9 public class Student implements Serializable {10 private Integer stuno; 11 private String name; 12 private Integer gradeid; 13 // The relationship between students and grades is many-to-one. 14 private Grade grade; 15 16 public Student () {} 17 public Integer getStuno () {18 return stuno; 19} 20 public void setStuno (Integer stuno) {21 this. stuno = stuno; 22} 23 public String getName () {24 return name; 25} 26 public void setName (String name) {27 this. name = name; 28} 29 public Integer getGradeid () {30 return gradeid; 31} 32 public void setGradeid (Integer gradeid) {33 this. gradeid = gradeid; 34} 35 public Grade getGrade () {36 return grade; 37} 38 public void setGrade (Grade grade) {39 this. grade = grade; 40} 41 42 43}Student

Grade:

1 package entity; 2 3 import java. io. serializable; 4 import java. util. set; 5/** 6 * Grade entity class 7 * @ author Administrator 8*9 */10 public class Grade implements Serializable {11 // Grade number 12 private Integer gradeid; 13 // Grade 14 private String gradename; 15 16 private Set <Student> stu; 17 18 public Grade () {} 19 20 public Set <Student> getStu () {21 return stu; 22} 23 24 public void setStu (Set <Student> stu) {25 this. stu = stu; 26} 27 28 public Integer getGradeid () {29 return gradeid; 30} 31 public void setGradeid (Integer gradeid) {32 this. gradeid = gradeid; 33} 34 public String getGradename () {35 return gradename; 36} 37 public void setGradename (String gradename) {38 this. gradename = gradename; 39} 40 41 42 43}Grade

 

2. Establish the relationship between objects

1 <! -- Grade Table --> 2 <? Xml version = "1.0"?> 3 <! DOCTYPE hibernate-mapping PUBLIC 4 "-// Hibernate/Hibernate Mapping DTD 3.0 // EN" 5" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd "> 6 7 3. Configure hibernate

1 <! DOCTYPE hibernate-configuration PUBLIC 2 "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" 3" http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd "> 4 5 

The preceding configuration file contains three new configuration nodes and new node attributes:

1. <set name = "stu" cascade = "all"> -- name specifies the attribute name in the object. cascade: cascade ---- all/none/save-update/delete/is literal.

<Key column = "gradeid"/> -- specifies the primary key associated with two objects.
<One-to-learn class = "Student"/> -- this is a one-to-many object class configuration. Because I have configured package in mapping, only the class name is written here.
</Set>

2.<Role-to-one name = "grade" class = "Grade"> -- specifies the attribute name of the object in the "1" link and specifies the class name.

<Column name = "gradeid"> </column> -- specify the associated primary key.
</Role-to-one>

 

3. <property name = "hbm2ddl. auto"> update </property> -- specify the operation type.

# Hibernate. hbm2ddl. auto create-drop
# Hibernate. hbm2ddl. auto create
# Hibernate. hbm2ddl. auto update
# Hibernate. hbm2ddl. auto validate

  

Test code:

    

1 package dao; 2 3 import org. hibernate. session; 4 import org. hibernate. sessionFactory; 5 import org. hibernate. transaction; 6 import org. hibernate. cfg. configuration; 7 8 import entity. grade; 9 import entity. student; 10 11/** 12 * multi-to-one link linkage exercise 13 * @ author Administrator14 * 15 */16 public class StudentDao {17 // open the Configuration 18 SessionFactory factory = new Configuration (). configure (). buildSessionFactory (); 19 Session Session = null; 20 21 public static void main (String [] args) {22 StudentDao stu = new StudentDao (); 23 try {24 // stu. update (); 25 stu. del (); 26} catch (Exception e) {27 // TODO Auto-generated catch block28 e. printStackTrace (); 29 30} 31} 32/** 33 * added 34 */35 public void update () throws Exception {36 session = factory. openSession (); 37 session. beginTransaction (); 38 // Grade grade = (Grade) session. get (Grade. class, 1); 39 40 Student student = new Student (); 41 Grade grade = new Grade (); 42 grade. setGradeid (1); 43 grade. setGradename ("First Grade"); 44 session. save (grade); 45 student. setName ("James"); 46 student. setGrade (grade); 47 session. save (student); 48 session. getTransaction (). commit (); 49 System. out. println ("successful! "); 50 51 session. close (); 52} 53/** 54 * Delete 55 */56 public void del () {57 session = factory. openSession (); 58 session. beginTransaction (); 59 // Student student = (Student) session. get (Student. class, 5); 60 // cascade Delete 61 Grade grade = (Grade) session. get (Grade. class, 1); 62 session. delete (grade); 63 // session. delete (student); 64 session. getTransaction (). commit (); 65 System. out. println ("successful! "); 66 67 session. close (); 68} 69}


 

 

Problems encountered in code: http://www.cnblogs.com/gcs1995/p/4139422.html

 

Related Article

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.