hibernate_04_hbm.xml介绍

Posted tzzt01

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate_04_hbm.xml介绍相关的知识,希望对你有一定的参考价值。

先贴上类文件Students.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <!-- Generated 2017-5-23 0:24:09 by Hibernate Tools 3.5.0.Final -->
 5 <hibernate-mapping>
 6     <class name="com.imooc.hibernate.Students" table="STUDENTS">
 7         <id name="sid" type="int">
 8             <column name="SID" />
 9             <generator class="assigned" />
10         </id>
11         <property name="sname" type="java.lang.String">
12             <column name="SNAME" />
13         </property>
14         <property name="gender" type="java.lang.String">
15             <column name="GENDER" />
16         </property>
17         <property name="birthday" type="java.util.Date">
18             <column name="BIRTHDAY" />
19         </property>
20         <property name="address" type="java.lang.String">
21             <column name="ADDRESS" />
22         </property>
23     </class>
24 </hibernate-mapping>

其中<generator class="assigned" />:适用于自然主键,由java代码设置标识符。

  <generator class="assigned" />:适用于代理主键,hibernate自动生成标识符。

自然主键和代理主键请参考:http://blog.csdn.net/haiross/article/details/21388997

举个栗子:

students类:

 1 package com.imooc.hibernate;
 2 
 3 import java.util.Date;
 4 
 5 public class Students {
 6 
 7     private int sid;
 8     private String sname;
 9     private String gender;
10     private Date birthday;
11     private String address;
12     
13     public Students() {}
14 
15     public Students(int sid, String sname, String gender, Date birthday, String address) {
16         super();
17         this.sid = sid;
18         this.sname = sname;
19         this.gender = gender;
20         this.birthday = birthday;
21         this.address = address;
22     }
23 
24     @Override
25     public String toString() {
26         return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday
27                 + ", address=" + address + "]";
28     }
29 
30     public int getSid() {
31         return sid;
32     }
33 
34     public void setSid(int sid) {
35         this.sid = sid;
36     }
37 
38     public String getSname() {
39         return sname;
40     }
41 
42     public void setSname(String sname) {
43         this.sname = sname;
44     }
45 
46     public String getGender() {
47         return gender;
48     }
49 
50     public void setGender(String gender) {
51         this.gender = gender;
52     }
53 
54     public Date getBirthday() {
55         return birthday;
56     }
57 
58     public void setBirthday(Date birthday) {
59         this.birthday = birthday;
60     }
61 
62     public String getAddress() {
63         return address;
64     }
65 
66     public void setAddress(String address) {
67         this.address = address;
68     }
69 }

hibernate.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://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7         <property name="connection.username">root</property>
 8         <property name="connection.password">root</property>
 9         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
10         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>
11         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
12         <property name="show_sql">true</property>
13         <property name="format_sql">true</property>
14         <property name="hbm2ddl.auto">create</property>
15         
16         <mapping resource="com/imooc/hibernate/Students.hbm.xml"/>
17     </session-factory>
18 </hibernate-configuration>

测试类Students.java

 1 package com.icoom.test;
 2 import static org.junit.Assert.fail;
 3 
 4 import java.util.Date;
 5 
 6 import org.hibernate.Session;
 7 import org.hibernate.SessionFactory;
 8 import org.hibernate.Transaction;
 9 import org.hibernate.cfg.Configuration;
10 import org.hibernate.service.ServiceRegistry;
11 import org.hibernate.service.ServiceRegistryBuilder;
12 import org.junit.After;
13 import org.junit.Before;
14 import org.junit.Test;
15 
16 import com.imooc.hibernate.Students;
17 
18 public class StudentsTest {
19     
20     private SessionFactory sessionFactory;
21     private Session session;
22     private Transaction transaction;
23 
24     @Before
25     public void init() {
26         // 1.创建配置对象
27         Configuration config = new Configuration().configure();
28         // 2.创建服务注册对象
29         ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
30         // 3.创建会话工厂对象
31         sessionFactory = config.buildSessionFactory(serviceRegistry);
32         // 会话对象
33         session = sessionFactory.openSession();
34         // 开启事务
35         transaction = session.beginTransaction();
36     }
37     @After
38     public void destory() {
39         transaction.commit(); //提交事务
40         session.close(); //关闭session
41         sessionFactory.close();//关闭会话工厂
42     }
43     @Test
44     public void testSaveStudents() {
45         // 生成学生对象
46 //        Students s = new Students(1, "老张", "男", new Date(), "山东");
47         Students s = new Students();
48         s.setSid(1);
49         s.setSname("老张");
50         s.setGender("男");
51         s.setBirthday(new Date());
52         s.setAddress("山东");
53         session.save(s);//保存对象进入数据库
54     }
55 }

数据库产生一条数据:

将hibernate.cfg.xml文件中的<property name="hbm2ddl.auto">create</property>改为<property name="hbm2ddl.auto">update</property>,然后再执行一次,就会报错。因为主键重复。

 

----------------------------------

将数据表删除,将Students.hbm.xml文件中的<generator class="assigned" />改成<generator class="native" />,然后再执行一次testSaveStudents()方法,会生成一条数据;再执行一次,Junit不会报错,而是插入了一条新数据:

 

以上就是assigned和native的区别。

以上是关于hibernate_04_hbm.xml介绍的主要内容,如果未能解决你的问题,请参考以下文章

hibernate_已有项目改为hibernate & 函数式编程 + lambda

hibernate_已有项目改为hibernate & 函数式编程 + lambda

hibernate笔记--实体类映射文件"*.hbm.xml"详解

Hibernate__多对一 一对多 一对多对多

Hibernate_添加联系人练习

hibernate.hbm.xml文件配置入门小结