public class HibernateUtil {
private static Configuration cfg;
private static ServiceRegistry sr;
private static SessionFactory sf;
static {
try {
cfg = new Configuration();
cfg.configure();
sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
sf = cfg.buildSessionFactory(sr);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static Configuration getConfiguration() {
return cfg;
}
public static SessionFactory getSessionFactory() {
return sf;
}
public static Session getSession() throws HibernateException {
return sf.getCurrentSession();
}
}public class EcrPartsDaoImpl extends HibernateUtil implements EcrPartsDao {
//添加数据
public boolean addData(EcrParts ecr) {
boolean flag = false;
Transaction tx = null;
try {
tx = getSession().beginTransaction();
getSession().save(ecr);
tx.commit();
flag = true;
} catch (Exception e) {
// TODO: handle exception
tx.rollback();
e.printStackTrace();
} finally {
getSessionFactory().close();
}
return flag;
}
}一到tx = getSession().beginTransaction();这里就出现异常了。不知道怎么回事,求解
private static Configuration cfg;
private static ServiceRegistry sr;
private static SessionFactory sf;
static {
try {
cfg = new Configuration();
cfg.configure();
sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
sf = cfg.buildSessionFactory(sr);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static Configuration getConfiguration() {
return cfg;
}
public static SessionFactory getSessionFactory() {
return sf;
}
public static Session getSession() throws HibernateException {
return sf.getCurrentSession();
}
}public class EcrPartsDaoImpl extends HibernateUtil implements EcrPartsDao {
//添加数据
public boolean addData(EcrParts ecr) {
boolean flag = false;
Transaction tx = null;
try {
tx = getSession().beginTransaction();
getSession().save(ecr);
tx.commit();
flag = true;
} catch (Exception e) {
// TODO: handle exception
tx.rollback();
e.printStackTrace();
} finally {
getSessionFactory().close();
}
return flag;
}
}一到tx = getSession().beginTransaction();这里就出现异常了。不知道怎么回事,求解
public class EcrPartsDaoImpl extends HibernateUtil implements EcrPartsDao {
//添加数据
public EcrParts searchData(int id) {
EcrParts ep = null;
Transaction tx = null;
try {
tx = getSession().beginTransaction();
ep = (EcrParts)getSession().get(EcrParts.class,id);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
tx.rollback();
e.printStackTrace();
} finally {
getSessionFactory().close();
}
return ep;
}
}
是执行查询的时候报的异常!之前说错了。报的就是上面的那个异常!不知道怎么回事!
return sf.openSession();
}不知可否。