集合映射
public class CollectionMapping { private int id; private String name; private Set setValue; private List listValue; private String[] arrayValue; private Map mapValue; }
<class name="CollectionMapping" table="CollectionMapping"> <id name="id" type="integer"> <generator class="increment" /> </id> <property name="name"/> <set name="setValue" > <key column="set_id" /> <element type="string" column="set_value" /> </set> <list name="listValue"> <key column="list_id" /> <list-index column="list_index"/> <element type="string" column="list_value"/> </list> <array name="arrayValue"> <key column="array_id" /> <list-index column="array_index"/> <element type="string" column="array_value" /> </array> <map name="mapValue"> <key column="map_id" /> <map-key type="string" column="map_key" /> <element type="string" column="map_value" /> </map> </class>
public void testSave1() { Session session = null; CollectionMapping c = new CollectionMapping(); c.setName("xxx"); Set setValue = new HashSet(); setValue.add("a"); setValue.add("b"); c.setSetValue(setValue); List listValue = new ArrayList(); listValue.add("c"); listValue.add("d"); c.setListValue(listValue); String[] arrayValue = new String[]{"e", "f"}; c.setArrayValue(arrayValue); Map mapValue = new HashMap(); mapValue.put("k1", "v1"); mapValue.put("k2", "v2"); c.setMapValue(mapValue); try { session = HibernateSessionFactory.getSession(); session.beginTransaction(); session.save(c); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateSessionFactory.closeSession(session); } }
组件映射
定义一个Contact类,User类,通过配置User映射文件
public class Contact { private String address; private String telephone; private String zipCode; private String email; }
public class User { private int id; private String name; private Contact contact; }
<class name="mapping.User" table="t_user"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <component name="contact"> <property name="email"/> <property name="address"/> <property name="telephone"/> <property name="zipCode"/> </component> </class>
public static void main(String[] args) { Session session = null; try { session = HibernateSessionFactory.getSession(); session.beginTransaction(); User user = new User(); user.setName("xxxxx"); Contact contact = new Contact(); contact.setEmail("[email protected]"); contact.setAddress("china"); contact.setZipCode("24237483274"); contact.setTelephone("13546546888"); user.setContact(contact); session.save(user); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally{ HibernateSessionFactory.closeSession(session); } }
复合(联合)主键映射
通常将复合主键相关的属性,单独放到一个类中
* 此类必须实现序列化接口
* 覆写hashcode和equals方法
主要一个实现序列号接口的类,主类,映射文件
public class FiscalYearPeriodPK implements Serializable { private static final long serialVersionUID = 9033183366102420040L; //核算年 private int fiscalYear; //核算月 private int fiscalPeriod; public int getFiscalPeriod() { return fiscalPeriod; } public void setFiscalPeriod(int fiscalPeriod) { this.fiscalPeriod = fiscalPeriod; } public int getFiscalYear() { return fiscalYear; } public void setFiscalYear(int fiscalYear) { this.fiscalYear = fiscalYear; } @Override public int hashCode() { final int PRIME = 31; int result = 1; result = PRIME * result + fiscalPeriod; result = PRIME * result + fiscalYear; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final FiscalYearPeriodPK other = (FiscalYearPeriodPK) obj; if (fiscalPeriod != other.fiscalPeriod) return false; if (fiscalYear != other.fiscalYear) return false; return true; } }
public class FiscalYearPeriod{ private FiscalYearPeriodPK fiscalYearPeriodPK; // 开始日期 private Date beginDate; // 结束日期 private Date endDate; // 状态 private String periodSts; }public void testSave1() { Session session = null; Random ran = new Random(); try { session = HibernateSessionFactory.getSession(); session.beginTransaction(); for(int i=10;i<1000;i++){ FiscalYearPeriod fiscalYearPeriod = new FiscalYearPeriod(); FiscalYearPeriodPK pk = new FiscalYearPeriodPK(); pk.setFiscalYear(ran.nextInt(i)); pk.setFiscalPeriod(ran.nextInt(i*1000)); fiscalYearPeriod.setFiscalYearPeriodPK(pk); fiscalYearPeriod.setBeginDate(new Date()); fiscalYearPeriod.setEndDate(new Date()); fiscalYearPeriod.setPeriodSts("Y"); session.save(fiscalYearPeriod); } session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateSessionFactory.closeSession(session); } }