Hibernate 中 联合主键映射 组合关系映射 大对象映射(或者说文本大对象,二进制数据大对象)

Clob:文本大对象,最长4G

Blob:二进制数据大对象,最长4G

util:

public class HibUtil {
  private static SessionFactory sessionFactory;
  static{
    //获取配置信息 hibernate.cfg.xml
    Configuration configuration = new Configuration().configure();
    //创建一个 ServiceRegistry的实例
    //首先获得其标准建造器,此处用了建造者模式 builder模式来创建对象
    //创建一个标准的构建器
    StandardServiceRegistryBuilder ssb = new StandardServiceRegistryBuilder();
    //建造(得到)ServiceRegistry的实例此处一定要传入//configuration.getProperties()否则会报错UnsupportedOperationException
    ServiceRegistry build = ssb.applySettings(configuration.getProperties()).build();
    //获得session工厂 一个数据库对应一个 SessionFactory
    sessionFactory = configuration.buildSessionFactory(build);
  }
  public static Session getSession(){
    return sessionFactory.openSession();
  }
  public static void main(String[] args) {
  }
}

hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
  <!-- 1:链接数据库的配置 -->
  <property name="hibernate.dialect">
    org.hibernate.dialect.Oracle10gDialect
  </property>
  <property name="hibernate.connection.driver_class">
    oracle.jdbc.driver.OracleDriver
  </property>
  <property name="hibernate.connection.username">scott</property>
  <property name="hibernate.connection.password">tiger</property>
  <property name="hibernate.connection.url">
    jdbc:oracle:thin:@localhost:1521:orcl
  </property>
  <!-- 2: 全局性配置 -->
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.format_sql">true</property>
  <!-- 仅供测试时使用 ,公司内禁止使用 -->
  <property name="hibernate.hbm2ddl.auto">update</property>
  <!-- 3: 映射文件的配置 -->
  <mapping resource="com/huawei/hib/po/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>

po:

public class Address {
  private Integer addrId;
  private String addrName;
  public Integer getAddrId() {
    return addrId;
  }
  public void setAddrId(Integer addrId) {
    this.addrId = addrId;
  }
  public String getAddrName() {
    return addrName;
  }
  public void setAddrName(String addrName) {
    this.addrName = addrName;
  }
}

public class PersonName {
  private String firstName;
  private String lastName;
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
}

import java.sql.Blob;
import java.sql.Clob;

public class Person {
  private PersonName personName;
  private Integer age;
  private Clob txtObj;
  private Blob imgObj;
  private Address address;
  public PersonName getPersonName() {
    return personName;
  }
  public void setPersonName(PersonName personName) {
    this.personName = personName;
  }
  public Integer getAge() {
    return age;
  }
  public void setAge(Integer age) {
    this.age = age;
  }
  public Clob getTxtObj() {
    return txtObj;
  }
  public void setTxtObj(Clob txtObj) {
    this.txtObj = txtObj;
  }
  public Blob getImgObj() {
    return imgObj;
  }
  public void setImgObj(Blob imgObj) {
    this.imgObj = imgObj;
  }
  public Address getAddress() {
    return address;
  }
  public void setAddress(Address address) {
    this.address = address;
  }
}

Person.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
  package="com.huawei.hib.po">
  <class name="Person" table="test_person" >
    <!-- 联合主键映射 -->
    <composite-id name="personName" class="PersonName">
      <key-property name="firstName" column="first_name" />
      <key-property name="lastName" column="last_name" />
    </composite-id>
    <property name="age" />
    <property name="txtObj" column="txt_Obj" />
    <property name="imgObj" column="img_Obj" />
    <!--组合关系映射 -->
    <component name="address" class="Address">
      <property name="addrId" column="addr_id" />
      <property name="addrName" column="addr_name" />
    </component>
  </class>
</hibernate-mapping>

dao:

public class PersonDao {
  public static void main(String[] args) throws FileNotFoundException {
    Session session = HibUtil.getSession();
    Address address = new Address();
    address.setAddrId(1);
    address.setAddrName("成都");
    PersonName personName = new PersonName();
    personName.setFirstName("三");
    personName.setLastName("张");
    Person p = new Person();
    p.setPersonName(personName);
    p.setAge(20);
    Blob imgObj = Hibernate.getLobCreator(session).createBlob(new FileInputStream("E:/123.jpg"),1200000);
    Clob txtObj = Hibernate.getLobCreator(session).createClob("成都是个美丽的地方");
    p.setTxtObj(txtObj);
    p.setImgObj(imgObj);
    p.setAddress(address);
    Transaction tx = session.beginTransaction();
    session.save(p);
    tx.commit();
    session.close();
  }
}

上一篇:thinkphp传送文章id值


下一篇:hibernate 联合主键生成机制(组合主键XML配置方式)