Home » Java » Hibernate - 01 - Manual

Hibernate - 01 - Manual

1.hibernate.cfg.xml


<, XML, version=' 1.0' encoding=' UTF-8'?
"-//Hibernate/Hibernate, Configuration, DTD, 3.0//EN.""
"Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >



oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@localhost:1521:orcl
scott
root
statement
true
statement
true
dialect database
org.hibernate.dialect.OracleDialect





2.Student.hbm.xml


<, XML, version=' 1.0' encoding=' UTF-8'?
"-//Hibernate/Hibernate, Mapping, DTD, 3.0//EN.""
"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >


key




The relationship between field and attribute -- configuration




3.crud

package com.ai;
Import org.hibernate.Session;
Import org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;
Import com.ai.entity.Student;
Public, class, TestSave {
Public, static, void, main (String[], args) {
Student, s=new, Student ();
S.setSid (3);
S.setSname (Li four);
S.setSage (19);
S.setSscore (67);
//1. load configuration file
Configuration, cfg=new, Configuration (),.Configure ();
//2. gets the connection object factory
SessionFactory, factory=cfg.buildSessionFactory ();
//3. gets a connection
Session, session=factory.openSession ();
//4. opens a transaction
Transaction, ts=session.beginTransaction ();
//5. implementation additions and deletions to change
Session.save (s);
//6. commit transaction
Ts.commit ();
((.Commit)) / / session.getTransaction;
//7. closes the connection
Session.close ();
}
}




Latest