Hibernate learning

xiaoxiao2021-03-06  37

First, Hibernate Environment Construction 1. Install ANT Direct Defination, Set Environment Variables Requires Environment Variables: Ant_Home: Ant's installation directory java_home: JDK installation directory PATH: Plug the% Ant_Home% / bin directory to PATH variables, To run directly from the command line to establish an engineering description file build.xml, specify the item related information. Assume that the tool uses WSAD 5.0, create a BULID directory in the Java Source class, which is copied into the hibernate.properties, log4j.properties file, copy it to the build.xml file. Modify some of the LIB absolute directories such as XDoClit, Hibernate, etc. in the build file, specify the Java file directory. 2. Copy corresponding file, it is recommended to establish a directory of Hibernate_API, which enters the database driver, log4j.jar, xdoclet, Hibernate, and other JAR files, so that Ant is compiled. 3. Analyze business objects, determine relationships, including One To Many, One To One, etc., establish a VO object, and change the object only including GET, SET method. It is also necessary to add List or Object to it according to the blocking relationship. Add a comment after completion. Note that you have to add a Hibernate primary key for each object, and the outer is automatically generated. 4. Establish a project in WSAD, put the build directory COPY to and the Java Source. Under the DOS window, go to the corresponding build directory, run: ant -buildfile build_test.xml If the build file is build.xml, then run the ANT command directly, if * .hbm.xml file, the DDL file correctly Then indicate that the environment configuration is correct. 5. Establish a hibernate.cfg.xml file, place it in the Java Source directory, configure and DB2 database connection parameters. Run Build generation DDL file, command: db2 -stvf xxxx.ddl generates a database table. 6. Establish the corresponding service file and JSP file, run the project. Second, the program test This is the simplest example to describe Hibernate's MANY-ONE usage. There is two objects that are user and groups, the User object has

UseridPasswordRealNameemail

Such a few simple fields, the group object has only one

Groupname

UseridPasswordRealNameemail This kind of simple field, the group object has only one

Groupname

Two Java classes are established separately. The relationship between GROUP and USER here is One-Many. So you want to add the foreign key of GroupPK in the user, this is automatically added using Build. Create a DAO of the service in Service GroupDao.java and UserDao.java, controlling two objects. Create a TestService.java file in Service, used to represent a layer interaction. Run http: // localhost: 9080 / source / test / managegroup.jsp shows the results. Third, add some hibernate concept 1. About the object Java said, everything is object. The value of the object is far more than just providing "container of storage data." There is a shape relationship between the objects. According to the classic object-oriented theory, the relationship between the objects has inclusive, polymerization, use, etc., there is also inheritance relationship between the class. It is because of these rich relationships, the object can become a truly useful software development tool. Hibernate is really exciting, where it can deal with the relationship between the persistent objects. In other words, the programmer does not care about "how the object is stored in the database", as long as the object is used in the most common way, Hibernate naturally handles all kinds of relationships. 2. The relationship between the objects Group and User are one-to-many relationship. User.hbm.xml and group.hbm.xml must be established in its generic Class directory. The relationship between two objects is to be specified. For Group to add the node, add the container reference to the User object. Here, use list, including Here are two important properties: Lazy and cascade.lazy refers to whether is provided? Lazy loading, usually use True to open. Cascade refers to whether a cascade operation is provided. If the value is all, indicates that the deletion of Group objects will cause cascading to delete, save all User objects related to it; if the value is None, the user's delete is not triggered to associated with it. Do anything. 3. About the primary key in each .hbm.xml, the node provides the definition of the primary key, there is an important node . Hibernate provides four main keys: Hi / Low, UUID, Identity and Assigned, the first three policies are automatically generated by the database; Assigned policies are objects that give the primary key value to the object by the Client code and store them into the database. 4. About O / R Mapping O / R Mapping refers to an operation: it is attempted to map the status of the Java object to the data of the RDBMS to provide transparent persistence operations. , O / R mapping is a large phase of the target of "database design" in the traditional sense, although they operate the same object-relational database. The goal of the relational database design is to save data with minimal redundancy; the O / R mapping target is a simple persistence of entity objects in the business model, enabling developers to avoid writing data access. We can divide common enterprise applications into two categories: OLTP (online transaction) and OLAP (online analysis).

