Improvement on "the first Hibernate program compiled by hand"

Source: Internet
Author: User

In the previous blog titled writing the first Hibernate program by hand, we introduced how to manually write the first simple Hibernate program. In that example, all classes have no package Declaration to reduce programming difficulty. However, in actual development, classes are usually declared in a package to solve the naming conflict problem. Therefore, this blog article introduces how to add a package Declaration for the Class Based on the first program example, and access the information stored in the previous example of atango (for ease of reading, comments repeated in all programs and configuration files and the previous instance are deleted. If you do not understand the comments, refer to the previous blog ).

In the directory E: \ DemoPrograms, create a directory ImprovedHibernate to save all the files of this instance.

First, let's look at the User (in the User. java file) class.

1. package com. abc. people;
2.
3. public class User {
4.
5. private int id;
6. private String name;
7. private int age;
8. private String gender;
9.
10. public int getId (){
11. return id;
12 .}
13.
14. public void setId (int id ){
15. this. id = id;
16 .}
17.
18.
19. public String getName (){
20. return name;
21 .}
22.
23. public void setName (String name ){
24. this. name = name;
25 .}
26.
27. public int getAge (){
28. return age;
29 .}
30.
31. public void setAge (int age ){
32. this. age = age;
33 .}
34.
35. public String getGender ()
36 .{
37. return gender;
38 .}
39.
40. public void setGender (String gender)
41 .{
42. this. gender = gender;
43 .}
44.
45 .}

 

As shown in the first line of code above, we declare the User class in the com. abc. people package. The package hierarchy corresponds to the directory hierarchy one by one. Therefore, we create the directory structure com \ abc \ people (as shown in) under the directory ImprovedHibernate ).

 

Then place User. java in The people directory and compile it. In fact, it does not matter where the User. java file is stored. What is important is that the compiled User. class file should be in the correct position. After the above compilation, the User. class file is generated in the correct location -- under the people directory.

Let's take a look at the User. hbm. xml ing file. Currently, the User class is in the package com. abc. people. Therefore, the package name is specified using the package attribute in the hibernate-mapping element. The sixth line is shown below.

1. <? Xml version = "1.0" encoding = "gb2312"?>
2. <! DOCTYPE hibernate-mapping PUBLIC
3. "-// Hibernate/Hibernate Mapping DTD 3.0 // EN"
4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
5. <! -- Package specifies the package prefix of the class -->
6. 7.
8. <class name = "User" table = "users">
9.
10. <id name = "id" type = "java. lang. Integer" column = "ID">
11. <generator class = "identity"/>
12. </id>
13. <property name = "age" column = "age"/>
14. <property name = "name"/>
15. <property name = "gender"/>
16. </class>
17.
(User. hbm. xml file)
This file should be associated with User. java files are placed in the same directory (more accurately, it should be consistent with the User. class is placed in the same directory), that is, the com \ abc \ people directory under ImprovedHibernate.
Next let's take a look at the configuration file hibernate. cfg. xml. Because the ing file User. hbm. xml is placed in the com \ abc \ people directory, You need to modify the resource value. As shown in rows 20 to 22 below.
1. <? Xml version = "1.0" encoding = "gb2312"?>
2.
3. <! DOCTYPE hibernate-configuration PUBLIC
4. "-// Hibernate/Hibernate Configuration DTD 3.0 // EN"
5. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
6.
7.
8.
9. <session-factory>
10.
11. <property name = "connection. driver_class"> com. microsoft. sqlserver. jdbc. SQLServerDriver </property>
12. <property name = "connection. url"> jdbc: sqlserver: // localhost; databaseName = mydb </property>
13. <property name = "connection. username"> sa </property>
14. <property name = "connection. password"> admin123 </property>
15. <property name = "connection. pool_size"> 1 </property>
16. <property name = "dialect"> org. hibernate. dialect. SQLServerDialect </property>
17. <property name = "current_session_context_class"> thread </property>
18. <property name = "cache. provider_class"> org. hibernate. cache. NoCacheProvider </property>
19. <property name = "show_ SQL"> true </property>
20. <! -- Specify the ing file to be used, and write the ing file relative to ImprovedHibernate.
21. Complete directory path. -->
22. <mapping resource = "com/abc/people/User. hbm. xml"/>
23.
24. </session-factory>
25.
26.
Hibernate. cfg. xml file
Note that "/" is used as the separator instead of "\" in Windows.
Next, modify the TestHibernate. java file. We declare it in the com. abc Package, and modify the function of this program to read the information stored in the first hibernate program. The details are as follows:
1. // package declaration.
2. package com. abc;
3.
4. import org. hibernate. Session;
5. import org. hibernate. SessionFactory;
6. import org. hibernate. cfg. Configuration;
7. // note that the User class should be correctly introduced.
8. import com. abc. people. User;
9.
10. public class TestHibernate {
11.
12. public static void main (String [] args ){
13.
14. Configuration cfg = new Configuration (). configure ();
15. SessionFactory factory = cfg. buildSessionFactory ();
16. Session session = factory. getCurrentSession ();
17. session. beginTransaction ();
18.
19. // load a User instance from the database. Two parameters are required. The first parameter
20. // com. abc. people. User. class is the Class of the User Class (see class Class
21. // related content) object. 1 is the identity attribute of the User instance. Actually
22. // read a record from the database based on the primary key and encapsulate it into a User object.
23. // is it easier and more natural to write JDBC statements by yourself?
24. User user = (User) session. load (com. abc. people. User. class, 1 );
25.
26. String name = user. getName ();
27. String gender = user. getGender ();
28.
29. System. out. println ("****************");
30. System. out. println ("* name:" + name + "*");
31. System. out. println ("* gender:" + gender + "*");
32. System. out. println ("****************");
33.
34. session. getTransaction (). commit ();
35.
36. factory. close ();
37.
38 .}
39 .}

 

