Hibernate entity type to map file

Source: Internet
Author: User
Tags generator

1. Convert an entity type to a table in a database by writing hibernate mapping files

One of the mapping files is 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 male, false indicates 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: Created or updated if the table structure is inconsistent. Validate: Validates the table structure at startup if it does not throw an exception.       --><!--automatically create data tables--><property name= "Hbm2ddl.auto" >update</property><!--SQL statements that show 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. Test the data in the 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 ();//----------- ----------------------------//reads the picture from the hard disk and then stores 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 ();}}



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.