Getting started with Hibernate-general Linux technology-Linux programming and kernel information. For more information, see the following section. Configure the environment in this article:
JBuilder X
Jdk 1.4.2
Mysql 4.0.11
Driver: mm. mysql-2.0.4-bin.jar (org. gjt. mm. mysql. Driver)
Hibernate 2.1
Decompress Hibernate
Open JB and create a project named hibernate
Add the packages required by Hibernate and the MYSQL driver
Step: Enter hibernate in file-> new project-> name, and select the path where you want to store the project-> next
-> Required libraries-> add-> new-> name: Enter the hibernate package name you want to set.
-> Add-> select the directory decompressed by hibernate, select the hibernate2.jar and all jar packages in the lib directory, and add your MYSQL driver package.
Then press OK and next.
Create a new class named Hello_Bean.java. The Code is as follows:
Private String name; // The names of name and address and id can be set by yourself, without any impact. However, the get and set methods are not supported. Because it must correspond to the database and configuration file.
Private String address;
Private int id;
Public Hello_Bean (){
}
Public Hello_Bean (String name, String address) {// constructor. I believe you will understand this chapter.
This. name = name;
This. address = address;
}
Public String getName () {// This method name must be the same as the name in the Hello_Bean.hbm.xml file.
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public int getId () {// required Method
Return id;
}
Public void setId (int id) // required Method
{
This. id = id;
}
}
Compile after completing this step
Copy the hibernate. properties and log4j. properties files in the src folder under the decompressed hibernate directory to the classes directory in your project directory.
(For example, in the hibernate \ classes \ directory ),
Create an empty file and save it in the same folder of your project class file (for example, hibernate \ classes \ hibernate \ directory). The file name is Hello_Bean.hbm.xml.
To explain this, The name in refers to the class of your generated table,
Table specifies the name of the database table you want to create. It can be freely modified without any impact.
Set the primary key ID. Here, the value id of name corresponds to the getId and setId of the method in Hello_Bean.java. Without this, hibernate will automatically call and configure it, the column value is the field name to be generated, which can be freely modified without any impact.
Property to auto-increment the ID of the primary key (1 is automatically added when data is inserted)
The name value "name" corresponds to the getName method in Hello_Bean.java, and column is the field name to be generated.
Add a field name and address respectively. Note that the type attribute type here is string. If the type here is different from the type set in Hello_Bean.java, an error will occur.
Save the modification.
Finally, create a new class named Hello. java in JB. I will explain it step by step. The Code is as follows:
Package hibernate;
Import net. sf. hibernate. cfg. Configuration;
Import net. sf. hibernate. SessionFactory;
Import net. sf. hibernate. tool. hbm2ddl. SchemaExport;
Import net. sf. hibernate. Session;
Import net. sf. hibernate. Query;
Import net. sf. hibernate. Hibernate;
Import net. sf. hibernate. type. LongType;
Import net. sf. hibernate. Transaction;
Import net. sf. hibernate. ScrollableResults;
Import java. util .*;
Public static void main (String [] args) throws Exception {
Configuration cfg = new Configuration (). addClass (Hello_Bean.class); // use the Hello_Bean.class class to initialize
SessionFactory sessions = cfg. buildSessionFactory (); // use the buildSessionFactory method to obtain a SessionFactory object
Session session = sessions. openSession (); // use the openSession method of SessionFactory to get a session
New SchemaExport (cfg). create (true, true); // This statement indicates creating a table. After the first operation, add comments to this line after the table is created. If this statement is not commented out after the table is created, the previously created table will be deleted and a new one will be created.
Hello_Bean my_hibernate = new Hello_Bean (); // get a Hello_Bean object
My_hibernate.setName ("my_name"); // you can specify my_name as the name of the Hello_Bean object. the database field name corresponds to the getName and setName methods in the Hello_Bean class. Form a ing relationship.
My_hibernate.setAddress ("my_address"); // As shown above
Session. save (my_hibernate); // This sentence is very important. Write the my_hibernate object into the database (the name and address in the my_hibernate object have just been set, and the name will be directly put, the address value is written into the database)
Session. flush ();
Session. close ();
// The above is a simple introduction to data insertion and table creation for the first time. Next I will introduce the deletion and modification methods. I have added comments to the following code, you can remove the comments in any method (delete, modify, or recycle Database Value) you need.
HSQL is relatively simple. Let's take a look at the example and we should understand it. We will not talk about it here.
There are three methods to traverse the database: Query, find, iterate, Query, and find. A List interface is returned, and iterate returns an Iterator. You can view the methods of these classes.
// Delete data
/*
Int a = session. delete ("from Hello_Bean where id = 1"); // if no data with id 1 is found, 0 is returned. If 1 is found, Hello_Bean here is our Hello_Bean class, it corresponds to the database table, so we use Hello_Bean to code the database table here.
System. out. println ();
Session. flush ();
Session. close ();
*/
// Query the data
/*
Hello_Bean my_hibernate = null;
Query q = session. createQuery ("from Hello_Bean ");
// Query q = session. createQuery ("from Hello_Bean where name =? "); // Here? It is similar to the PreparedStatement method of JDBC, except that it starts with 0 and starts with 1 in jdbc.
// Q. setString (0, "my_name ");
// Q. setFirstResult (0); // This statement means that the query result is used to list data from the first row.
// Q. setMaxResults (3); // This statement indicates how much data is obtained, which is the same as the SQL SERVER TOP method and MYSQL LIMIT method.
// ScrollableResults SC = q. scroll (); // obtain a ScrollableResults that can be rolled. If your database supports moving the cursor freely, you can add it, that is, you can determine whether the query result has a value, or move to the next record.
// If (! SC. next ())
//{
// System. out. println ("the data you need is not found ");
//}
Session. flush (); // If ScrollableResults is used, comment out this line.
Session. close (); // comment out this line if ScrollableResults is used.
List l = q. list (); // returns a List interface to traverse the result set.
For (int I = 0; I My_hibernate = (Hello_Bean) l. get (I); // obtain a my_hibernate object from the List.
System. out. println (my_hibernate.getName (); // call the getName method of the my_hibernate object to obtain the value of the Database name field.
}
*/
// Query data using the find Method
/*
Hello_Bean my_hibernate = null;
List q = session. find ("from Hello_Bean ");
Session. flush ();
Session. close ();
For (int I = 0; I {
My_hibernate = (Hello_Bean) q. get (I );
System. out. println (my_hibernate.getName ());
}
*/
// Query data using the iterate method
/*
Hello_Bean my_hibernate = null;
Iterator q = session. iterate ("from Hello_Bean ");
While (q. hasNext ())
{
My_hibernate = (Hello_Bean) q. next ();
System. out. println (my_hibernate.getName ());
}
Hello_Bean my_hibernate = (Hello_Bean) session. load (Hello_Bean.class, new Integer (2 ));
// The new Integer (2) indicates the row whose id is 2 In the table. It must be a packaging object. If int is used, an error occurs.
My_hibernate.setName ("geezer"); // change the name field value of the row whose id is 2 to "geezer"
Session. flush ();
Session. close ();
*/
}
}
Finally, you can run it.
If you have any questions, download the example of this article and run it.
After the download, use JB to open hibernate. jpx. Add the hibernate and MYSQL drivers before using them. If you do not know the method, read the first part of this chapter.
Common Errors:
Caused by: org. dom4j. Specify entexception: Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence.
If this line of error occurs, it indicates that your xml configuration file contains nonstandard characters. Check.
Net. sf. hibernate. MappingException: Error reading resource: hibernate/Hello_Bean.hbm.xml
If this line of error occurs, your hibernate XML configuration file is incorrect.
Net. sf. hibernate. MappingException: Resource: hibernate/Hello_Bean.hbm.xml not found
If this line of error occurs, the XML configuration file of hibernate is not found. You should put the XML file in the same directory as your class file, this article is stored in the hibernate \ classes \ hibernate \ directory, that is, together with the Hello_Bean.class class file.
Net. sf. hibernate. PropertyNotFoundException: cocould not find a setter for property name in class hibernate. Hello_Bean
If this line of error occurs, the field name value set in your xml file is inconsistent with the getXXX or setXXX method in the Hello_Bean.java class.
Net. sf. hibernate. HibernateException: JDBC Driver class not found: org. gjt. mm. mysql. Driver
If this line of error occurs, your MYSQL driver is not added to the JB library or not in the CLASSPATH.
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