Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Hibernate learning introductory tutorial     - Ubuntu How to mount iso file (Linux)

- Offline (local) Yum source structures (Linux)

- grep search from files and display the file name (Linux)

- Oracle delete table space prompted ORA-00604 and ORA-38301 (Database)

- Vim useful plugin: YouCompleteMe (Linux)

- Open source backup software installed on Ubuntu Systemback 1.6.201 (Linux)

- mysql_config_editor encryption and decryption of the new features of MySQL realization (Database)

- Character Encoding notes: ASCII, Unicode and UTF-8 (Linux)

- hexdump Linux command (Linux)

- Ubuntu uses the / etc / profile file to configure the JAVA environment variable (Linux)

- Linux vi command list (Linux)

- Linux Getting Started tutorial: Ubuntu 14.04 in the installation Sogou Pinyin (Linux)

- Debian 8.1 (amd64) deployed Memcached (Server)

- MariaDB 10 Multi-source replication (Database)

- Ubuntu 15.10 / 14.04 install subtitling software Aegisub (Linux)

- Ubuntu modify DNS restart loss problem (Linux)

- CentOS 6 rapid deployment of Java applications (Linux)

- OpenGL Superb Learning Notes - GLSL language foundation (Programming)

- RedHat Linux 6.4 install Oracle 10g error (Database)

- Fedora 22 users to install the VLC media player (Linux)

  Hibernate learning introductory tutorial
  Add Date : 2018-11-21      
  In fact, a couple of months ago in terms of knowledge of Hibernate, but has been no good summary, and has been using myeclipse, feeling some fool-style operation can be erected Hibernate development environment, but so little not good, do not understand in the end is how to Hibernate configuration, so you special use today to build step by step Hibernate Eclipse development environment, following the formal entry to the topic.

Create a new web project, whose name it whatever you like what the name what it

Introduced hibernate dependent jar package, I am using hibernate-release-4.3.10.Final, After downloading unpack, require open file under the lib directory under the archive folder, which is to hibernate So must the jar package Next, in just the new project in a new libs folder you just said jar package copy into the other, because we need to connect to the MySQL database and the use of JUnit test, the required mysql-connector-java- 5.0.8-bin.jar and junit-4.5.jar into two jar package references about these jar package, you can search online. Next, is to add these jar package to the build environment to go, check the jar under the package libs, right click and select Build Path -> Add to Build Path, so put the dependent jar package successfully added into it.

Continue down, we need to configure the most important hibernate configuration file hibernate.cfg.xml well log4j.properties property file log processing: Open the hibernate file step unpacked folder, open the project-> etc folder, the file folder under the hibernate.cfg.xml and log4j.properties files are copied to the project src folder, opens the hibernate.cfg.xml file to replace the session-factory tag content into the following topics:

< Session-factory>
        < ! - Configure mysql database connection parameters ->
        < Property name = "hibernate.dialect"> org.hibernate.dialect.MySQLDialect < / property>
        < ! - Driver Name ->
        < Property name = "hibernate.connection.driver_class"> com.mysql.jdbc.Driver < / property>
        < ! - Database name ->
        < Property name = "hibernate.connection.url"> jdbc: mysql: /// hibernatedemo < / property>
        < ! - Username ->
        < Property name = "hibernate.connection.username"> root < / property>
        < ! - Password ->
        < Property name = "hibernate.connection.password"> yzp140103 < / property>
    < / Session-factory>

This is configured.

Next thing to do is do development test: a new entity class com.joe.entity package in the src directory of the project, a new entity class Student in the packet, as follows:


package com.joe.entity;

import java.io.Serializable;

public class Student implements Serializable {
    / **
    * /
    private static final long serialVersionUID = 5548279324472937805L;
    private int id;
    private String name;
    private int age;
    / **
    * Affirming no-argument constructor
    * /
    public Student () {
    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 int getAge () {
        return age;
    public void setAge (int age) {
        this.age = age;



By the way, note, you must declare a no-argument constructor, this is a must Oh!


The key question is, we are not saying do hibernate data persistence it? That is to say the one hundred and interact with the database chant for Student class, relational database and how it, then we have to talk about this object-relational mapping files Xxx.hbm.xml files, where Xxx is the name of the entity class, the contents of our Student.hbm.xml file is:


< ? Xml version = "1.0"?>
< ! DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
< Hibernate-mapping>
    < -! Tag a class corresponding to an entity class, name attribute specifies the name of the entity class, table attribute specifies the table associated with the database ->
    < Class name = "com.joe.entity.Student" table = "stu_tab">
        < ! - Primary key ->
        < Id name = "id" column = "stu_id">
            < ! - The primary key generation strategy ->
            < Generator class = "native"> < / generator>
        < / Id>
        < ! - Other attributes, name corresponding to the attributes of the entity type, column corresponding column of relational database tables ->
        < Property name = "name" column = "stu_name"> < / property>
        < Property name = "age" column = "stu_age"> < / property>
    < / Class>
< / Hibernate-mapping>


The file also added to the com.joe.entity package, so the list? Of course the answer is no, we have to register the entity class to hibernate.cfg.xml, after session-factory label below the last add a property tag:

< ! - Add Student.hbm.xml mapping files ->
        < Mapping resource = "com / joe / entity / Student.hbm.xml" />

Then go down, we have to test, create a new resource in the project folder test, and then test the new file in the test package com.joe.test, StudentTest.java a new class in this package, and write the following code:


package com.joe.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

import com.joe.entity.Student;

public class StudentTest {
    / **
    * @Test Comment is way junit4 test affirms
    * Generate DDL files based on object-relational mapping
    * /
    public void createTable () {
        Configuration cfg = new Configuration () configure ().;
        SchemaExport se = new SchemaExport (cfg);
        se.create (true, true);


Of course, you can also use the following ways to generate DDL, the new asking price in the hibernate.cfg.xml:

< ! - Generated DDL Configuration ->
        < Property name = "hibernamr.hbm2ddl.auto"> create | update < / property>

Finally, the implementation StudentTest.java category, select Run As-> JUnit Test, the console can see the following information, it would prove a success

Of course, you can also open MySQL, further validation.

Well, hibernate development environment even if formally completed structures.
- The basic method RHEL6 (CentOS6) used in the source package compiled RPM: Upgrade OpenSSH articles (Linux)
- XtraBackup achieve non-stop use of master-slave synchronization service (Database)
- Ubuntu 14.04 install PostgreSQL 9.2 (Database)
- Linux crontab commands and detailed usage examples (Linux)
- The direct insertion sort algorithm (Programming)
- Linux command line under HTTP traffic sniffing tool: httpry (Linux)
- Sublime Text 3 using summary (Linux)
- Android project using the command to create and install the package (Programming)
- Linux uses the SMTP proxy to send mail (Linux)
- Ceph single / multi-node installation summary Powered by CentOS 6.x (Server)
- How to write a new Git protocol (Linux)
- CentOS 6.5 dual card configuration, one of the external network, a local area network connection (Linux)
- RHEL (RedHat) 6.3 using EPEL Yum source (Linux)
- Heartbeat cluster components Overview (Server)
- Subsequent binary search tree traversal sequence (Programming)
- Ubuntu 14.04 LTS installed Hadoop 1.2.1 (distributed cluster mode) (Server)
- Binary Tree Traversal (Linux)
- To use iostat display I / O status under Linux (Linux)
- CentOS ClamAV antivirus package updates (Linux)
- Getting Started with Linux system to learn: how to install autossh (Linux)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.