Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Hibernate persistence class

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use the Hibernate persistence class, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The Hibernate persistent class uses the JavaBean style and provides a set of get and set methods for the properties to be accessed, also known as Hibernate persistent class access methods. I remember that I was confused when I came into contact with JavaBean. I always thought that it would be better to set the property of Bean to public and then call the object directly. Attribute, this is convenient. But later, there was a requirement that a person's name could be checked, but not modified. At this time, if you use the set method, you only need to change the modifier of the set method to private and OK. If you are using an object. It is troublesome to call the property in the form of the.

And the most important thing is that the style of JavaBean can simplify the process of Hibernate getting access methods of Hibernate persistence classes through JAVA reflection mechanism. As for the reflection mechanism of JAVA, I also have some superficial knowledge. It is said to be very esoteric. Friends who are interested can Google it. It is worth noting that JAVA applications cannot access private-type get,set methods of JavaBean persistence classes. Hibernate does not have this restriction and can access all levels. Including private default,protected,public.

There are eight basic types of Java: byte,short,char,int,long,float,double,boolean and eight corresponding wrapper types. Byte,Short,Character,Integer,Long,Float,Double,Boolean wrapper type means to wrap a basic type into an object. Basic types can be easily converted between wrapper types, such as:

Int I = 0; Integer ie = new Integer (I); / / convert the basic type to the packaging type I = ie.intValue (); / / convert the packaging type to the basic type

Note that direct conversion is also possible. For example, I = ie; JAVA automatically converts the wrapper type to the base type. Or ie = I; JAVA automatically converts the base type to the wrapper type. In the Hibernate persistence class, attributes can be defined as either the basic type or the wrapper type, their corresponding Hibernate mapping types int and Integer both correspond to int types, this representation is not very obvious, in using the JAVA object type string for example String, the database is varhcar (50), all written as string in the hibernate * .hbm.xml mapping file.

In fact, using basic types or wrapper types to define properties in Hibernate persistence classes has its own advantages and disadvantages. The basic type is easy to use, simple, and can be operated directly when numerical operations are needed. The wrapper type is troublesome to convert to the basic type and then perform the operation, but the advantage of the wrapper type is that it can express the null value, and the default value of each wrapper type object is null when it is created. While basic types cannot express null, their default value is 0. 0. Why express a null value? Because in SQL. The default value for all types of data is null.

When entering insert, there is no attribute with complex value, and the default value is null, so the corresponding relationship between the wrapper type of JAVA and the database is more direct. It is recommended that the OID of Hibernate be set to the packaging type, and other properties should be set according to business needs and personal habits.

In the initialization phase, Hibernate will predefine insert statements for all Hibernate persistence classes according to the information of the mapping file, update statements where ID,delete statements where ID,select statements where ID, the statements here are the standard SQL add, delete, change, check. Statement, parameter is used? Represents the parameters in JDBC PreparedStatement, so I won't give an example here. These SQL statements are stored in the SessionFactory cache. When the save (), update (), delete (), load () methods of Session are executed, these predefined SQL statements will be read from the SessionFactory cache. When binding specific parameter values to SQL statements, this is the basic principle of Hibernate. By default, these statements represent all fields. Of course, Hibernate also allows us to control the contents of insert and update statements in the mapping file.

For example, in the mapping file

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report