1. Project structure
2. Three persistence classes
①pay.java
Package Com.baidu.cfghbm;public class Pay {//salary private integer monthlypay;//annual salary private integer yearpay;//paid false private Integer vocationwithpay;//private worker worker;public worker Getworker () {return worker;} public void Setworker (worker worker) {this.worker = worker;} Public Integer Getmonthlypay () {return monthlypay;} public void Setmonthlypay (Integer monthlypay) {this.monthlypay = Monthlypay;} Public Integer Getyearpay () {return yearpay;} public void Setyearpay (Integer yearpay) {this.yearpay = Yearpay;} Public Integer Getvocationwithpay () {return vocationwithpay;} public void Setvocationwithpay (Integer vocationwithpay) {this.vocationwithpay = Vocationwithpay;}}
②person.java
Package Com.baidu.cfghbm;import Java.sql.blob;import Java.util.date;public class Person {private Integer id;private String Name;private string Shiyan;public string Getshiyan () {return Shiyan;} public void Setshiyan (String shiyan) {This.shiyan = Shiyan;} private string Interest;private Date birth;//The value of this property is: name:birthprivate string desc;//Map long text private String content;// Maps the binary private Blob image;public person () {super ();} Public person (string name, string interest, Date birth) {super (); this.name = Name;this.interest = Interest;this.birth = Bi Rth;} Public String getcontent () {return content;} public void SetContent (String content) {this.content = content;} Public Blob GetImage () {return image;} public void SetImage (Blob image) {this.image = image;} Public String GetDesc () {return desc;} public void Setdesc (String desc) {this.desc = desc;} Public Integer GetId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = NAMe;} Public String Getinterest () {return interest;} public void Setinterest (String interest) {this.interest = interest;} Public Date Getbirth () {return birth;} public void Setbirth (Date birth) {This.birth = birth;}}
③worker.java
Package Com.baidu.cfghbm;public class Worker {private Integer id;private String name;private pay Pay;public Integer getId ( ) {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public pay Getpay () {return pay;} public void SetPay (pay pay) {This.pay = pay;}}
3. Two bhm.xml mapping files
① 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 "><!--Generated 2016-5-17 14:39:37 by Hibernate Tools 3.4.0.CR1-->< Hibernate-mapping package= "COM.BAIDU.CFGHBM" > <class name= "person" table= "person" dynamic-update= "true" > <id name= "id" type= "java.lang.Integer" > <column name= "id"/> <generator class= "Native"/> </id> <property name= "name" type= "java.lang.String" column= "name" Length= "30 "> </property> <property name=" Shiyan "type=" java.lang.String "unique=" true " Update= "false" index= "Person_index" length= "> <!--name: Specifies the name of the property of the persisted class column: Specifies the field of the table with the class's property mapping Name. If the property is not set, Hibernate uses the class's property name directly as the field name. Type: Specifies the Hibernate mapping type. The Hibernate mapping type is a bridge between Java type and SQL type. If the mapping type is not explicitly set for an attribute, Hibernate will be shippedThe reflection mechanism identifies the Java type of a specific property of the persisted class, and then automatically uses the default Hibernate mapping type that corresponds to it. Unique: Sets whether to add a unique constraint for the data column that is mapped by the property. Access: Specifies the default property access policy for Hibernate. The default value is property, which is to use getter, setter methods to access properties. If you specify field, Hibernate ignores the Getter/setter method and accesses the member variable not-null by reflection: If the property value is true, it indicates that NULL is not allowed and the default is False update: set To false, which means that it is not allowed to be modified, and true indicates that it can be modified by index: Specifies the name of a string. When the system needs Hibernate to automatically build the table, it is used to create an index for the data column that the property maps, thereby speeding up the query for that data column. Scale: Specifies the number of decimal digits of the data column that the property is mapped to, and is valid for data columns of type double, float, decimal, and so on. Formula: Sets an SQL expression that Hibernate will use to calculate the value of the derived property. Derived property: Not all properties of a persisted class are directly matched to the field of the table, and the values of some properties of the persisted class must be computed at run time, which is called the derived property when using the Formula property, the Formula= "(SQL)" Parenthesis does not The column names and table names in the SQL expression should be the same as the database, not the properties of the persisted object if you need to use parameters in the Formula property, this directly uses the where Cur.id=id form, where the ID is the parameter, and the column that corresponds to the ID property of the current persisted object. The ID value is passed in as a parameter. Length: Specifies the small detail of the field of the data column to which the property is mapped: If you want the map file to be created in the database with a value of length set, you must configure the Package column property to the properties, otherwise the length setting will not succeed. Examples are as follows: <property name= "Shiyan" type= "java.lang.String" column= "Shi_yan" length= "" ></PROPERTY&G "T --<column name= "Shi_yan" ></column> </property> <property name= "Intere St "type=" java.lang.String "> <column name=" INTEREST "/> </property> <pr Operty name= "Birth" type= "Time" index= "Person_index" > <column name= "Birth"/> </property> <!--Map Dispatch Properties--<property name= "desc" formula= "(select concat (Name, ': ', birth) from the person P WHERE p.id = ID) "></property> <!--Map Large object <property name=" content "type=" Clob ">& Lt;/property> the reflection below is precisely mapped content type--<property name= "Content" > <column name= "CONTENT" sql-type= "Mediumtext" ></column> </property> <!--mapping binary <property nam E= "image" Type= "blob" ></property> the following way is the exact mapping of the type of image--<property name= "image" type= "blob "> <column name= "IMAGE" sql-type= "Mediumblob"/> </property> </class>
Ii. Worker.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 ">
4. Hibernate.cfg.xml<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
~~~~~~~~~~~~~~~~~~~~~~~~~ Split Line ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Package Com.baidu.cfghbm;import Java.io.ioexception;import Java.io.inputstream;import java.sql.Blob;import Java.sql.connection;import Java.sql.sqlexception;import Java.util.date;import Org.hibernate.Session;import Org.hibernate.sessionfactory;import Org.hibernate.transaction;import Org.hibernate.cfg.configuration;import Org.hibernate.jdbc.work;import Org.hibernate.service.serviceregistry;import Org.hibernate.service.serviceregistrybuilder;import Org.junit.after;import Org.junit.before;import Org.junit.Test ;p Ublic class Testcfg {private sessionfactory sessionfactory;private Session session;private Transaction transaction;@ beforepublic void init () {Configuration configuration = new configuration (). Configure (); Serviceregistry serviceregistry = new Serviceregistrybuilder (). Applysettings (Configuration.getproperties ()). Buildserviceregistry (); sessionfactory = Configuration.buildsessionfactory (serviceregistry); session = Sessionfactory.opensession (); transaction = Session.begintransaction ();} @AfTerpublic void Destroy () {transaction.commit (); Session.close (); Sessionfactory.close ();} @Testpublic void Testcomponent () {Worker worker = new Worker (); Pay pay = new pay ();p Ay.setmonthlypay (10000);p Ay.setyearpay (150000);p Ay.setvocationwithpay (5); Worker.setname (" Shoping "); Worker.setpay (pay); Session.save (worker);} /** * Read/write to BLOB * * */@Testpublic void Testblob () throws IOException, sqlexception{//save blob object//person person = new Pers On ();//person.setname ("AA");//person.setinterest ("Sport");; /person.setbirth (New Date ());; /person.setdesc ("DESC");//person.setcontent ("I Love java! ////inputstream stream = new FileInputStream ("image"). PNG ");//blob image = Hibernate.getlobcreator (session)//. Createblob (Stream, stream.available ());//person.setimage ( image);; Session.save (person);//Get Blob object person person = (person) session.get (Person.class, 1); Blob image = Person.getimage (); InputStream in = Image.getbinarystream (); System.out.println (In.available ());} @Testpublic void Testpropertyupdate () {person person = (person) session.get (Person.class, 1);p erson.setname ("Tom"); System.out.println (Person.getbirth ());} @Testpublic void Testidgenerator () throws Interruptedexception{person person = new Person ("Tom", "Music", New Date ()), SES Sion.save (person);//thread.sleep (5000);} @Testpublic void Testdynamicupdate () {Person person = (person) session.get (Person.class, 1);p erson.setname ("Wangxiang" );p Erson.setbirth (New Date ());} @Testpublic void Testdowork () {session.dowork (new work () {@Overridepublic void execute (Connection Connection) throws SQLException {System.out.println (connection);}});}}
How to manipulate BLOB large objects, map dispatch properties, map composition relationships