"Hibernate" (3) Hibernate single-table operation

Source: Internet
Author: User

1. Single PRIMARY key

This is configured in the Student.hbm.xml that we generated:

We configured the Hbm2ddl.auto to update

<property name= "Hbm2ddl.auto" >update</property>
(1). Assigned: Generated by Java application (manually assigned value)

The manual assignment is the way we wrote it before:

Student s = new Student () S.setname ("John Doe"); S.setgender ("male"); S.setbirthday (new Date ()); S.setaddress ("Beijing"); Session.save (s); Saving objects into the database

This method executes the first time is successful, the ID automatically gives the value 0, but executes the second time the error, should be the ID default or 0, will be the primary key conflict.

(2). Native: The identifier is automatically generated by the underlying database, if MySQL is increment, if Oracle is sequence, etc.

Using native, if the pail top ID, it will default to start from 1 automatically increase, so when the execution of two times after the addition of data, will not error, and the ID is 1, 2, 3 such sub-line. Even if you manually specify the value of the ID, it does not work, and the values that are inserted into the database are added in the original increment order.


2. Basic Types



In the Student.hbm.xml file

<property name= "Birthday" type= "java.util.Date" ><column name= "Birthday"/></property>

Java.util.Date is a format in Java that includes date and time.

Type if date is the format of date only.

Type if time is a format that is only seconds and minutes.

Type timestamp is the format that contains the date and time of the year.

Date, time, and timestamp are the data types for hibernate.

3. Object Type


MySQL does not support the CLOB type of standard SQL, in MySQL, the text, Mediumtext, and longtext types to represent long text data of more than 255 length.

Add a new property to the previous student class:

private Blob picture;

Note that the Java.sql.Blob class needs to be imported here

then we need to delete our previous Student.hbm.xml file, regenerate it again, and write a method to write to the database:

@Testpublic void Testwriteblob () throws Exception {Student s = new Student (1, "Zhang San", "male", New Date (), "Xi ' an");//Get photo file first f = new File ("F:" + File.separator + "qtww.jpg"); InputStream is = new FileInputStream (f); Blob image = Hibernate.getlobcreator (session). Createblob (Is,is.available ()); S.setpicture (image); Session.save (s);}
then write a method to read the image file to the Local:

@Testpublic void Testreadblob () throws Exception {//Here the second parameter is the student's primary key Student s = (Student) session.get (Student.class, 1); Blob image = S.getpicture (); InputStream is = Image.getbinarystream (); File F = new file ("D:/head.jpg"); F.createnewfile (); OutputStream os = new FileOutputStream (f); byte[] Buff = new Byte[is.ava Ilable ()];is.read (buff); os.write (buff); Is.close (); Os.close ();}

4. Component Properties

An attribute in an entity class belongs to an object of a user-defined class.

Add a new Address entity class:

public class Address {private string Postcode;private string Phone;private string address;}
Generating construction methods, get and set methods

Modify the student class to include this attribute.

Modify the Student.hbm.xml file, delete the original address attribute, add in the <class> tag:

<component name= "Address" class= "com.thr.bean.Address" ><property name= "postcode" column= "postcode" > </property><property name= "Phone" column= "phone" ></property><property name= "Address" column= " ADDRESS "></property></component>
To modify the test code:

@Testpublic void Testsavestudent () {//Generate Student object//Student s = new Student (1, "Zhang San", "male", New Date (), "Xian"); Student s = new Student (); S.setid (+) S.setname ("John Doe"); S.setgender ("male"); S.setbirthday (new Date ());//S.setaddress (" Beijing "); Address address = new Address ("710065", "0298856445", "Xian City"); s.setaddress (address); Session.save (s); Save object into Database}
After running, we found that in the database, 3 new varchar fields were added to store the entity class address we just created.


5. Single-table Crup operation Example

The first step is to modify the policy to update.

@Testpublic void Testgetstudent () {Student s = (Student) session.get (Student.class, 1000); System.out.println (s);} @Testpublic void Testloadstudent () {Student s = (Student) session.load (Student.class, 1000); System.out.println (s);} @Testpublic void Testupdatestudent () {Student s = (Student) session.get (Student.class,); S.setgender ("female"); System.out.println (s);} @Testpublic void Testdeletestudent () {Student s = (Student) session.get (Student.class, n); Session.delete (s); System.out.println (s);}
The difference between get and load:

(1). Without regard to caching, the Get method issues an SQL statement to the database immediately after the call, returning the persisted object. The Load method returns a proxy object after the call, which only holds the ID of the entity object until the SQL statement is issued when the object's non-primary key attribute is used.

(2). When querying data that does not exist in the database, the Get method returns the Null,load method to throw an exception org.hibernate.ObjectNotFoundException.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Hibernate" (3) Hibernate single-table operation

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.