Hibernate entity Type mapping file

Source: Internet
Author: User

1. Write the hibernate mapping file. Entity types are converted to tables in the database

Written according to the entity type.

Entity type User.java

Package Cn.wwh.www.hibernate.dd.property;import java.util.arrays;import java.util.date;/** * Class Function: * * * @author Skiff *@ Version 1.0 *@ created: 2014-8-17 pm 08:05:30 */public class User {private Integer id;private String name;//Name Private Boole an gender; True indicates a male. False indicates the female private Date birthday; Birthday Private String desc; A large paragraph description, up to 5000 words private byte[] photo; Photo/** * @return The ID */public Integer getId () {return ID;} /** * @param id The ID to set */public void SetId (Integer id) {this.id = ID;} /** * @return The name */public String getName () {return name;} /** * @param name the name to set */public void SetName (String name) {this.name = name;} /** * @return The Gender */public Boolean Isgender () {return gender;} /** * @param gender the gender to set */public void Setgender (Boolean gender) {This.gender = gender;} /** * @return The birthday */public Date getbirthday () {return birthday;} /** * @param birthday The birthday to set */public void Setbirthday (Date birthday) {this.birthday = Birthday;} /** * @return The desc */public String GetDesc () {return desc;} /** * @param desc the desc to set */public void Setdesc (String desc) {this.desc = desc;} /** * @return The photo */public byte[] Getphoto () {return photo;} /** * @param photo The photo to set */public void Setphoto (byte[] photo) {This.photo = photo;} @Overridepublic String toString () {return "User [birthday=" + birthday + "\ n desc=" + desc + "\ n gender=" + gender + "\ n ID = "+ id +" \ n name= "+ name +" \ n photo= "+ arrays.tostring (photo) +"] ";}}

2. mapping file User.hbm.xml:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.sourceforge. Net/hibernate-mapping-3.0.dtd ">
3. master configuration file:
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 ">

                        Create-drop: Create a table at startup and delete the table before exiting.

<span style= "White-space:pre" ></span> update: If the table structure is inconsistent, create or update it. Validate: Validates the table structure at startup, assuming that the exception is not thrown.       --><!--own initiative to create a data table--><property name= "Hbm2ddl.auto" >update</property><!--SQL statement showing data manipulation --><property name= "Show_sql" >true</property><!--formatted display SQL statement--><property name= "Format_sql ">true</property><!--3. Import the mapping profile--><mapping resource=" cn/wwh/www/hibernate/dd/property/ User.hbm.xml "/></session-factory>

4. Data in the test database:

Package Cn.wwh.www.hibernate.dd.property;import Java.io.fileinputstream;import Java.util.date;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;import org.junit.test;/** * Function of class: * * * @author Skiff * @version 1.0 *@ created: 2014-8-17 pm 08:10:38 */public class Testsqlbyxml {Priv Ate static sessionfactory sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();//Save @testpublic Void Testsave () throws Exception {Session session = Sessionfactory.opensession (); session.begintransaction ();//----------- ----------------------------//Read the picture from the hard drive. Then store the database fileinputstream in = new FileInputStream ("f:/psb.jpg"); byte[] photo = new Byte[in.available ()];in.read (photo); In.close ();//Prepare the object User user = new user (); User.setname ("skiff"); User.setgender (true); User.setbirthday (new Date ()); User.setdesc ("Skiff is a positive child, striving for the dream ..."); User.setphoto (photo);//save Session.save (user);//------------------- --------------------session.gettransaction (). commit (); sEssion.close ();} Get @testpublic void Testget () throws Exception {Session session = Sessionfactory.opensession (); Session.begintransaction ();//---------------------------------------User user = (user) Session.get (user.class, 1); System.out.println (User.getname ()); System.out.println (User.isgender ()); System.out.println (User.getbirthday ()); System.out.println (User.getdesc ()); System.out.println (User.getphoto ());//system.out.println (user);//--------------------------------------- Session.gettransaction (). commit (); Session.close ();}}



Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Hibernate entity Type mapping file

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.