A combined primary key mapping in Hibernate large object mapping (or large object with binary data)

Source: Internet
Author: User

Clob: Text large object, maximum 4G

BLOB: Binary data large object, maximum 4G

Util

public class Hibutil {
private static Sessionfactory sessionfactory;
static{
Get configuration information Hibernate.cfg.xml
Configuration configuration = new configuration (). Configure ();
Create an instance of a Serviceregistry
First get its standard builder, where you create objects using the Builder Mode builders mode
To create a standard builder
Standardserviceregistrybuilder SSB = new Standardserviceregistrybuilder ();
Build (Get) Serviceregistry instance here must pass in//configuration.getproperties () or it will be an error unsupportedoperationexception
Serviceregistry build = Ssb.applysettings (Configuration.getproperties ()). build ();
Get session factory a database corresponding to a 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" >
<session-factory>
<!--1: Configuration of Linked databases--
<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: Global Configuration--
<property name= "Hibernate.show_sql" >true</property>
<property name= "Hibernate.format_sql" >true</property>
<!--used for testing only and not allowed in the company
<property name= "Hibernate.hbm2ddl.auto" >update</property>
<!--3: Configuration of the mapping file--
<mapping resource= "Com/huawei/hib/po/person.hbm.xml"/>
</session-factory>

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" >
Package= "Com.huawei.hib.po" >
<class name= "Person" table= "Test_person" >
<!--Federated PRIMARY KEY mapping--
<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"/>
<!--a composite relationship map--
<component name= "Address" class= "Address" >
<property name= "Addrid" column= "addr_id"/>
<property name= "Addrname" column= "Addr_name"/>
</component>
</class>

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 ("Chengdu");
PersonName personname = new PersonName ();
Personname.setfirstname ("three");
Personname.setlastname ("Zhang");
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 ("Chengdu is a beautiful place");
P.settxtobj (Txtobj);
P.setimgobj (imgobj);
P.setaddress (address);
Transaction tx = Session.begintransaction ();
Session.save (P);
Tx.commit ();
Session.close ();
}
}

A combined primary key mapping in Hibernate large object mapping (or large object with binary data)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.