创建实体后,实体类中有一个List类型的字段
package com.keduox.collections; import java.util.List; public class ListUser { private int id; private String name; private List<Integer> likes; 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 List<Integer> getLikes() { return likes; } public void setLikes(List<Integer> likes) { this.likes = likes; } }
HBM文件
<?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> <class name="ListUser" table="t_list_user"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name"/> <!-- 集合的配置 --> <list name="likes" table="list_likes"> <key column="like_id" not-null="true"/> <index column="like_index"></index> <element type="java.lang.Integer"> <column name="like_value"></column> </element> </list> </class> </hibernate-mapping>
测试代码
@Test public void test() { SessionFactory sessionFactory = SessionFactoryUtil.getSessionFactory(); Session session=sessionFactory.openSession(); Transaction tr=session.beginTransaction(); /*ListUser listUser=new ListUser(); listUser.setName("测试"); List<Integer> list=new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.add(4); listUser.setLikes(list); session.save(listUser);*/ ListUser listUser=(ListUser) session.load(ListUser.class, 1); List<Integer> list=new ArrayList<Integer>(); list.add(8); list.add(9); list.add(10); listUser.setLikes(list); session.update(listUser); tr.commit(); }
时间: 2024-12-28 20:15:54