版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26906345/article/details/79140085
下载地址
http://download.csdn.net/download/qq_26906345/10225021
注意的点
一. hibernate.cfg.xml中的hbm2ddl.auto(除了2为数字外,另一个是字母L)属性必须为update
<property name="hbm2ddl.auto">update</property>
二.如果采用new->other->Hibernate->Hibernate XML Mapping file(hbm.xml)生成的Person.hbm.xml,需要做以下改动
1. id需要指定为主键
<generator class="identity" />
2. table改为SCHOOL,因为要生成两个表
<list name="schools" inverse="false" table="school" lazy="true">
3.给List的索引定义字段
<list-index>
<column name="list_order"/>
</list-index>
4.正确的Person.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">
<!-- Generated 2018-1-23 14:25:48 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="test.Person" table="PERSON">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<list name="schools" inverse="false" table="school" lazy="true">
<key>
<column name="ID" />
</key>
<list-index>
<column name="list_order"/>
</list-index>
<element type="java.lang.String">
<column name="SCHOOLS" />
</element>
</list>
</class>
</hibernate-mapping>
5.改之前的Person.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">
<!-- Generated 2018-1-23 14:25:48 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="test.Person" table="PERSON">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<list name="schools" inverse="false" table="PERSON" lazy="true">
<key>
<column name="ID" />
</key>
<list-index></list-index>
<element type="java.lang.String">
<column name="SCHOOLS" />
</element>
</list>
</class>
</hibernate-mapping>
三、数据库里不要创建person表和school,负责会起冲突,hibernate可以自动创建表
1.控制台输出的sql语句
通过hibernate.cfg.xml中name=”show_sql”来控制的,开发中设置为true,发布后设置为false
<property name="show_sql">true</property>
Hibernate:
insert
into
PERSON
(NAME, AGE)
values
(?, ?)
Hibernate:
insert
into
school
(ID, list_order, SCHOOLS)
values
(?, ?, ?)
Hibernate:
insert
into
school
(ID, list_order, SCHOOLS)
values
(?, ?, ?)
Hibernate:
insert
into
school
(ID, list_order, SCHOOLS)
values
(?, ?, ?)
2.mysql查询运行结果
四、Hibernate 映射集合属性 完整代码(参考李刚老师java EE5.6.4)
Hibernate需要映射集合是因为以往需要用sql进行的关联查询,在Hibernate中是自动生成sql语句,所以把常用的外表内容一次性加入到实体类和Session中,有利于提高系统性能,而不是做多次查询,再进行后台处理。
1.Person.java 实体类
package test;
import java.util.List;
import java.util.ArrayList;
public class Person
{
//标识属性
private Integer id;
//普通属性name
private String name;
//普通属性age
private int age;
//集合属性,保留该对象关联的学校
private List<String> schools = new ArrayList<String>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<String> getSchools() {
return schools;
}
public void setSchools(List<String> schools) {
this.schools = schools;
}
}
2.PersonManager.java 管理Session生成类
package test;
import org.hibernate.Transaction;
import org.hibernate.Session;
import java.util.List;
import java.util.ArrayList;
public class PersonManager
{
public static void main(String[] args)
{
PersonManager mgr = new PersonManager();
mgr.createAndStorePerson();
HibernateUtil.sessionFactory.close();
}
//创建并保存Person对象
private void createAndStorePerson()
{
//打开线程安全的session对象
Session session = HibernateUtil.currentSession();
//打开事务
Transaction tx = session.beginTransaction();
//创建Person对象
Person yeeku = new Person();
//为Person对象设置属性
yeeku.setAge(29);
yeeku.setName("crazyit.org");
//创建List集合
List<String> schools = new ArrayList<String>();
schools.add("小学");
schools.add("初中");
schools.add("高中");
//设置List集合属性
yeeku.setSchools(schools);
session.save(yeeku);
tx.commit();
HibernateUtil.closeSession();
}
}
3.HibernateUtil.java 工具类 创建一个线程安全的session进程
package test;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil
{
public static final SessionFactory sessionFactory;
static
{
try
{
//采用默认的hibernate.cfg.xml来启动一个Configuration的实例
Configuration configuration = new Configuration()
.configure();
//由Configuration的实例来创建一个SessionFactory实例
sessionFactory = configuration.buildSessionFactory();
}
catch (Throwable ex)
{
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
//ThreadLocal可以隔离多个线程的数据共享,因此不再需要对线程同步
public static final ThreadLocal<Session> session
= new ThreadLocal<Session>();
public static Session currentSession()
throws HibernateException
{
Session s = session.get();
//如果该线程还没有Session,则创建一个新的Session
if (s == null)
{
s = sessionFactory.openSession();
//将获得的Session变量存储在ThreadLocal变量session里
session.set(s);
}
return s;
}
public static void closeSession()
throws HibernateException
{
Session s = session.get();
if (s != null)
s.close();
session.set(null);
}
}
4.Person.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">
<!-- Generated 2018-1-23 14:25:48 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="test.Person" table="PERSON">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<list name="schools" inverse="false" table="school" lazy="true">
<key>
<column name="ID" />
</key>
<list-index>
<column name="list_order"/>
</list-index>
<element type="java.lang.String">
<column name="SCHOOLS" />
</element>
</list>
</class>
</hibernate-mapping>
5.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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">0308</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.timeout">5000</property>
<!-- 最大缓存多少个statement对象 -->
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hbm2ddl.auto">update</property>
<!-- JNDI tomcat -->
<property name="connection.datasource">java:comp/env/jdbc/dstest</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 显示Hibernate持久化操作所生成的SQL -->
<property name="show_sql">true</property>
<!-- 将SQL脚本进行格式化后再输出 -->
<property name="hibernate.format_sql">true</property>
<!-- <mapping resource="com/cauc/app/bean/News.hbm.xml"/>
--> <mapping resource="test/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>