Hibernate 集合属性的操作
来源:互联网 发布:韩国美女直播秀软件 编辑:程序博客网 时间:2024/05/02 00:11
Hibernate对集合属性的操作
Set集合属性的操作
Set结合接口的配置
在*.hbm.xml文件中使用<set>标签
<set>元素用来映射java.util.Set类型的属性,常见的属性和子元素有:
name属性
table属性
<key>子元素
<element>子元素
实例:
entity/StudentSet.java
package entity;import java.io.Serializable;import java.util.Set;/** * Created by lenovo on 2016/3/29. */public class StudentSet implements Serializable{ private int id; private String name; private int age; private Set<String> hobby; public StudentSet() { } public Set<String> getHobby() { return hobby; } public void setHobby(Set<String> hobby) { this.hobby = hobby; } public StudentSet(String name){ this.name=name; } public StudentSet(String name, int age) { this.name = name; this.age = age; } 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; }}entity/StudentSet.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC "//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="entity.StudentSet" table="studentSet"> <id name="id" column="id" > <generator class="native"/> </id> <property name="name" column="name" /> <property name="age" column="age"/> <set name="hobby" table="hobby"> <key column="id"/> <element type="java.lang.String" column="hobby"/> </set> </class></hibernate-mapping>action/handleSet.java
package action;import entity.Student;import entity.StudentSet;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;import org.junit.Test;import java.util.HashSet;import java.util.Set;/** * Created by lenovo on 2016/3/29. */public class HandleSet { @Test public void createTable(){ Configuration configuration=new Configuration().configure(); SchemaExport schemaExport=new SchemaExport(configuration); schemaExport.create(true,true); } @Test public void save(){ Session session=HandleSession.getSession(); Transaction transaction=session.beginTransaction(); try{ StudentSet student=new StudentSet(); student.setName("wo"); student.setAge(22); Set<String> set=new HashSet<>(); set.add("football"); set.add("basketball"); student.setHobby(set); session.save(student); transaction.commit(); }catch (Exception e){ transaction.rollback(); e.printStackTrace(); }finally { session.close(); } } @Test public void get(){ Session session=HandleSession.getSession(); Transaction transaction=session.beginTransaction(); try{ StudentSet student=(StudentSet)session.get(StudentSet.class,1); System.out.println(student.getName()+student.getAge()); System.out.println(student.getHobby()); transaction.commit(); }catch (Exception e){ transaction.rollback(); e.printStackTrace(); }finally { session.close(); } }}对List集合的操作
<list>元素用来映射java.util.List类型的属性,常用的属性和子元素有:
name属性
table属性
<key>子元素
<list-index>子元素
<element>子元素
entity/StudentList.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC "//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="entity.StudentList" table="studentList"> <id name="id" column="id" > <generator class="native"/> </id> <property name="name" column="name" /> <property name="age" column="age"/> <list name="hobby" table="hobby_list"> <key column="id"/> <list-index column="position"/> <element type="java.lang.String" column="hobby"/> </list> </class></hibernate-mapping>
Collection集合属性的操作
<bag>或<ibag>元素用来映射java.util.Collection类型的属性,常用的属性和子元素有:
name属性
table属性
<key>子元素
<collection-id>子元素
<element>子元素
StudentCollection.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC "//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="entity.StudentCollection" table="studentCollection"> <id name="id" column="id" > <generator class="native"/> </id> <property name="name" column="name" /> <property name="age" column="age"/> <list name="hobby" table="hobby_collection"> <key column="id"/> <element type="java.lang.String" column="hobby"/> </list> </class></hibernate-mapping>
Map集合属性的操作
<map>元素用来映射java.util.Map类型的属性,常用的属性和子元素有:
name属性
table属性
<key>子元素
<map-key>子元素
<element>子元素
<!DOCTYPE hibernate-mapping PUBLIC "//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="entity.StudentMap" table="studentMap"> <id name="id" column="id" > <generator class="native"/> </id> <property name="name" column="name" /> <property name="age" column="age"/> <map name="hobby" table="hobby_map"> <key column="id"/> <element type="java.lang.String" column="hobby"/> <map-key type="java.lang.String" column="map-key"/> </map> </class></hibernate-mapping>
0 0
- Hibernate 集合属性的操作
- Hibernate对集合属性的操作
- hibernate的集合属性
- hibernate的集合属性 .
- ssh学习:hibernate中 集合属性的操作
- Hibernate对集合属性的操作之Set集合篇 6
- hibernate的集合映射的一些属性
- Hibernate List 集合属性的配置
- Hibernate 集合属性的延迟加载
- Hibernate集合属性的延迟加载
- Hibernate获得实体类的属性集合
- hibernate属性集合
- hibernate映射集合属性
- Hibernate集合属性映射
- Hibernate集合属性配置
- Hibernate映射集合属性
- 【Hibernate】Hibernate的集合映射与sort、order-by属性
- 【Hibernate】Hibernate的集合映射与sort、order-by属性
- Linux入门:VIM
- 动态规划之寻找两个字符串最长公共子串
- hdu 5652 bestcoder 77 div2 2
- Android 6.0 运行时权限处理
- 贪心1008
- Hibernate 集合属性的操作
- spark HA
- hibernate 数据库 方言配置在哪个jar包的什么位置?
- 将HashMap,HashSet写入文本的工具类
- 自动异机(异地)备份
- word中实现章节标题的自动编号
- 深入探索Java 8 Lambda表达式
- 了解基于Token的身份验证的来龙去脉
- live555断流