【hibernate】映射集合属性list

本文介绍了一个使用Hibernate实现的人员及其所在学校的数据库关系映射示例,包括实体类定义、映射文件配置及简单的增删改查操作。

代码组织:


数据库:一个人可以再多个学校学习



======================================================================================================================


package po;


import java.util.ArrayList;
import java.util.List;


/**
 * Person entity. @author MyEclipse Persistence Tools
 */


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


/** default constructor */
public Person() {
}


/** full constructor */
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 {


/**
* @param args
*/
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();
}


}


}


运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值