?? hibernate.java
字號:
package mrgf.other;
import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;
import java.util.List;
public class Hibernate {
public Hibernate() {
}
//
private static SessionFactory sessionFactory;
static {
try {
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
} catch (Exception e) {
System.out.println("------在初始化hibernate時拋出異常,內容如下:");
e.printStackTrace();
}
}
private Session session = null;
private Transaction tx = null;
//打開session,開啟事務
private void openSession() {
// System.out.println("------開啟session,打開事務:");
session = sessionFactory.openSession();
tx = session.beginTransaction();
}
//關閉session,關閉事務
private void closeSession() {
// System.out.println("------提交事務,關閉session:");
if (tx != null) {
tx.commit();
}
if (session != null) {
session.close();
}
}
//檢索所有對象
public List query(String hql) {
this.openSession();
List result = null;
try {
Query query = session.createQuery(hql);
result = query.list();
} catch (Exception e) {
System.out.println("------在檢索對象時拋出異常,內容如下:");
e.printStackTrace();
}
this.closeSession();
return result;
}
//檢索一個對象
public Object queryOne(String hql) {
this.openSession();
Object obj = null;
try {
obj = (Object) session.createQuery(hql).setMaxResults(1).
uniqueResult();
} catch (Exception e) {
System.out.println("------在檢索對象時拋出異常,內容如下:");
e.printStackTrace();
}
this.closeSession();
return obj;
}
//檢索對象通過SQL
public List queryWithSql(String sql) {
this.openSession();
List result = null;
try {
Query query = session.createSQLQuery(sql);
result = query.list();
} catch (Exception e) {
System.out.println("------在檢索對象時拋出異常,內容如下:");
e.printStackTrace();
}
this.closeSession();
return result;
}
//檢索對象部分屬性
public Object[] querySomeAttribute(String sql) {
this.openSession();
Object[] result = null;
try {
Query query = session.createSQLQuery(sql);
result = query.getReturnTypes();
} catch (Exception e) {
System.out.println("------在檢索對象時拋出異常,內容如下:");
e.printStackTrace();
}
this.closeSession();
return result;
}
//刪除對象
public void delete(Object object) {
this.openSession();
try {
session.delete(object);
} catch (Exception e) {
System.out.println("------在刪除對象時拋出異常,內容如下:");
e.printStackTrace();
}
this.closeSession();
}
//刪除對象通過SQL
public void delete(String hql) {
this.openSession();
try {
Query query = session.createQuery(hql);
List objects = query.list();
for (int i = 0; i < objects.size(); i++) {
Object object = (Object) objects.get(i);
session.delete(object);
}
} catch (Exception e) {
tx.rollback();
System.out.println("------在刪除對象時拋出異常,內容如下:");
e.printStackTrace();
}
this.closeSession();
}
//持久化對象
public boolean save(Object object) {
this.openSession();
boolean hasSave = true;
try {
session.save(object);
} catch (Exception e) {
hasSave = false;
System.out.println("------在持久化對象時拋出異常,內容如下:");
e.printStackTrace();
}
this.closeSession();
System.out.println(hasSave);
return hasSave;
}
//更新對象
public void update(Object object) {
this.openSession();
try {
session.update(object);
} catch (Exception e) {
System.out.println("------在更新對象時拋出異常,內容如下:");
e.printStackTrace();
}
this.closeSession();
}
//更新對象
public void saveOrUpdate(Object object) {
this.openSession();
try {
session.saveOrUpdate(object);
} catch (Exception e) {
System.out.println("------在更新對象時拋出異常,內容如下:");
e.printStackTrace();
}
this.closeSession();
}
//取字段最大值
public String getMax(String sql) {
this.openSession();
String max = null;
try {
max = (String) session.createSQLQuery(sql).uniqueResult();
} catch (Exception e) {
System.out.println("------在取字段最大值時拋出異常,內容如下:");
e.printStackTrace();
} finally {
this.closeSession();
}
return max;
}
//統計信息
public int count(String hql) {
this.openSession();
Long c = new Long(0);
try {
c = (Long) session.createQuery(hql).uniqueResult();
} catch (Exception e) {
System.out.println("------在取字段最大值時拋出異常,內容如下:");
e.printStackTrace();
} finally {
this.closeSession();
}
return c.intValue();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -