Basic Object-link ing and basic object ing
Attributes and access methods of persistence classes
There are two visitors to the persistence class in Hibernate:
1. Java applications
2. hibernate (when to call the get and set methods ?)
Java applications cannot access the private method of persistence classes, but hibernate does not have this restriction. It can access various levels of methods.
Basic data type and packaging type
The basic data type and the hibernate ing type corresponding to the packaging type are the same.
<Property name = "price" type = "double" column = "PRICE"/>
Basic types can be operated directly, null cannot be expressed, and the default value of numeric type is 0.
The default value of the packaging class is null. Use the packaging class when the default value has business significance.
Policy for accessing persistence attributes of Hibernate
1. Default property value: indicates that hibernate uses getXXX and setXXX to define class attributes. Recommended. Improves the transparency of the domain model.
2. field: hibernate uses the java reflection mechanism to directly render class attributes. You can set this access policy for attributes without the javabean method.
<Property name = "name" access = "field"/>
In addition to setting the access attribute of property to field and property, you can also customize the access policy. You need to create a class that implements the net. sf. hibernate. property. PropertyAccessor interface, and then complete the class name.
Access attribute assigned to the <property> element
Add program logic to persistence class methods
class Customer{ ….. private String firstname ; private String lastname ; public String getName(){ return firstname + “ ” + lastname ; } public void setName(String name){ StringTokenizer t = new StringTokenizer(name); firstname = t.nextToken(); lastname = t.nextToken(); }}
In the customer. hbm. xml file, you do not need to map the firstname and lastname attributes, but the name attributes.
<Property name = "name" column = "NAME"/>
Although the class does not have the name attribute, because hibernate does not directly access the Name attribute, but calls the get and set methods, the relationship between Firstname, Lastname, and table is established.
No matter whether the name attribute exists in the class, you only need to map the name attribute in the Customer. hbm. xml file to access it in the hql statement.
Session. find ("from customer as c where c. name = 'Tom '")
Add the program logic to the setOrders () method of the customer class.
public void setOrders(Set orders){ this.orders = orders ; calprice();}Public calprice(){ …… setAvgPrice(avgprice);}
Add the data verification logic to the setSex () method
public void setSex(char sex){ if(sex != ‘M’ && sex != ‘F’){ throw new IllegalArgumentException(“Invalid sex”); } this.sex = sex;}
Set derived attributes
Use the formula attribute of the <property> element to set an SQL expression. hibernate calculates the value of the derived attribute based on it.
<property name=“totalprice” formula=“(select sum(o.PRICE) from ORDERS o where o.CUSTOMER_ID=ID)” /><property name=“unitprice” formula=“BASE_PRICE*QUANTITY” />
Another solution is to add the program logic to the set method without ing attributes in the ing file.
Insert and update statements
Ing Property |
Function |
<Property> Insert attributes |
If the value is false, this field is never inserted if it is not included in the insert statement. The default value is true. |
<Property> Update attributes |
If the value is false, the update statement does not contain this field, and this field cannot be updated. The default value is true. |
<Class> Mutable attributes |
If it is false, the update attribute of all <property> elements is false, and the entire instance cannot be updated. The default value is true. |
<Property> Dynamic-insert attributes |
If the value is true, a dynamic insert statement is generated when an object is saved. Only when the value of this field is not null will it be included in the insert statement. The default value is false. |
<Property> Dynamic-update attributes |
If the value is true, a dynamic update statement is generated when an object is updated. Only when the value of this field is not null will it be included in the update statement. The default value is false. |
<Class> Dynamic-insert attributes |
True is equivalent to true in dynamic-insert of all <property> elements. when an object is saved, an insert statement is generated dynamically. The statement contains only fields whose values are not null. The default value is false. |
<Class> Dynamic-update attributes |
True is equivalent to true in dynamic-update of all <property> elements. when an object is updated, an update statement is generated dynamically. The statement contains only fields whose values are not null. The default value is false. |
Processing SQL reference identifiers in SQL syntax, identifiers refer to strings used to name database tables, views, fields, or indexes. Regular identifiers do not contain spaces or special characters, therefore, you do not need to use a reference symbol. If the database table name or column name contains special characters, you can use a reference identifier.
<Property name = "description" column = "'customer description'"/>
Set the class package nameIf a ing file contains multiple classes in the same package, you can set the package attribute of the