Hibernate persistent object configuration

Pojo defined objects and **. Hbm.xml file

-1 For each persistent objects are required to create a need Pojo class definition, Hibernate requires the existence of a no-argument constructor of POJO class definition, create an instance by facilitating Hibernate java reflection mechanism: [java.lang.reflect. Constructor.newInstance ()];

-2 Pojo class definition for class attributes, the operator regardless of whether or not access to public, regardless of whether the corresponding setter / getter methods, Hibernate can be accessed by a java reflection, class attribute type is preferably used packing type This facilitates service isolation;
-3 does not require each of the Hibernate POJO must implement Serializable, but if employed J2EE application and distributed architecture of the RMI, POJO transfer nodes between different processes or in the HttpSession, it is always necessary this interface is implemented;

1 <hibernate-mapping package="com.leo.chen.dao.hibernate">
2     <class name="User" table="user" dynamic-insert="true" dynamic-update="true"> 
3         <id name="id" column="id" access="property">
4             <generator class="native"></generator>
5         </id>
6         <property name="name" column="name access="field"></property>
7         <property name="password" column="password" access="field"></property>
8     </class>
9  </hibernate-mapping>

Specify insertion row primary key generation strategy

Design of the database table when the primary key ID generally use a separate field of non-business-related, thus ensuring affect the main key at any time, notwithstanding the changes for <generator> For hibernate offers a variety of ways:
# 1 surrogate primary key: increment
hibernate responsible for the generation, each in increments of 1, insert a data performs two sql statements to find out the maximum id start with the table, do one, then insert the current data; applicable to all databases, but if more than SessionFactory simultaneously connected to a DB, the increment of the increment fails to operate; DB-only access by a single Hibernate App scene; the OID must Long, INT or Short;

# 2 surrogate primary key: identity
responsible for the underlying database generation, depends on a database primary key self-energizing function; provided by the DB, according to different DB configuration DB dialect, whereby mapped into different types, MySQL is auto_increment, SQL Server as Identity; bottom the database must support automatic growth type field; OID must be Long, INT or Short;

Agent primary key # 3: sequence
set by Hibernate, but from the underlying database sequences; sequences underlying database must support, MySQL does not support sequences, Oracle, DB2, and PostgreSQL support; the OID must Long, INT or Short;

# 4 single natural primary key: assigned
represented by the application for the current assignment, and the property is a natural primary key, business-related, is never null; general saveOrUpdate () method of the primary key ID is null object can be determined by determining the state when the primary key generator generates the attribute is assigned when not is judged; only through an additional attribute <version> controls; unsaved-value is null if it is expressed as a temporary object, it means the free non-null objects;

 1 <hibernate-mapping>
 2   <class name="org.hibernate.tutorial.domain.Cart" 
 3       table="CARTS" dynamic-update="true">
 4     <id name="name" type="string" access="field">
 5       <column name="CART_NAME" />
 6       <generator class="assigned" />
 7     </id> 
 8     <version name="version" column="VERSION" 
 9       unsaved-value="null"/>
10   </class>
11 </hibernate-mapping>

# 5 complex natural primary key: Composite-ID
the Customer object is determined by the CustomerId objects, CustomerId by the name and company composed; all Natural bond as the primary key class need to define the version attribute to distinguish between state; <many-to-one> tag indicates company many to one relationship with the customer; Insert and update property to false representation when the customer object is saved when the company ignores the property;

 1 <hibernate-mapping>
 2   <class name="mypack.Customer" table="CUSTOMERS">
 3     <composite-id name="customerId" class="mypack.CustomerId">
 4       <key-property name="name" column="NAME" type="string" />
 5       <key-property name="company" column ="COMPANY_ID" type="long" />
 6     </composite-id>
 7     <version name="version" column="VERSION" unsaved-value="null" />
 8     <many-to-one name="company" column="CLIENT_ID" 
 9       insert="false” update="false”
10       class="org.hibernate.tutorial.domain.Company" />
11   </class>
12 </hibernate-mapping> 

# 6 hilo: a hibernate primary key generation algorithm according hilo themselves.
# 7 native: The identity of the underlying database supports the ability to automatically select the primary key generation | Sequence | Hilo
# UUID. 8: According to the mac address of the current machine and the time generated UUID as the primary key.
# 9 customized class: id can customize the interface by implementing org.hibernate.id.IdentifierGenerator.

