Hibernaate Set集合的使用

package model;

import java.util.Set;

public class Student {

	private int id;
	private String name;
	private int age;
	private Set<String> hobby;
	
	public Student(){
		
	}

	public int getId() {
		return id;
	}

	public void setId(int 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 Set<String> getHobby() {
		return hobby;
	}

	public void setHobby(Set<String> hobby) {
		this.hobby = hobby;
	}
	
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<!-- name属性指定类名(全限定名) table指明表名,不指明table数据默认的表名和实体名一致 -->
    <class name="model.Student" table="stu_tab">
    	<!-- type指明当前字段的类型    name对应实体中的属性名 -->
        <id type="integer" name="id">
        	<!-- 提供ID自增的策略  native会根据数据库自行判断 -->
            <generator class="native"/>
        </id>
        <property name="name" type="string"></property>
        <property name="age" type="integer"></property>
        <!-- set 标签中name表示的是实体类中集合的属性名称 -->
        <set name="hobby" table="stu_hobby" lazy="false">
        	<!-- key子元素指明当前表的外键列 -->
        	<key column="stu_id" foreign-key="stu_hobby_fk"></key>
        	<!-- element元素是用来保存集合属性中的值 -->
        	<element column="hobby_name" type="string"></element>
        </set>
    </class>
		
</hibernate-mapping>
<!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.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
		<!-- 数据库驱动 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///test</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		 <property name="hibernate.show_sql">true</property>
		 <!-- 其它属性配置 -->
		<!-- 指明C3P0的提供者 -->
		<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		<!-- 连接池参数的配置 -->
		<property name="hibernate.c3p0.min_size">5</property> 
	    <property name="hibernate.c3p0.max_size">30</property> 
	    <property name="hibernate.c3p0.timeout">1800</property> 
	    <property name="hibernate.c3p0.max_statements">50</property>
	    
	    <!-- 打印SQL语句到控制台 -->
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">update</property> 
		<!-- 注册实体的对象关系映射文件 -->
		
		<mapping resource="model/Student.hbm.xml"/>
	</session-factory>
</hibernate-configuration>
package test;

import java.util.HashSet;
import java.util.Set;

import model.Student;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

import util.HibernateUtils;

public class StudentTest {

	@Test
	public void createTable(){
		Configuration cfg=new Configuration().configure();
		SchemaExport se=new SchemaExport(cfg);
		se.create(true, true);
	}
	@Test
	public void save(){
		Session session=HibernateUtils.getSession();
		Transaction tx=session.beginTransaction();
		
		Student stu=new Student();
		
		stu.setName("zhangsan");
		stu.setAge(20);
		
		Set<String>set=new HashSet<>();
		set.add("eat");
		set.add("sleep");
		set.add("play");
		
		stu.setHobby(set);
		session.save(stu);
		tx.commit();
		HibernateUtils.close(session);
	}
	
	@Test
	public void get(){
		Session session=HibernateUtils.getSession();
		Transaction tx=session.beginTransaction();
		
		Student stu=(Student)session.get(Student.class, 1);
		System.out.println(stu.getId()+"--->"+stu.getName()+"--->"+stu.getAge());
	//	for(String str:stu.getHobby()){
	//		System.out.println(str);
	//	}
		tx.commit();
		HibernateUtils.close(session);
	}
}


转载于:https://my.oschina.net/u/2356966/blog/466418

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值