hibernate映射简单实例
生活随笔
收集整理的這篇文章主要介紹了
hibernate映射简单实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1創建數據庫:
1 --班級表 2 create table grade 3 ( 4 gid number primary key, --班級ID 5 gname varchar2(50), --班級名稱 6 gdesc varchar2(50) --班級介紹 7 ); 8 --學生表 9 create table student 10 ( 11 sid number primary key, --主鍵ID學生ID 12 sname varchar2(20), --學生姓名 13 sex varchar2(20), --學生性別 14 gid number references grade(gid) ---外鍵班級ID 15 ); 16 17 --學生證表 18 create table paper 19 ( 20 pid number primary key, 21 pdesc varchar2(100) , 22 sid number references student(sid) not null 23 24 );二、創建配置文件和持久化類:
學生類
package entity;/** 學生類*/ public class Student implements java.io.Serializable {// Fieldsprivate static final long serialVersionUID = 1L;private int sid;private String sname;private String sex;//增加班級屬性private Grade grade;//學生證類private Paper paper;// Constructors/** default constructor */public Student() {}/** minimal constructor */public Student(int sid) {this.sid = sid;}/** full constructor */public Student(int sid, String sname, String sex ) {this.sid = sid;this.sname = sname;this.sex = sex;}// Property accessorspublic int getSid() {return this.sid;}public void setSid(int sid) {this.sid = sid;}public String getSname() {return this.sname;}public void setSname(String sname) {this.sname = sname;}public String getSex() {return this.sex;}public void setSex(String sex) {this.sex = sex;}public Grade getGrade() {return grade;}public void setGrade(Grade grade) {this.grade = grade;}public Paper getPaper() {return paper;}public void setPaper(Paper paper) {this.paper = paper;}} View Code學生證類
1 package entity; 2 3 /* 4 * 學生證類 5 */ 6 7 public class Paper implements java.io.Serializable { 8 9 // Fields 10 11 private static final long serialVersionUID = 1L; 12 private int pid; 13 private Student student; 14 private String pdesc; 15 16 // Constructors 17 18 /** default constructor */ 19 public Paper() { 20 } 21 22 /** minimal constructor */ 23 public Paper(int pid) { 24 this.pid = pid; 25 } 26 27 /** full constructor */ 28 public Paper(int pid, Student student, String pdesc) { 29 this.pid = pid; 30 this.student = student; 31 this.pdesc = pdesc; 32 } 33 34 // Property accessors 35 36 public int getPid() { 37 return this.pid; 38 } 39 40 public void setPid(int pid) { 41 this.pid = pid; 42 } 43 44 public Student getStudent() { 45 return this.student; 46 } 47 48 public void setStudent(Student student) { 49 this.student = student; 50 } 51 52 public String getPdesc() { 53 return this.pdesc; 54 } 55 56 public void setPdesc(String pdesc) { 57 this.pdesc = pdesc; 58 } 59 60 } View Codehibernate.cfg.xml配置文件
1 <?xml version='1.0' encoding='UTF-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 8 <session-factory> 9 <property name="dialect"> 10 org.hibernate.dialect.Oracle9Dialect 11 </property> 12 <property name="connection.url"> 13 jdbc:oracle:thin:@localhost:1521:orcl 14 </property> 15 <property name="connection.username">root</property> 16 <property name="connection.password">root</property> 17 <property name="connection.driver_class"> 18 oracle.jdbc.OracleDriver 19 </property> 20 <property name="show_sql">true</property> 21 <property name="format_sql">true</property> 22 23 <mapping resource="entity/Grade.hbm.xml" /> 24 <mapping resource="entity/Student.hbm.xml" /> 25 <mapping resource="entity/Paper.hbm.xml" /> 26 27 </session-factory> 28 29 </hibernate-configuration>Student類配置文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 5 <hibernate-mapping> 6 <class name="entity.Student" table="STUDENT" schema="ROOT"> 7 <id name="sid" type="java.lang.Integer"> 8 <column name="SID" precision="22" scale="0" /> 9 <generator class="assigned" /> 10 </id> 11 <property name="sname" type="java.lang.String"> 12 <column name="SNAME" length="20" /> 13 </property> 14 <property name="sex" type="java.lang.String"> 15 <column name="SEX" length="20" /> 16 </property> 17 <!--配置grade屬性 --> 18 <many-to-one name="grade" class="entity.Grade" cascade="save-update"> 19 <!--指定學生表中的外鍵 --> 20 <column name="GID" /> 21 </many-to-one> 22 <!-- 添加學生證的配置 --> 23 <one-to-one name="paper" class="entity.Paper" cascade="all" lazy="false"/> 24 </class> 25 </hibernate-mapping>Paper類配置文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 5 <hibernate-mapping> 6 <class name="entity.Student" table="STUDENT" schema="ROOT"> 7 <id name="sid" type="java.lang.Integer"> 8 <column name="SID" precision="22" scale="0" /> 9 <generator class="assigned" /> 10 </id> 11 <property name="sname" type="java.lang.String"> 12 <column name="SNAME" length="20" /> 13 </property> 14 <property name="sex" type="java.lang.String"> 15 <column name="SEX" length="20" /> 16 </property> 17 18 <!-- 添加學生證的配置 --> 19 <one-to-one name="paper" class="entity.Paper" cascade="all" lazy="false" property-ref="student"/> 20 </class> 21 </hibernate-mapping>測試類
1 package Test; 2 3 import org.hibernate.Session; 4 import org.hibernate.Transaction; 5 import org.hibernate.cfg.Configuration; 6 7 import entity.Paper; 8 import entity.Student; 9 10 public class Demo6 { 11 12 public static void main(String[] args) { 13 findByStu(); 14 } 15 16 public static void save() { 17 18 Student stu1 = new Student(); 19 stu1.setSid(20151109); 20 stu1.setSname("王霸"); 21 stu1.setSex("女"); 22 23 Paper paper=new Paper(); 24 paper.setPid(9001); 25 paper.setPdesc("王霸的學生證"); 26 paper.setStudent(stu1); 27 28 29 stu1.setPaper(paper); 30 31 // 建立session 32 Session session = new Configuration().configure().buildSessionFactory() 33 .openSession(); 34 // 開始事務 35 Transaction transaction = session.beginTransaction(); 36 37 38 // 保存學生證 39 session.save(stu1); 40 // 提交事務 41 transaction.commit(); 42 // 關閉session 43 session.close(); 44 45 } 46 47 public static void find() { 48 // 建立session 49 Session session = new Configuration().configure().buildSessionFactory() 50 .openSession(); 51 Paper paper=(Paper) session.get(Paper.class, 9001); 52 System.out.println(paper.getPid()+paper.getPdesc()); 53 Student stu1=paper.getStudent(); 54 System.out.println(stu1.getSid()+"\t"+stu1.getSname()); 55 } 56 public static void findByStu() { 57 // 建立session 58 Session session = new Configuration().configure().buildSessionFactory() 59 .openSession(); 60 Student stu=(Student) session.get(Student.class, 20151109); 61 System.out.println(stu.getSname()); 62 Paper paper=stu.getPaper(); 63 System.out.println(paper.getPid()+"\t"+paper.getPdesc()); 64 } 65 66 }?
轉載于:https://www.cnblogs.com/smellpawn/p/10805392.html
總結
以上是生活随笔為你收集整理的hibernate映射简单实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ_2104 K-th Number
- 下一篇: MyBatis 与 Hibernate