Configure hibernate in Java

Source: Internet
Author: User

In fact, it is not very difficult to configure hibernate in Java. The main reason is that you should pay attention to some settings. Otherwise, errors may easily occur. You can run the configuration once before. After the system was reinstalled, It Was Just configured again, but it could not be run. After a few twists and turns, I knew it was wrong.

Now, write down the configuration process to avoid mistakes in the future.

Create a Java Project

Import the required jar packages, including:

--------------------------------------------

Hibernate3.jar,

Antlr-2.7.6.jar,

Commons-collections-3.1.jar,

Dom4j-1.6.1.jar,

Javassist-3.9.0.jar,

Jta-1.1.jar,

Slf4j. api-1.5.8.jar,

Slf4j-nop-1.5.8.jar

--------------------------------------------

If you are using annotation, you also need to import the following jar package:

--------------------------------------------

Hibernate-annotations.jar

Hibernate-commons-annotations.jar

Ejb3-persistence.jar

--------------------------------------------

Create a class as the student model: Student. java. The Code is as follows:

 

 package com.hzucmj.hibernate.model;

public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}

 

Create a ing file for this class in the same directory of Student. Java: Student. HBM. xml. The content is as follows:(The content can be copied in the hibernate API documentation. There is no need to repeat it yourself. Who is the dumb)

 

 

 <? XML version = "1.0"?>
<! Doctype hibernate-mapping public
"-// Hibernate/hibernate mapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>

<Hibernate-mapping package = "com. hzucmj. hibernate. Model"> <! -- Remember to write the correct package here. Otherwise, an error occurs. -->
<Class name = "student" table = "student"> <! -- If the table name is similar to the class name, the table attribute can be left blank. -->
<ID name = "ID"> </ID>
<Property name = "name"> </property>
<Property name = "Age"> </property>
</Class>
</Hibernate-mapping>

 

 

Create a configuration file hibernate. cfg. XML in the src directory. The content is as follows:(The content can be copied in the hibernate API documentation. Likewise, who is the dummies)

 

 <? XML version = '1. 0' encoding = 'utf-8'?>
<! Doctype hibernate-configuration public
"-// Hibernate/hibernate configuration DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd>

<Hibernate-configuration>

<Session-factory>

<! -- Database connection settings --> <! -- Select the database driver. For MySQL, change it to com. MySQL. JDBC. Driver. -->
<Property name = "connection. driver_class "> COM. mySQL. JDBC. driver </property> <property name = "connection. URL "> JDBC: mysql: // localhost/hibernate </property>
<Property name = "connection. username"> root </property>
<Property name = "connection. Password"> 123456 </property>

<! -- JDBC connection pool (use the built-in) -->
<Property name = "connection. pool_size"> 1 </property>

<! -- SQL Dialect --> <! -- Select the SQL language. For MySQL, change it to org. hibernate. dialect. mysqldialect -->
<Property name = "dialect"> org. hibernate. dialect. hsqldialect </property>

<! -- Enable hibernate's automatic Session Context Management -->
<Property name = "current_session_context_class"> thread </property>

<! -- Disable the second-level cache -->
<Property name = "cache. provider_class"> org. hibernate. cache. nocacheprovider </property>

<! -- Echo all executed SQL to stdout -->
<Property name = "show_ SQL"> true </property>

<! -- Drop and re-create the database schema on startup --> <! -- Write update is only an update operation. You can also write create to delete all the content in the database and recreate it when writing create. Of course, the premise is that the database must exist, because hibernate only creates a table structure and does not create a database. If the specified database does not exist, Hibernate throws an exception -->
<Property name = "hbm2ddl. Auto"> Update </property>

<Mapping Resource = "com/hzucmj/hibernate/model/student. HBM. xml"/>
<! -- Note that if you use annotation, you should write it as <mapping class = "com. hzucmj. hibernate. Student"/>
-->
</Session-factory>

</Hibernate-configuration>

 

Create another Java file, studenttest. Java, and test whether the connection is successful. The Code is as follows:

 

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.hzucmj.hibernate.model.Student;


public class StudentTest {
public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setAge(23);
s.setName("hzucmj");

Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}

}

 

If the configuration is successful, the following information appears in the debugging window:

 

Hibernate: insert into student (name, age, ID) values (?, ?, ?)

This proves that your configuration is successful.

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.