One-way, many-to-one mapping of hibernate learning

Source: Internet
Author: User

? Copyright NOTICE: This article is the original blogger article, reproduced please indicate the source

Description: This instance is implemented by mapping files and annotations in two ways. You can choose the right way according to your own needs.

Instance:

1. Project structure

2.pom.xml

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion><groupid>org.hibernate</groupid><artifactid> hibernate-manytoone</artifactid><version>0.0.1-snapshot</version><packaging>jar</ Packaging><properties><project.build.sourceencoding>utf-8</project.build.sourceencoding> </properties><dependencies><!--Junit--><dependency><groupid>junit</groupid ><artifactid>junit</artifactid><version>4.12</version><scope>test</scope    ></dependency><!--Hibernate--><dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.1.7.final</version></dependency ><!--MySQL--><dependency> <groupId>mysql</groupId> <artifactid>mysql-connector-java</ Artifactid> <version>5.1.42</version></dependency></dependencies></project>

3.grade.java

Package Org.hibernate.entity;import Javax.persistence.column;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import Javax.persistence.id;import org.hibernate.annotations.genericgenerator;@ Entitypublic class Grade {@Id @generatedvalue (generator= "GID") @GenericGenerator (name= "gid", strategy= "native") private int gid;//class @column (length=100) private string gname;//class name private string gdesc;//description public Grade () {}public Grade ( String gname, String gdesc) {super (); this.gname = Gname;this.gdesc = Gdesc;} public int Getgid () {return GID;} public void SetGid (int gid) {this.gid = gid;} Public String Getgname () {return gname;} public void Setgname (String gname) {this.gname = Gname;} Public String Getgdesc () {return gdesc;} public void Setgdesc (String gdesc) {this.gdesc = Gdesc;}}

4.student.java

Package Org.hibernate.entity;import Javax.persistence.column;import Javax.persistence.entity;import Javax.persistence.fetchtype;import Javax.persistence.generatedvalue;import Javax.persistence.id;import Javax.persistence.joincolumn;import Javax.persistence.manytoone;import Org.hibernate.annotations.cascade;import Org.hibernate.annotations.GenericGenerator; @Entitypublic class Student {@Id @generatedvalue (generator= "Sid") @ Genericgenerator (name= "Sid", strategy= "native") private int sid;//Science number @column (length=100) Private String sname;// Name @column (length=2) private String sex;//sex @manytoone (Fetch=fetchtype.eager) @Cascade (value= Org.hibernate.annotations.CascadeType.SAVE_UPDATE) @JoinColumn (name= "GID") private Grade grade;//belongs to class public Student () {}public Student (string sname, String sex, Grade Grade) {this.sname = Sname;this.sex = Sex;this.grade = Grade;} public int GetSID () {return SID;} public void Setsid (int sid) {this.sid = SID;} Public String Getsname () {return sname;} public void Setsname (Stringsname) {this.sname = sname;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;} Public Grade Getgrade () {return Grade;} public void Setgrade (Grade Grade) {this.grade = Grade;}}

5.grade.hbm.xml

<?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 ">

6.student.hbm.xml

<?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 ">

7.hibernate.cfg.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-configuration-3.0.dtd ">

8.testmanytoone.java

Package Org.hibernate.test;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.transaction;import Org.hibernate.cfg.configuration;import Org.hibernate.entity.grade;import Org.hibernate.entity.student;import Org.junit.after;import Org.junit.before;import Org.junit.Test;public class Testmanytoone {private sessionfactory sessionfactory;private Session session;private Transaction transaction;@ Beforepublic void before () {//Create Sessionfactory Object sessionfactory = new Configuration (). Configure (). buildsessionfactory ();//Get Session Object session = Sessionfactory.opensession ();//Open transaction transaction = Session.begintransaction ();} @Afterpublic void After () {//COMMIT transaction Transaction.commit ();//close Sessionsession.close ();// Close Sessionfactorysessionfactory.close ();} @Testpublic void Testadd () {Grade Grade = new Grade ("Java Class One", "Java Application Development class"); Student student1 = new Student ("Zhang San", "male", grade); Student Student2 = new Student ("John Doe", "male", grade); Session.save (grade); Session.save (student1); session.sAve (Student2);} @Testpublic void Testquery () {Student Student = Session.get (Student.class, 1); System.out.println (Student.getsname () + "-" + student.getsex () + "-" + Student.getgrade (). Getgname () + "-" + student. Getgrade (). Getgdesc ());} @Testpublic void Testupdate () {Grade Grade = new Grade ("Java Class II", "Java Application Development Class II"); Student Student = Session.get (Student.class, 2); Student.setgrade (grade); Session.update (Student);} @Testpublic void Testdelete () {Student Student = Session.get (Student.class, 2); Session.delete (Student);}}

9. Preview of effects

9.1 Implementing the Testadd method

9.2 Implementing the Testquery method

9.3 Implementing the Testupdate method

9.4 Implementing the Testdelete method

Reference: http://www.imooc.com/video/8675

http://www.imooc.com/video/10089

One-way, many-to-one mapping of hibernate learning

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.