Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Hibernate learning introductory tutorial     - C # C ++ Java interface type conversion (Programming)

- About Nginx 7 (Server)

- Let CentOS perform PPPoE dial-up, ADSL can be used in a network environment! (Linux)

- JavaScript basic tools list (Programming)

- Linux Powerful IDE - Geany configuration instructions (Linux)

- Preview function to confirm the stop resource Oracle 12c new feature crsctl (Database)

- Ubuntu 14.04 running ASP.NET Configuration Mono + Jexus (Server)

- Four levels to deal with Linux server attacks (Linux)

- Talking about modern programming language syntax and standard library tightly bound phenomenon (Programming)

- MySQL high availability cluster fragmentation of deployment uses Cobar (Database)

- Linux system installation Gitlab (Server)

- CentOS 6.4 installation environment to build Scrapy 0.22 (Linux)

- RedHat Linux 6 desktop installation (Linux)

- How to make Linux a non-root user uses less than 1024 ports (Linux)

- How Mutt mail client to use cipher text password (Linux)

- iOS custom keyboard emoji expression (Programming)

- [Errno 4] IOError: [Errno ftp error] with yum appears as a workaround (Linux)

- Setting Hibernate connection MySQL database connection pool coding (Database)

- HTML5 Application Cache (Programming)

- VMware virtual machine to use bridged mode fast Internet access (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
    * /
    @Test
    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.
     
         
         
         
  More:      
 
- Oracle 12C truncate table cascade (Database)
- C ++ runtime environment built on CentOS 6.6 and Oracle database connection (Database)
- No password on Oracle and MySQL login (Database)
- CentOS7 install MySQL5.6.22 (Linux)
- 10 Regulation of painless SQL Schema (Database)
- Ubuntu 12.04 configure NVIDIA CUDA 5.5 Record (Linux)
- JavaScript cross-browser event object library (Programming)
- MySQL Tutorial: Some optimized for I / O memory areas (Database)
- Oracle archive log full cause abnormal slow database performance (Database)
- Linux System Tutorial: How to Check MariaDB server versions (Database)
- Nginx log cutting and MySQL script regular backup script (Server)
- Upload the project to GitHub, synchronous remote repository Github (Linux)
- Android Studio Clear Project or Rebuild Project Error (Linux)
- Java uses JDBC connect database (Programming)
- Android and OpenCV2.4.4 build an image processing framework (2013 edition) (Linux)
- Objective-C basic program structure (Programming)
- Linux Defensive / mitigate DDOS attacks (Linux)
- The principle Httpclient4.4 (HttpClient Interface) (Programming)
- Java threads in the life cycle (Programming)
- Oracle table Access Control (Database)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.