Hibernate 事务未正常启动????
Exception in thread "main" org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:100)
at app.Test.count(Test.java:77)
at app.Test.main(Test.java:46)
源码如下:
// 统计每种类型产品的数量,包括子类型下面的类型。直到根类型是null
@SuppressWarnings("unchecked")
static void count()
{
Session session = sessionFactory.openSession();
Transaction tran = session.beginTransaction();
Query query = session.createQuery(" from Product");
List<Product> list = query.list();
for (Product p : list)
{
// 输出测试
System.out.println("name:" + p.getName());
ProductType type = p.getType();
// 输出测试
System.out.println("name:" + type.getName());
type.setCount(type.getCount() + 1);
// 更新类型的数量
session.update(type);
ProductType parent = type.getParents();
while (parent != null)
{
// 输出测试
System.out.println("name:" + parent.getName());
parent.setCount(parent.getCount() + 1);
// 更新类型的数量
session.update(type);
parent = parent.getParents();
}
tran.commit();
}
session.close();
}