Correspondingly, according to the package name, this file should be placed in the com \ abc directory under the ImprovedHibernate directory.

If TestHibernate. java is compiled, a large number of errors are reported, indicating that the XX package does not exist and the YY symbol cannot be found. This is because we have not introduced the hibernate JAR package to be used by the program, nor correctly set the CLASSPATH environment variable. The JAR package used this time is the same as the previous one, so you only need to simply copy the lib directory used last time to ImprovedHibernate. The directory structure of the entire instance is shown in:

 
 

The next step is to write the batch processing file setclasspath. bat. Similar to the previous batch processing file, you need to add the JAR package to CLASSPATH. In particular, do not miss the current directory. The content of this batch processing file is as follows.

  1. Set classpath = .; e: \ DemoPrograms \ ImprovedHibernate \ lib \ hibernate3.jar; E: \ DemoPrograms \ ImprovedHibernate \ lib \ sqljdbc4.jar; E: \ DemoPrograms \ ImprovedHibernate \ lib \ dom4j-1.6.1.jar; E: \ DemoPrograms \ ImprovedHibernate \ lib \ slf4j-api-1.6.2.jar; E: \ DemoPrograms \ ImprovedHibernate \ lib \ slf4j-simple-1.6.2.jar; E: \ DemoPrograms \ ImprovedHibernate \ lib \ hibernate-jpa-2.0-api-1.0.1.Final.jar; E: \ DemoPrograms \ ImprovedHibernate \ lib \ cglib-2.2.jar; E: \ DemoPrograms \ ImprovedHibernate \ lib \ commons-collections-3.1.jar; E: \ DemoPrograms \ ImprovedHibernate \ lib \ javassist-3.12.0.GA.jar; E: \ DemoPrograms \ ImprovedHibernate \ lib/jta-1.1.jar

 
 

Note: The preceding set command is a complete doscommand. Do not add the Enter key. Otherwise, an error is returned.

In the command line window, switch the directory to ImprovedHibernate and run this batch of files. Because the preceding com \ abc and com \ abc \ people directories are stored in this directory, the current directory in CLASSPATH is used to execute javac and java commands in this directory. In this way, User classes can be correctly loaded. Use the following command to compile TestHibernate. java.

Javac com \ abc \ TestHibernate. java

In this way, the TestHibernate. class file is generated under the com \ abc directory. Run the following command: java com. abc. TestHibernate (full name of the write class ). Do not forget to enable the SQL Server service before running. We can see that after a long string of logs, the program runs as follows:

 

It runs successfully.

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.