==========hibernate方法=====openinview模式下========更新删除要先从数据库获得对象才能操作==========
--更新:方法一----------------------------------
public Integer merge(Auction d) {
String hql="update Auction set auctionname=:auctionname ,auctionstartprice=:auctionstartprice" +
" ,auctiondesc=:auctiondesc ," +
"auctionupset=:auctionupset ,auctionstarttime=:auctionstarttime" +
" ,auctionendtime=:auctionendtime " +
"where auctionid=:auctionid";
Session session=HibernateSessionFactory.getSession();
Query query=session.createQuery(hql);
query.setProperties(d);
int i=query.executeUpdate();
return i;}
---更新:方法二----------------------------------
public void merge(Auction d) {
Session session=HibernateSessionFactory.getSession();
session.update(d);
}
==hql语句查询=================================使用内联查询特定用户已拍卖完成的商品
public List<Auction> selectAuction(Auctionuser user) throws Exception {
Session session = HibernateUtil.currentSession();
String hql = " select a from Auction a inner join a.auctionrecords r " +
"where a.auctionendtime<? and r.auctionuser.userid=?";
Query query=session.createQuery(hql);
query.setTimestamp(0,new java.sql.Timestamp(new java.util.Date().getTime()));
query.setInteger(1, user.getUserid());
Set set=new HashSet(query.list());
List<Auction> list=new ArrayList(set);
return list;
}
============================================HibernateUtil用于建立和关闭session
public class HibernateUtil {
// 初始化一个ThreadLocal对象
private static final ThreadLocal sessionTL = new ThreadLocal();
private static Configuration configuration;
private final static SessionFactory sessionFactory;
static {
try {
configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static Session currentSession() {
//sessionTL的get()方法根据当前线程返回其对应的线程内部变量,
//也就是我们需要的Session,多线程情况下共享数据库连接是不安全的。
//ThreadLocal保证了每个线程都有自己的Session。
Session session = (Session) sessionTL.get();
// 如果session为null,则打开一个新的session
if (session == null) {
//创建一个数据库连接对象session。
session = sessionFactory.openSession();
// 保存该数据库连接session到ThreadLocal中。
sessionTL.set(session);
}
//如果当前线程已经访问过数据库了,
//则从sessionTL中get()就可以获取该线程上次获取过的数据库连接对象。
return session;
}
/**
* 关闭Session
*/
public static void closeSession(){
Session session = (Session) sessionTL.get();
sessionTL.set(null);
session.close();
}
}
--更新:方法一----------------------------------
public Integer merge(Auction d) {
String hql="update Auction set auctionname=:auctionname ,auctionstartprice=:auctionstartprice" +
" ,auctiondesc=:auctiondesc ," +
"auctionupset=:auctionupset ,auctionstarttime=:auctionstarttime" +
" ,auctionendtime=:auctionendtime " +
"where auctionid=:auctionid";
Session session=HibernateSessionFactory.getSession();
Query query=session.createQuery(hql);
query.setProperties(d);
int i=query.executeUpdate();
return i;}
---更新:方法二----------------------------------
public void merge(Auction d) {
Session session=HibernateSessionFactory.getSession();
session.update(d);
}
==hql语句查询=================================使用内联查询特定用户已拍卖完成的商品
public List<Auction> selectAuction(Auctionuser user) throws Exception {
Session session = HibernateUtil.currentSession();
String hql = " select a from Auction a inner join a.auctionrecords r " +
"where a.auctionendtime<? and r.auctionuser.userid=?";
Query query=session.createQuery(hql);
query.setTimestamp(0,new java.sql.Timestamp(new java.util.Date().getTime()));
query.setInteger(1, user.getUserid());
Set set=new HashSet(query.list());
List<Auction> list=new ArrayList(set);
return list;
}
============================================HibernateUtil用于建立和关闭session
public class HibernateUtil {
// 初始化一个ThreadLocal对象
private static final ThreadLocal sessionTL = new ThreadLocal();
private static Configuration configuration;
private final static SessionFactory sessionFactory;
static {
try {
configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static Session currentSession() {
//sessionTL的get()方法根据当前线程返回其对应的线程内部变量,
//也就是我们需要的Session,多线程情况下共享数据库连接是不安全的。
//ThreadLocal保证了每个线程都有自己的Session。
Session session = (Session) sessionTL.get();
// 如果session为null,则打开一个新的session
if (session == null) {
//创建一个数据库连接对象session。
session = sessionFactory.openSession();
// 保存该数据库连接session到ThreadLocal中。
sessionTL.set(session);
}
//如果当前线程已经访问过数据库了,
//则从sessionTL中get()就可以获取该线程上次获取过的数据库连接对象。
return session;
}
/**
* 关闭Session
*/
public static void closeSession(){
Session session = (Session) sessionTL.get();
sessionTL.set(null);
session.close();
}
}