The main task of the former is to complete a set of business, data is stored as business processing services; the main task of the latter is to query analysis of existing data, provide various reports and even decision support, all work around existing data Expand. Obviously, the category of O / R mapping is strictly limited to OLTP. OLAP is completely unnecessary to consider using O / R mapping, it is another completely different world. 5. Object Structure Model Example Some of the explanations of the figure: the DAO (Data Access Object, Data Access Object) of the persistence medium, is assembled into an entity; the service layer's service object uses an entity to operate, assemble DTO (Data Transfer Object, Data Transfer Object); the display layer carries the data carried by DTO to the user in some form. For transaction management, put on the business layer, a complete transaction is performed for each service method, and its effect is all or all. If you put your transaction in a persistent layer, you will cause great performance losses, such as EJB. DTO looks quite similar to the entity, but it plays a completely different role. The entity is the corresponding object of the Physical World in the software system, and DTO is the package of data required for the display layer. For DAO design is very simple, there is no need to consider the transaction, just achieve the following methods: public entity create (); public object load (long ID); public void update; public void remove (entity entity); 6 Class Structure Example 7. Supplementary object-oriented development methods are the mainstream of today, but we have to use relational databases, so in environments developed in enterprise applications, objects, and ORMs are a time consumption. work. Hibernate is a new ORM mapping tool not only provides a map from the Java class to the data sheet, but also provides a data query and recovery mechanism. To manually operate the database relative to using JDBC and SQL, use Hibernate, which can greatly reduce the workload of the operational database. Fourth, Hibernate Query Language: HQL first clarifies that the HQL statement is case sensitive. 1. From statement from eg.cat Here CAT is an object name instead of a table name. Usually we will start a separate name, such as eg.cat as cat. AS can be omitted. 2. SELECT statement is to determine which objects you have to return from the query or which objects. Write a few examples of it: select employee form Employee as employee select employee form Employee as employee where employee.Name like 'J%' select employee.Name form Employee as employee where employee.Name like 'J%' select employee.ID as id1 , employee.Name as name1, department.ID as id2, department.Nameas name2 from Employee as employee right join Department as department on employee.DepNo = department.ID here you can also use inner join and outer join, and usage is similar to SQL.

3. Mathematical functions HQL also supports AVG (...), Sum (...), Min (...), Max (...), Count (*), Count (...), Count (Distinct) ...), count (all ...), etc., the usage and SQL basically the same, such as Select Distinct Employee .Name from Employee As Employee Select (DistINCT Employee.Name, Count (Employee) from Employee As Employee 4. Polymorphismfrom com.test.animal as animal is not only available for all Animal instances, and you can get all animal subclasses (if we define a subclass CAT) A comparative extreme example from java.lang.Object AS O can get all lasting persistence example 5. defined conditions where clause of the query type, bar a few examples: from Employee as employee where employee.Name = 'Jplateau'from Employee as employee where employee.Name like' J% 'from Employee as employee where employee .Name like '% u' in the where statement "=" not only compares the properties of the object, or compares the object, such as: select animal from com.test.animal as animal where animal.name = dog6. Expressment knows When the passing parameters in SQL are inquiry, we usually use prepaaredStatement, write a lot of "?" In the statement, and in HQL can also be used in HQL, such as: list mats = sess.find ("SELECT EMPLOYEE. Name from Employee As Employee " " Where Employee.name =? ", name, hibernate.string; (Note: The above use of the Find method in the session, there is a lot of Find methods in the Hibernate's API session, it can meet In a variety of forms of queries), it is a case of a parameter, and in this case, the introduction parameters and definitions are The type of parameters, when multiple parameters, call another Find method, the latter two parameters are the form of an array and another method to solve the problem of the upper side, such as Query Q = sess.createQuery ("SELECT Employee.name from Employee As Employee Employee.Name =: Name "); Q.SetString (" Name "," JPLATEAU "); // When there are multiple parameters, Iteerator Employees = Q.ITERATE ( ); 7. Sub-query Hibernate also supports subqueries, written a few examples: from eg.cat as fatcat where fatcat.weight> (SELECT AVG (Cat.Weight) from eg.domesticcat cat) 8. Hibernate.cfg.xml Configuration 转载请注明原文地址:https://www.9cbs.com/read-68830.html


New Post(0)