hibernate< two > Map Base single persistent class with single database table mappings

Source: Internet
Author: User

One, a single persistence class and a single database table mapping foundation

1. In hibernate applications, there are two callers to the persistence class's Access method:

①, Java application: Invokes the GetXXX method of the user object to read the user information. The Setxxx method is called to write user information entered by the users into the client object.

②, Hibernate: Call the User object's GetXXX method, read the user information, save it to the database call the user object's Setxxx method, the user information read from the database is written to the user object. Hibernate's session invokes the user's GetXXX method when executing the Save (), update (), Saveorupdate () method, when the session executes get () load () find () method, the Setxxx method of the user object is called. Java applications cannot access the GETXXX and Setxxx methods of the private type of the persisted class, and hibernate does not have this restriction.

2. Hibernate policy for accessing persisted class properties

The Access property of the,<property> element in the object-relational mapping file is used to specify how hibernate accesses persisted class properties:

①, property: The default value, which indicates that hibernate accesses persisted class properties through the appropriate setxxx () and GetXXX () methods. Recommended way.

②, field: Indicates that hibernate accesses the properties of the class directly using the Java reflection mechanism.

3. The processing method when the properties of the persisted class do not match the fields in the database table:

For example, the persistence class is as follows:

public class user{private String firstName; Private String LastName;}

The field in the database table is name. When hibernate obtains the name field of the user table from the database, it calls the SetName () method, which should allow hibernate to automatically set the Fistname and Latname properties through the SetName () method, at which time the persisted class is as follows:

public class user{private String firstName;    Private String LastName;     Public String GetName () {return firstname+ "" +lastname;        public string SetName (string name) {StringTokenizer t=new StringTokenizer (name);        Firstname=t.nexttoken ();    Lastname=t.nexttoken (); }}

In object-relational mapping file User.hbm.xml, you do not need to map the FirstName and LastName properties of the user class, but instead map the Name property

<property name= "name" column= "name"/>

In addition, the Formula property of the,<property> element specifies an SQL expression that can refer to the table's fields, such as

<property name= "Totoalpirce" formula= "Baseprice * Count" >

4. Control the INSERT and UPDATE statements:

Hibernate pre-defined SQL statements for all persisted classes based on the mapping information of the mapping file during the initialization phase:

Insert, such as user class: INSERT into User (Id,username,order_number) VALUES (?,?,?)

UPDATE statement, DELETE statement, select statement

The mapping properties used to control the INSERT and UPDATE statements:

Insert for <property> element: False indicates that the field can never be inserted, which is true by default.

Update for <property> element: False indicates that the field is never updated and is true by default.

Dynamic-insert of the <property> element: True to indicate that an INSERT statement is dynamically generated when an object is saved, and only if the field is not NULL, it is included in the INSERT statement. The default is False.

Dynamic-update of the <property> element: True indicates that when an object is updated, the UPDATE statement is dynamically generated, and only if the value of the field is changed will it be included in the UPDATE statement, false by default.

<class> Dynamic-insert: Equivalent to all <property> elements of the Dynamic-inser property is true.

The dynamic-update of <class> elements:

The Mutable property of the <class> element: false, equivalent to the Update property of all <property> elements is false, indicating that the entire instance cannot be updated, and the default is true.

5. Set the package name of the class

hibernate< two > Map Base single persistent class with single database table mappings

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.