Hibernate 组合主键映射、大对象映射、组件映射

xiaoxiao2021-02-28  131

通过组件来实现组合主键的步骤: 这里举个例子,一个表有学生编号,科目编号,成绩,显然学生编号和科目编号的组合才能作为主键


1.编写组合主键的属性的类,该类必须实现Serializable

public class ScoreId implements Serializable { private int stuId;//学生编号 private int subjectId;//科目编号 public int getSubjectId() { return subjectId; } public void setSubjectId(int subjectId) { this.subjectId = subjectId; } public int getStuId() { return stuId; } public void setStuId(int stuId) { this.stuId = stuId; } }

2 .主类

public class Score { private ScoreId scoreId; private double result;//成绩 public ScoreId getScoreId() { return scoreId; } public void setScoreId(ScoreId scoreId) { this.scoreId = scoreId; } public double getResult() { return result; } public void setResult(double result) { this.result = result; } }

3 .映射文件编写

<hibernate-mapping package="cn.siggy.pojo"> <class name="Score"> <composite-id name="scoreId" class="ScoreId"> <key-property name="stuId"></key-property> <key-property name="subjectId"></key-property> </composite-id> <property name="result"></property> </class> </hibernate-mapping>

4 .测试(session对象获得等封装在了HibernateUtil里面,略)

@Test public void testSave() throws HibernateException{ Session session=null; Transaction tx=null; try{ session=HibernateUtil.getSession(); tx=session.beginTransaction(); Score s=new Score(); ScoreId sid=new ScoreId(); sid.setStuId(2); sid.setSubjectId(6); s.setResult(89); s.setScoreId(sid); session.save(s); tx.commit(); } catch(HibernateException e){ if(tx!=null){ tx.rollback(); e.printStackTrace(); throw e; } }finally{ HibernateUtil.closeSession(); } }

大对象映射 在pojo类中使用了大对象,比如Blob类和Clob类。比如Blob我们以前都是通过文件上传来来存储图片,如果我们要把它存放到数据库,那么如下。 1.pojo类

public class Student { private int id; private String name; private int age; //存放大数据 可以存放4G的内容 private Blob image; private Clob introduce; //省略get/set }

2.hbm配置 需要指定大对象的类型

<hibernate-mapping package="cn.siggy.pojo"> <class name="Student"> <id name="id"> <generator class="native"></generator> </id> <property name="name"/> <property name="age"/> <property name="image" type="java.sql.Blob"/> <property name="introduce" type="java.sql.Clob"/> </class> </hibernate-mapping>

3.测试(session对象获得等封装在了HibernateUtil里面,略)

@Test public void testSave() throws HibernateException, SerialException, SQLException{ Session session=null; Transaction tx=null; try{ session=HibernateUtil.getSession(); tx=session.beginTransaction(); Student stu = new Student(); stu.setName("尹志平"); stu.setAge(23); Blob blob = new SerialBlob("ttt".getBytes()); Clob clob = new SerialClob("sss".toCharArray()); stu.setImage(blob); stu.setIntroduce(clob); session.save(stu); tx.commit(); } catch(HibernateException e){ if(tx!=null){ tx.rollback(); e.printStackTrace(); throw e; } }finally{ HibernateUtil.closeSession(); } }

组件是一个被包含的对象 比如这里指一个人有很多个地址,直接把地址值写在一张表的时候,只涉及一张表,没有建立另外一张表和这种表地址id的外键,这个时候用主键。(但是不推荐这种表关系,因为这样维护起来很麻烦)

主类

public class Teacher { private int id; private String name; private String sex; private Address address; //省略get/set }

类Teacher的组件

public class Address { private String addr1; private String addr2; private String addr3; //省略get/set }

Teacher.hbm.xml

<hibernate-mapping package="cn.siggy.pojo"> <class name="Teacher"> <id name="id"> <generator class="native"></generator> </id> <property name="name"/> <property name="sex"/> <!-- 组件 --> <component name="address" class="Address"> <property name="addr1"/> <property name="addr2"/> <property name="addr3"/> </component> </class> </hibernate-mapping>

测试(session对象获得等封装在了HibernateUtil里面,略)

@Test public void testSave() throws HibernateException, SerialException, SQLException{ Session session = null; Transaction tx = null; try{ session = HibernateUtil.getSession(); tx = session.beginTransaction(); Teacher t = new Teacher(); t.setName("老裴"); t.setSex("男"); Address address = new Address(); address.setAddr1("西三旗"); address.setAddr2("西直门"); address.setAddr3("南六环"); t.setAddress(address); session.save(t); tx.commit(); }catch (HibernateException e) { if(tx!=null) tx.rollback(); e.printStackTrace(); throw e; }finally{ HibernateUtil.closeSession(); } }
转载请注明原文地址: https://www.6miu.com/read-22350.html

最新回复(0)