Access method specified Dao class attribute
<id> and <property> attribute access class properties can be controlled access mode, the default for the Property:
# 1 access = "field": Let hibernate represents direct field access by way of reflection, loss encapsulation;
# 2 = access "property": let hibernate expressed by Foreigners exposed getter / setter access field, it is recommended;

Dao and data associated with the specified object class loading strategy generation method and SQL
load Dao objects <class> element can be used to control timing of the loading lazy property class data, the default is to true;
#. 1 = lazy "to true": When a load () Mr. loading the objects will become a proxy object, which only primary key is assigned to the DB will generate SQL statements to query attributes only when other objects such as actually used.
# 2 lazy = "false": load immediately, that is, using the load method will generate a SQL query to the DB immediately.

Cascade query <set> element uses lazy loading to control the timing of associated objects and defaults to true;
# 1 = lazy "to true": with <class> level of empathy;
# 2 lazy = "false": with <class > Similarly level;
# =. 3 the lazy "Extra": fine use lazy loading, such as a method call to the elements associated with the object size / contains / isEmpty non when access to the entire collection does not load the whole set of elements, generate a return only specific SQL information;

Cascade query <set> SQL when using the fetch element to control the associated object query generation strategy
# 1 fetch = "select": default value, independent of additional generating a SQL query.
# 2 fetch = "subselect": an additional set of data to generate an SQL query, a query such as when a plurality of users, each user has a plurality of orders, which can be via a separate SQL all orders disposable check out all users, different orders in a memory assigned to different users.
# 3 fetch = "join": the use of LEFT JOIN table is the only way to generate SQL, SELECT reduce the number of executions, but will ignore lazy property loading all the data immediately, but due to the associated multiple tables can cause performance degradation;

Cascade query <many-to-one> / <one-to-many> lazy and also fetch properties, functions similarly. Also provided on the batch-size <class> and <set> element = "10" may generate SQL bulk load data into the data cache, thereby reducing the number of execute SQL statements.

Obtaining complex queries by sql statement field value
Hibernate totalPrice to dynamically set value, the operation of each formula may Field of the formula according to sql statement, there is no need total_price orders table has a field; if the result of the query is empty formula totalPrice value returns null, so totalPrice Java class attribute type needs to be set Double packaging.

1 <property name=”totalPrice” 
2   formula=”SELECT SUM(o.price) FROM orders o WHERE o.customer_id=id” />

Forced insert and update dynamically generated SQL statements
Hibernate can pre-generated and compiled by default for all column of the insert and update clause; but precompiled sql statement to be written to the DB field regardless of whether it is empty, regardless of pending whether the fields are written to the DB update, are all operated, when more of the column when the default settings will consume more of the resources DB; Hibernate provides therefore for the respective key on the control column of <class> insert and update:
# 1 insert properties: default is true; if false, indicating the price field is no longer generated Hibernate insert, i.e. can not be inserted into the DB column;
<property name = "price" iNSERT = "to false" />

# 2 update attributes: Default is true; if false, indicating the price field is no longer generated Hibernate update, i.e. which can not be updated DB column;
<Property name = "price" Update = "to false" />

# 3 mutable attributes: Default is true; if false, indicating that the update of the attributes of all the class property is false, this example can not be updated;
<class name = "org.hibernate.totorial.domain.User" Table = " USER "mutable =" false "/ >

# 4 dynamic-insert attribute: Default is false; if true, indicates insert statements dynamically generated during insertion of an object, insert the field contains only the value of the field is not null;
<class name = "org.hibernate.totorial. domain.User "table =" USER "dynamic -insert =" true "/>

# 5 dynamic-update attribute: Default is false; if true, indicates update statement updating dynamically generated object, update the value of the field contains only the updated fields;
<class name = "org.hibernate.totorial.domain .User "table =" USER "dynamic -update =" true "/>

# 6 select-before-update attribute: The default is false, that as long as the use of Session.update (), regardless of whether the target object property updates UPDATE operation will be executed; if true, it will execute a SELECT statement to query the first DB and comparing whether there is a state update, it indicates the target object only if the properties are modified only when an uPDATE operation;
<class name = "org.hibernate.totorial.domain.User" Table = "the USER" SELECT-before-update = " true "/>

Reproduced in: https: //www.cnblogs.com/leo-chen-2014/p/4940569.html

Guess you like

Origin blog.csdn.net/weixin_33859844/article/details/94232543