"Note--hibernate" a preliminary understanding of Hibernate 4.3.5

Source: Internet
Author: User
Tags generator object object log4j

A framework for connecting to a database, one of the ORM frameworks.

First, download: http://hibernate.org/

Second, the configuration:

1. Import the necessary packages

Hibernate-release-4.3.5.final\lib\required (Required jar)

If you need to use Annotataion, import:

HIBERNATE-RELEASE-4.3.5.FINAL\LIB\JPA (JAP-related jar)

Hibernate-release-4.3.5.final\lib\jpa-metamodel-generator (JPA Generator)

Javaweb into the Webcontent\web-inf\lib directory.

Javase into any folder, and then import the build path.

2, Configuration Hibernate.cfg.xml

In the root directory of SRC, the basic content is as follows

<?xml version="1.0" encoding="UTF-8"?>

<! DOCTYPE hibernate-configuration Public

"-//hibernate/hibernate Configuration DTD 3.0//en"

"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >

<session-factory>

<!--JDBC Connection settings--

<property name="Connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="Connection.url">jdbc:mysql://localhost/dinnersystem</ Property>

<property name="Connection.username">root</property>

<property name="Connection.password">303269789</property>

<!--connection pool configuration using C3P0 connection pool--

<property name="hibernate.c3p0.max_size">20</property>

<property name="hibernate.c3p0.min_size">1</property>

<property name="Hibernate.c3p0.timeout">1800</property>

<property name="hibernate.c3p0.max_statements">50</property>

<property name="Hibernate.c3p0.idle_test_period">3000</property>

<!--database Language--

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- SQL Settings-

<property name="Show_sql">true</property>

<property name="Format_sql">true</property>

<!--persistence class set all classes that need to be persisted here, here is the class for annotation comments--

<mapping class="com.kirning.android.entity.Test" />

</session-factory>

3. Configuring Persistent classes

Only classes that have attributes and Getter/setter methods, which need to be labeled annatation on the class name @entity indicate that this is a durable class. and add @entity on the ID, indicating that this is an ID corresponding to the keyword in the database, each persistent class must be filed in Hibernate.cfg.xml, otherwise throw an exception, cannot be used.

@Entity

Public class Test {

Private int ID;

Private String name;

Private String Pass;

@Id

Public int getId () {

return ID;

}

Public void setId (int id) {

this. id = ID;

}

Public String GetName () {

return name;

}

Public void setName (String name) {

this. Name = name;

}

Public String Getpass () {

return Pass;

}

Public void setPass (String pass) {

this. Pass = Pass;

}

}

4, if you want to use log4j, then download

apache-log4j-1.2.17 http://pan.baidu.com/share/link?shareid=121565833&uk=2047106924

slf4j-1.7.7 http://pan.baidu.com/share/link?shareid=123250651&uk=2047106924

Apache-log4j-1.2.17/log4j-1.2.17.jar

Slf4j-1.7.7/slf4j-log4j12-1.7.7.jar

Enter the HIBERNATE-RELEASE-4.3.5.FINAL\PROJECT\ETC

Log4j.properties to the SRC root directory.

Third, the use

1. Get session

Public class Hibernateutil {

Private Static Final Sessionfactory sessionfactory = buildsessionfactory();

Private Static Sessionfactory Buildsessionfactory () {

Try {

Load Settings

Configuration configuration = new configuration (). Configure ();

Create Serviceregistry

Serviceregistry serviceregistry = new standardserviceregistrybuilder ()

. Applysettings (Configuration.getproperties ()). build ();

Get Sessionfactory

Sessionfactory sessionfactory = Configuration

. Buildsessionfactory (Serviceregistry);

return sessionfactory;

} catch (Throwable ex) {

System. err. println ("Initial sessionfactory creation failed." + ex);

Throw New Exceptionininitializererror (ex);

}

}

Public Static Sessionfactory Getsessionfactory () {

return sessionfactory;

}

}

A tool class can be created to facilitate access to the session this is based on the 4.3.5 official documents in the case and the online query results, do not know why the official use, there are errors.

The session encapsulates Hibernate's method of accessing the database, all of which need to get the session.

1. Storage:

First, you need to instantiate a persistent class. and use setter methods to put the data in. Then call the session's Save method.

e.g:

Public class Testpost {

Public void Test () {

Test test = new test ();

Test.setname ("test");

Test.setpass ("test");

Get session

Session se = hibernateutil. getsessionfactory (). Opensession ();

Start transaction processing

Se.begintransaction ();

Save to Database

Se.save (test);

Commit a transaction

Se.gettransaction (). commit ();

}

}

2. Enquiry:

A), according to the keyword query:

Public class Testpost {

Public void Test () {

Get session

Session se = hibernateutil. getsessionfactory (). Opensession ();

Start transaction processing

Se.begintransaction ();

0 is the ID,hibernate will take the data out according to the ID and populate the test class of the various properties to return an object object, strong to go.

Test Test = (test) se.get (test.class, 0);

Commit a transaction

Se.gettransaction (). commit ();

}

}

b), according to the keyword query

Public class Testpost {

Public void Test () {

Get session

Session se = hibernateutil. getsessionfactory (). Opensession ();

Key words

String key = "Tom";

Start transaction processing

Se.begintransaction ();

Create a Criteria Object

Criteria cr = Se.createcriteria (Test.class);

Add condition

Cr.add (restrictions. EQ("name", key));

return result set

list<test> List = cr.list ();

Commit a transaction

Se.gettransaction (). commit ();

}

}

c) Access to the first 10 data

Public class Testpost {

Public void Test () {

Get session

Session se = hibernateutil. getsessionfactory (). Opensession ();

Start transaction processing

Se.begintransaction ();

Create a Criteria Object

Criteria cr = Se.createcriteria (Test.class);

The HQL statement is used here

Query query = Se.createquery ("From menutable Order by id");

Query.setfirstresult (0);

Query.setmaxresults (10);

return result set

list<test> List = cr.list ();

Commit a transaction

Se.gettransaction (). commit ();

}

}

3. Modify the data

This word ... Check out first, in the preservation of the Good (*^__^*)!

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.