Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Hibernate4 The Hello World (basic environmental structures)     - High-performance JavaScript reflows and repaints (Programming)

- Installation Elementary OS Freya 20 things to do (Linux)

- Ubuntu Series Installation Docker (Linux)

- Check the Linux server performance with ten or so commands in a minute (Server)

- crontab task scheduling Health Check (Linux)

- Ubuntu modify locale problem solving (Linux)

- Copy U disk files to the Linux system on a virtual machine (Linux)

- Install Oracle JDK 8 and JVM class loading mechanism in Linux (Linux)

- MySQL monitoring tools -orzdba (Database)

- Linux environmental performance data acquisition system (Linux)

- Linux System Getting Started Tutorial: Five image processing software (Linux)

- CentOS6.5 install SVN & visual management tools iF.SVNAdmin (Server)

- GDB remote connections RX Probe online debug program (Programming)

- Ubuntu 14.10 installation GNOME 3.14 (Linux)

- Acquaintance C ++: the most simple C ++ program (Programming)

- Ubuntu Install OpenSSL (Linux)

- Linux mention the right notes (Linux)

- VMware difference in three network connection (Linux)

- Learning Linux coding style (Programming)

- Linux System Getting Started Tutorial: How to change the default Java version in Linux (Linux)

 
         
  Hibernate4 The Hello World (basic environmental structures)
     
  Add Date : 2018-11-21      
         
         
         
  At first review Hibernate, just reviewed, we found all forgotten, and even the environment to build will not equal all over again ah, no way to bite the bullet and had to start all over again.

Hibernate is an excellent ORM framework, object relation mapping object-relational mapping. My understanding is automatically put into operation pojo class of objects in the corresponding operations on the database tables. It simply is to create a pojo class object, then the corresponding database table will insert such an object. Modify, delete, of course, of the. It is to be understood as isolated java database operation and Development.

First, download the Hibernate jar package

You find the necessary Hibernate version http://www.Hibernate.org/ in. I downloaded the latest Hibernate-release-4.3.1.Final.

Second, copy the jar package

Copy in lib / required to package all jar lib folder of the project under

Third, the deployment Hibernate.cfg.xml, corresponding pojo of Student.hbm.xml

New Hibernate.cfg.xml file in the src directory of the project. In the documentation / manual / en-US / html_single / index.html

< ? 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">

< Hibernate-configuration>

    < Session-factory>

        < -! Database connection settings ->
        < Property name = "connection.driver_class"> Oracle.jdbc.driver.OracleDriver < / property>
        < Property name = "connection.url"> jdbc: oracle: thin: @ 127.0.0.1: 1521: lubby < / property>
        < Property name = "connection.username"> admin < / property>
        < Property name = "connection.password"> admin < / property>

    
        < -! SQL dialect ->
        < Property name = "dialect"> org.hibernate.dialect.Oracle10gDialect < / property>

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

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

        < -! Drop and re-create the database schema on startup ->
        < -! < Property name = "hbm2ddl.auto"> update < / property> ->

        < Mapping resource = "com / lubby / pojo / Student.hbm.xml" />

    < / Session-factory>

< / Hibernate-configuration>

The first four labels are driven configuration database address and user name password.

 < -! SQL dialect ->
        < Property name = "dialect"> org.hibernate.dialect.Oracle10gDialect < / property>

This is the name and version of the configuration database. In the documentation / manual / en-US / html_single / index.html can be found in the corresponding database field corresponding I use the oracle 11g is written above the corresponding

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

This is to choose whether to display the sql statement.

 < Mapping resource = "com / lubby / pojo / Student.hbm.xml" />

This is very important to tell hibernate configuration file configuration file configured pojo class address

Fourth, create a class Student pojo

package com.lubby.pojo;

public class Student {
 private String sid;
 private String sname;
 private int age;
 public Student () {
  super ();
  // TODO Auto-generated constructor stub
 }
 public Student (String sid, String sname, int age) {
  super ();
  this.sid = sid;
  this.sname = sname;
  this.age = age;
 }
 public String getSid () {
  return sid;
 }
 public void setSid (String sid) {
  this.sid = sid;
 }
 public String getSname () {
  return sname;
 }
 public void setSname (String sname) {
  this.sname = sname;
 }
 public int getAge () {
  return age;
 }
 public void setAge (int age) {
  this.age = age;
 }
 @Override
 public String toString () {
  return "Student [sid =" + sid + ", sname =" + sname + ", age =" + age
    + "]";
 }
 
}

Fifth, the configuration Student.hbm.xml

Pojo Each class has a corresponding profile. They are placed inside the package pojo class resides.

< ? Xml version = "1.0"?>
< ! DOCTYPE hibernate-mapping PUBLIC
        "- // Hibernate / Hibernate Mapping DTD 3.0 // EN"
        "Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

< Hibernate-mapping package = "com.lubby.pojo">
 < Class name = "Student" table = "tab_stu">
  < Id name = "sid" column = "sid"> < / id>
  < Property name = "sname" column = "sname"> < / property>
  < Property name = "age" column = "age"> < / property>
 < / Class>
< / Hibernate-mapping>

< Class name = "Student" table = "tab_stu"> < / class>

name: the name of the class

table: the name of the corresponding database table

< Id name = "sid" column = "sid"> < / id>

id: the primary key is set pojo class, column is the primary key of the corresponding database name. If you do not write the column, and the default name of the same content.

< Property name = "sname" column = "sname"> < / property>
< Property name = "age" column = "age"> < / property>

property: Setting the general properties

Sixth, call hibernate

package com.lubby.main;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.jboss.weld.logging.messages.EventMessage;

import com.lubby.pojo.Student;

public class StudentTest {
 
 public static void main (String args []) {
  Student stu = new Student ();
  stu.setSid ( "3");
  stu.setAge (25);
  stu.setSname ( "Li Huitang");
  Configuration cfg = new Configuration ();
  SessionFactory sf = cfg.configure () buildSessionFactory ().;
  Session session = sf.openSession ();
  session.beginTransaction ();
  session.save (stu);
  session.getTransaction () commit ().;
  session.close ();
  sf.close ();
 
 }
}
     
         
         
         
  More:      
 
- Try debugfs restore the deleted files ext3 file system (Linux)
- About Nginx 7 (Server)
- Nginx-1.9.7 TCP reverse proxy (Server)
- 20 Advanced Java interview questions summary (Programming)
- iTerm - let your command line can also be colorful (Linux)
- NFS installation process under the CentOS (Linux)
- Linux Tutorial: Open multiple tabs in the GNOME terminal in Ubuntu 15.04 (Linux)
- Varnish achieve page jump (Server)
- RHEL7.0 log system (Linux)
- Python exception summary (Programming)
- Install the latest development version of Wine on RedHat and Debian-based systems (Linux)
- Linux NFS service fixed ports and firewall configuration (Linux)
- How to prevent page five invisible killer (Linux)
- Examples of Python any parameters (Programming)
- Use IF NOT EXISTS create a data table (Database)
- The oh-my-zsh into true my zsh (Linux)
- crontab cause CPU exception Analysis and Processing (Linux)
- Oracle table compression Technology Introduction (Database)
- Linux file compression and file system packaged with instructions (Linux)
- Ubuntu 14.04 Install WordPress on Nginx (Server)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.