src/com/jiudai/test/HibernateTest.java

(完整代码) 1KB

package com.jiudai.test;

 
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.jiudai.Utils.HibernateUtil;
import com.jiudai.domain.Customer;

public class HibernateTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		List customerList = QueryCustomer();
		
		System.out.println(customerList.size());
	
	}

	static void AddCustomer(Customer customer) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			s.save(customer);
			tx.commit();
		} catch (HibernateException e) {
			if(tx != null)
				tx.rollback();
			throw e;
		} finally {
			HibernateUtil.closeSession();
		} 
	}
	
	static List QueryCustomer(){
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			//Query q = s.createSQLQuery("select t_student.* from t_student,t_class where t_student.student_class_number=t_class.class_number and t_class.class_name like '%3%'").addEntity(Student.class);
			Query q = s.createQuery("From Customer"); 
			List customerList = q.list();
			return customerList;
		} finally {
			HibernateUtil.closeSession();
		}
	}
	
 
}

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。