代码组织:
数据库:一个人可以再多个学校学习
======================================================================================================================
package po;
import java.util.ArrayList;
import java.util.List;
public class Person implements java.io.Serializable {
// Fields
private Integer id;
private String name;
private Integer age;
private List<String> schools=new ArrayList<String>(); //List必须初始化,否则程序运行是抛空指针异常
// Constructors
public Person() {
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return this.age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<String> getSchools() {
return schools;
}
public void setSchools(List<String> schools) {
this.schools = schools;
}
}
===============================================================================
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="po.Person" table="person" catalog="hibernate">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" not-null="true" />
</property>
<property name="age" type="java.lang.Integer">
<column name="age" not-null="true" />
</property>
<list name="schools" table="school">
<key column="person_id" not-null="true"/>
<list-index column="list_order"/>
<element type="string" column="school_name"/>
</list>
</class>
</hibernate-mapping>
============================================================================
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
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);
}
}
============================================================================================
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import po.Person;
public class T {
public static void main(String[] args) {
Person yeeku= new Person();
yeeku.setName("dsfdg");
yeeku.setAge(20);
List<String> schools=new ArrayList<String>();
schools.add("小学");
schools.add("中学");
yeeku.setSchools(schools);
{
Session sess = HibernateUtil.currentSession();
Transaction tx = sess.beginTransaction();
sess.save(yeeku);
tx.commit();
HibernateUtil.closeSession();
}
}
}
运行结果: