作者: | 来源:互联网 | 2023-10-09 19:47
packagecn.hb.test;importorg.hibernate.SessionFactory;importorg.hibernate.Transaction;impo
package cn.hb.test;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.junit.Test;
import cn.hb.entitys.User;
public class UserTest {
public static void main(String[] args) {
getUser();
}
@Test
public static void getUser(){
Configuration config = new Configuration().configure();
SessionFactory factory = config.buildSessionFactory();
Session session = factory.getCurrentSession();
Transaction tc = null;
try{
tc = session.beginTransaction();
User user = (User)session.get(User.class, 5);
System.out.println(user.getUid() + "\t" + user.getUname() +
"\t" + user.getUpwd() + "\t" + user.getUaddress());
tc.commit();
}catch (Exception e) {
if(tc!=null){
tc.rollback();
}
}
}
}
1 个解决方案
你使用的是junit来测试,不需要在方法体上添加static。或者你直接在类里面执行main方法。