hibernate集合映射实例

xiaoxiao2025-12-05  4

<?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"> <hibernate-mapping package="com.lifeng.po.other"> <class name="Other" schema="dbo" catalog="pubs"> <id name="id"> <generator class="identity" /> </id> <property name="name" /> <set name="set" table="t_set" schema="dbo" catalog="pubs"> <key column="set_id" /> <element column="set_valuse" type="string" /> </set> <list name="list" table="t_list" schema="dbo" catalog="pubs"> <key column="list_id" /> <list-index column="list_index" /> <element type="string" column="list_values" /> </list> <array name="s" table="t_s" schema="dbo" catalog="pubs"> <key column="s_id" /> <list-index column="s_index" /> <element type="string" column="s_values" /> </array> <map name="map" table="t_map" schema="dbo" catalog="pubs"> <key column="map_id" /> <map-key column="map_key" type="string" /> <element column="map_values" type="string" /> </map> </class> </hibernate-mapping> --------------------------------使用此类添加表-------------------------- import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class HibernateMian { public static void main(String[] args) { try { Configuration cfg = new Configuration().configure(); SchemaExport se = new SchemaExport(cfg); se.create(true, true); } catch (Exception e) { e.printStackTrace(); } } -------------------------------对应类---------------------------------- import java.util.List; import java.util.Map; import java.util.Set; public class Other { public Other(){} private int id; private String name; private Set set; private List list; private String[] s; private Map map; 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 Set getSet() { return set; } public void setSet(Set set) { this.set = set; } public List getList() { return list; } public void setList(List list) { this.list = list; } public String[] getS() { return s; } public void setS(String[] s) { this.s = s; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } } ----------------------------------测试类------------------------------- import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import org.hibernate.Session; import com.lifeng.mian.HibernatePublic; import com.lifeng.po.other.Other; public class OtherTest { public static void main(String[] args) { // new OtherTest().save(); new OtherTest().query(); } public void save() { Session session = null; Other other = new Other(); List list = new ArrayList(); list.add("lifeng"); Map map = new HashMap(); map.put("1", "admin"); Set set = new HashSet(); set.add("hello"); String[] a = { "a", "b" }; other.setList(list); other.setMap(map); other.setName("system"); other.setS(a); other.setSet(set); try { session = HibernatePublic.getSession(); session.beginTransaction().begin(); session.save(other); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } finally { HibernatePublic.getClose(session); } } public void query() { Session session = null; try { session = HibernatePublic.getSession(); session.beginTransaction().begin(); Other other = (Other) session.load(Other.class, 3); System.out.println("名字是:" + other.getName()); String[] s = other.getS(); System.out.println(" String数组是:" + s); for (int i = 0; i < s.length; i++) { System.out.println("---------------------" + s[i] + "-------------------"); } Set set = other.getSet(); System.out.println("Set是:" + set); for (Iterator iterator = set.iterator(); iterator.hasNext();) { Object object = (Object) iterator.next(); System.out.println("----------------------" + object + "------------------------"); } List list = other.getList(); System.out.println("list是:" + list); for (Object object : list) { System.out.println("--------------------------" + object + "-----------------------"); } Map map = other.getMap(); System.out.println("map是:" + map); for (Object key : map.keySet()) { System.out.println("--------" + map.get(key) + "-----------------" + key); } session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } finally { HibernatePublic.getClose(session); } } } 其他就是hibernate的基本配置了,配置好了先运行HibernateMian 类 在运行测试类。在测试中先得到SessionFactory import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernatePublic { private static SessionFactory factory; static { try { Configuration cfg = new Configuration().configure(); factory = cfg.buildSessionFactory(); } catch (Exception e) { e.printStackTrace(); } } public static Session getSession(){ return factory.openSession(); } public static void getClose(Session session){ if(session!=null){ if(session.isOpen()){ session.close(); } } } } 此类就可以得到SessionFactory
转载请注明原文地址: https://www.6miu.com/read-5040359.html

最新回复(0)