Java 这个错误意味着什么?我如何修复它?
我正在使用由Hibernate工具生成的DAO类:Java 这个错误意味着什么?我如何修复它?,java,hibernate,orm,Java,Hibernate,Orm,我正在使用由Hibernate工具生成的DAO类: /** * Home object for domain model class Empleados. * @see com.hibernate.Empleados * @author Hibernate Tools */ public class EmpleadosHome { private static final Log log = LogFactory.getLog(EmpleadosHome.class);
/**
* Home object for domain model class Empleados.
* @see com.hibernate.Empleados
* @author Hibernate Tools
*/
public class EmpleadosHome {
private static final Log log = LogFactory.getLog(EmpleadosHome.class);
private final SessionFactory sessionFactory = getSessionFactory();
protected SessionFactory getSessionFactory() {
try {
return (SessionFactory) new InitialContext()
.lookup("java:/hibernate/SessionFactory");
} catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException(
"Could not locate SessionFactory in JNDI");
}
}
public void persist(Empleados transientInstance) {
log.debug("persisting Empleados instance");
try {
sessionFactory.getCurrentSession().persist(transientInstance);
log.debug("persist successful");
} catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}
}
public void attachDirty(Empleados instance) {
log.debug("attaching dirty Empleados instance");
try {
sessionFactory.getCurrentSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Empleados instance) {
log.debug("attaching clean Empleados instance");
try {
sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void delete(Empleados persistentInstance) {
log.debug("deleting Empleados instance");
try {
sessionFactory.getCurrentSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Empleados merge(Empleados detachedInstance) {
log.debug("merging Empleados instance");
try {
Empleados result = (Empleados) sessionFactory.getCurrentSession()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public Empleados findById(java.lang.Integer id) {
log.debug("getting Empleados instance with id: " + id);
try {
Empleados instance = (Empleados) sessionFactory.getCurrentSession()
.get("com.hibernate.Empleados", id);
if (instance == null) {
log.debug("get successful, no instance found");
} else {
log.debug("get successful, instance found");
}
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Empleados instance) {
log.debug("finding Empleados instance by example");
try {
List results = sessionFactory.getCurrentSession().createCriteria(
"com.hibernate.Empleados").add(Example.create(instance))
.list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
}
这是我的hibernate.cfg.xml
文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="SessionFactory">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3307/ejemplos</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="connection.autocommit">true</property>
<property name="current_session_context_class">jta</property>
<mapping resource="com/hibernate/Empleados.hbm.xml" />
</session-factory>
</hibernate-configuration>
签出此页面:
看起来可能是appserver的配置问题签出此页面:
看起来这可能是appserver的配置问题,我不确定您的环境是否支持JTA和自动事务处理(至少当前的设置不支持)。实际上,如果您不打算访问多个事务性资源,我的建议是使用默认的JDBC方法,即修改
hibernate.cfg.xml
,如下所示:
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
org.hibernate.transaction.jdbc事务工厂
线
并重新生成代码(我对Hibernate工具生成的DAO没有太多经验,我不能保证结果)。我不确定您的环境是否支持JTA和自动事务处理(至少,不支持您当前的设置)。实际上,如果您不打算访问多个事务性资源,我的建议是使用默认的JDBC方法,即修改
hibernate.cfg.xml
,如下所示:
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
org.hibernate.transaction.jdbc事务工厂
线
并重新生成代码(我对Hibernate工具生成的DAO没有太多经验,我不能保证结果)。用openSession()替换getCurrentSession()用openSession()替换getCurrentSession()欢迎使用StackOverflow!要发布代码,请将所有代码缩进四个空格。编辑时,请参阅右侧的格式提示列表,和/或阅读格式帮助:(编辑:看起来像是Pointy为您修复了它,但下次…)欢迎使用StackOverflow!要发布代码,请将所有代码缩进四个空格。编辑时,请参阅右侧的格式提示列表,和/或阅读格式帮助:(编辑:看起来像是Pointy为您修复了它,但下次使用…)好的,我使用jboss as 5.1,我想它可以管理事务,从dao类中完全删除任何事务划分。……好的,我使用jboss as 5.1,我想它可以管理事务,从dao类中完全删除任何事务划分。。。。