- db table
CREATE TABLE Dbuser
(
USER_ID NUMBER(5) NOT NULL,
USERNAME VARCHAR2(20) ,
CREATED_BY VARCHAR2(20) ,
CREATED_DATE DATE
)
- hibernate.cfg.xml
oracle.jdbc.OracleDriver
*****
jdbc:oracle:thin:@DbServer:port:sid
***
org.hibernate.dialect.Oracle10gDialect
true
true
- Dbuser.hbm.xml
- POJO
package pojo.oneConfig;
import java.util.Date;
public class Dbuser {
private int userId;
private String username;
private String createdBy;
private Date createdDate;
public Dbuser() {
}
public Dbuser(int userId) {
this.userId = userId;
}
public Dbuser(String name, int id) {
this.userId = id;
this.username = name;
}
public Dbuser(int userId, String username, String createdBy, Date createdDate) {
this.userId = userId;
this.username = username;
this.createdBy = createdBy;
this.createdDate = createdDate;
}
//getter and setter
}
- HibernateUtil
package config.hibernate.oneConfig;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
- Test case
package main.oneConfig;
import java.util.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import config.hibernate.oneConfig.HibernateUtil;
import pojo.oneConfig.Dbuser;
public class Main {
static SessionFactory factory;
public static void main(String[] args) {
factory = HibernateUtil.getSessionFactory();
Dbuser dbuser = new Dbuser();
dbuser.setCreatedBy("yz20537");
dbuser.setCreatedDate(new Date());
dbuser.setUserId(202);
dbuser.setUsername("hahaha");
// add(dbuser);
// query();
// update(201, "aaaa");
// delete(201);
// testHql();
testCriteria();
// testNativeSql();
}
static void add(Dbuser dbuser) {
Session session = factory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.save(dbuser);
transaction.commit();
} catch (Exception ex) {
ex.printStackTrace();
transaction.rollback();
} finally {
if (session != null) {
session.close();
}
}
}
static void query() {
Session session = factory.openSession();
try {
// List result = session.createQuery("from Dbuser").list();
List result = session.createSQLQuery("select * from dbuser").addEntity(Dbuser.class).list();
for (Object object : result) {
Dbuser dbuser = (Dbuser) object;
System.out.println(String.format("%s %s", dbuser.getUserId(), dbuser.getUsername()));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
}
static void update(int id, String name) {
Session session = factory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Dbuser dbuser = (Dbuser) session.get(Dbuser.class, id);
dbuser.setUsername(name);
session.update(dbuser);
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
} finally {
if (session != null) {
session.close();
}
}
}
static void delete(int id) {
Session session = factory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Dbuser dbuser = (Dbuser) session.get(Dbuser.class, id);
session.delete(dbuser);
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
} finally {
if (session != null) {
session.close();
}
}
}
static void testHql() {
Session session = factory.openSession();
try {
/*Query query = session.createQuery("from Dbuser");
List result = query.list();
for (Object object : result) {
System.out.println(((Dbuser) object).getUsername());
}*/
/*Query query = session.createQuery("select u.username from Dbuser as u");
List result = query.list();
for (Object object : result) {
System.out.println(object.toString());
}*/
/*Query query = session.createQuery("select u.username,u.userId from Dbuser as u");
List
改成注解的版本:
- hibernate.cfg.xml
oracle.jdbc.OracleDriver
*****
jdbc:oracle:thin:@DbServer:port:sid
***
org.hibernate.dialect.Oracle10gDialect
true
true
- POJO
package pojo.oneAnnotation;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "Dbuser")
public class Dbuser {
@Id
@Column(name = "USER_ID")
private int userId;
@Column(name = "USERNAME")
private String username;
@Column(name = "CREATED_BY")
private String createdBy;
@Column(name = "CREATED_DATE")
private Date createdDate;
public Dbuser() {
}
public Dbuser(int userId) {
this.userId = userId;
}
public Dbuser(String name, int id) {
this.userId = id;
this.username = name;
}
public Dbuser(int userId, String username, String createdBy, Date createdDate) {
this.userId = userId;
this.username = username;
this.createdBy = createdBy;
this.createdDate = createdDate;
}
//getter and setter
}