JBoss Hibernate 与 SessionFactory 和 JNDI 冲突
我正在 JBoss AplicationServer 上使用 ICEFaces 和 Hibernate 启动一个项目,现在我陷入了让 Hibernate 工作的困境。
问题是我的 DAO 类无法获取 SassionFacade。 我认为 Hibernate 没有创建一个数据库,因为它与 JNDI 存在相同的冲突。
我到目前为止所做的事情:
使用 MySQL 设置数据库。我使用了本教程: https://community.jboss.org/wiki/DataSourceConfigurationInAS7
(我安装了 JDBC 驱动程序作为模型)不太确定它是否有效,因为当服务器启动时,看起来他正在加载其他驱动程序
18:30:26,980 INFO [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 26) JBAS010403: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
比我几乎使用了本教程的第一部分:
http://www.packtpub.com/article/developing -applications-with-jboss-and-hibernate-1
它看起来好像它工作了,我看到我的数据库和表格,但标志会话工厂是空的并向我显示这个警告:
2012-01-26 18:36:25,177 WARN Worker-14 org.hibernate.impl.SessionFactoryObjectFactory - Could not bind factory to JNDIjavax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getNameParser(Unknown Source)
at org.hibernate.util.NamingHelper.bind(NamingHelper.java:75)
at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:113)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:378)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
at org.hibernate.console.ConsoleConfiguration$4.execute(ConsoleConfiguration.java:268)
at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:63)
at org.hibernate.console.ConsoleConfiguration.execute(ConsoleConfiguration.java:107)
at org.hibernate.console.ConsoleConfiguration.buildSessionFactory(ConsoleConfiguration.java:263)
at org.hibernate.eclipse.console.workbench.LazySessionFactoryAdapter.getChildren(LazySessionFactoryAdapter.java:43)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.getChildren(BasicWorkbenchAdapter.java:100)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.fetchDeferredChildren(BasicWorkbenchAdapter.java:106)
at org.eclipse.ui.progress.DeferredTreeContentManager$1.run(DeferredTreeContentManager.java:235)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
我记得在同一点我有相同的会话工厂,机器人不记得我改变了什么,他们消失了。
因为这只是一个警告,所以我继续学习教程并成功生成了我的表和 DAO 的类,然后我编写了一个小测试程序并使用了其中一个 DAO,并收到以下错误:
18:42:44,232 SCHWERWIEGEND [db.fassaden.hibernate.PlattenHome] (http--127.0.0.1-8080-3) Could not locate SessionFactory in JNDI: javax.naming.NameNotFoundException: SessionFactory -- service jboss.naming.context.java.SessionFactory
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:92)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:177)
at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:87)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:213)
at javax.naming.InitialContext.lookup(Unknown Source) [:1.6.0_17]
at db.fassaden.hibernate.PlattenHome.getSessionFactory(PlattenHome.java:26) [classes:]
at db.fassaden.hibernate.PlattenHome.<init>(PlattenHome.java:22) [classes:]
at fassadenkalkulation.kalkulation.testhibernate(kalkulation.java:21) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_17]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [:1.6.0_17]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [:1.6.0_17]
at java.lang.reflect.Method.invoke(Unknown Source) [:1.6.0_17]
at org.apache.el.parser.AstValue.invoke(AstValue.java:262) [jbossweb-7.0.7.Final.jar:]
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) [jbossweb-7.0.7.Final.jar:]
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at javax.faces.component.UICommand.broadcast(UICommand.java:300) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.7.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.7.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.7.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.7.Final.jar:]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:151) [jboss-as-web-7.1.0.CR1b.jar:7.1.0.CR1b]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.7.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.7.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.7.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [jbossweb-7.0.7.Final.jar:]
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:897) [jbossweb-7.0.7.Final.jar:]
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:626) [jbossweb-7.0.7.Final.jar:]
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:2033) [jbossweb-7.0.7.Final.jar:]
at java.lang.Thread.run(Unknown Source) [:1.6.0_17]
18:42:44,237 SCHWERWIEGEND [javax.faces.event] (http--127.0.0.1-8080-3) Beim Aufrufen des Aktionszielgeräts 'java.lang.IllegalStateException' für Komponente '#{kalkulation.testhibernate}' wurde '_t8' erhalten. ...
这是独立的服务器。 xml
<datasources> <datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS">
<connection-url>
jdbc:mysql://localhost:3306/fassaden
</connection-url>
<driver>
com.mysql
</driver>
<transaction-isolation>
TRANSACTION_READ_COMMITTED
</transaction-isolation>
<pool>
<min-pool-size>
10
</min-pool-size>
<max-pool-size>
100
</max-pool-size>
<prefill>
true
</prefill>
</pool>
<security>
<user-name>
root
</user-name>
<password>
1taste
</password>
</security>
<statement>
<prepared-statement-cache-size>
32
</prepared-statement-cache-size>
<share-prepared-statements>
true
</share-prepared-statements>
</statement>
</datasource>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>
jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
</connection-url>
<driver>
h2
</driver>
<security>
<user-name>
sa
</user-name>
<password>
sa
</password>
</security>
</datasource>
<drivers>
<driver name="com.mysql" module="com.mysql">
<xa-datasource-class>
com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
</xa-datasource-class>
</driver>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>
org.h2.jdbcx.JdbcDataSource
</xa-datasource-class>
</driver>
</drivers>
</datasources>
我的 hibernate 配置 (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">1taste</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/fassaden</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>
从 Hibernate 生成的 DAO (PlattenHome.java)
package db.fassaden.hibernate;
// Generated 26.01.2012 15:18:18 by Hibernate Tools 3.4.0.CR1
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
/**
* Home object for domain model class Platten.
* @see db.fassaden.hibernate.Platten
* @author Hibernate Tools
*/
public class PlattenHome {
private static final Log log = LogFactory.getLog(PlattenHome.class);
private final SessionFactory sessionFactory = getSessionFactory();
protected SessionFactory getSessionFactory() {
try {
return (SessionFactory) new InitialContext().lookup("SessionFactory");
}
catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException("Could not locate SessionFactory in JNDI");
}
}
...
public Platten findById( java.lang.Integer id) {
log.debug("getting Platten instance with id: " + id);
try {
Platten instance = (Platten) sessionFactory.getCurrentSession()
.get("db.fassaden.hibernate.Platten", 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 void testhibernate(ActionEvent e){
PlattenHome pdao = new PlattenHome();
Platten platte = new Platten();
platte = pdao.findById(1);
this.setName(platte.getMaterial());
System.out.println("test");
System.out.println(platte.getMaterial());
}
我刚刚注意到,如果我在服务器启动后立即重建 hibernate 配置,它会给我另一个警告
2012-01-26 18:48:55,359 WARN main org.hibernate.impl.SessionFactoryObjectFactory - Could not unbind factory from JNDI
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.unbind(Unknown Source)
at org.hibernate.impl.SessionFactoryObjectFactory.removeInstance(SessionFactoryObjectFactory.java:139)
at org.hibernate.impl.SessionFactoryImpl.close(SessionFactoryImpl.java:981)
at org.hibernate.console.ConsoleConfiguration.closeSessionFactory(ConsoleConfiguration.java:411)
at org.hibernate.console.ConsoleConfiguration.reset(ConsoleConfiguration.java:130)
at org.hibernate.eclipse.console.views.ReloadConfigurationAction.doRun(ReloadConfigurationAction.java:34)
at org.hibernate.eclipse.console.actions.ConsoleConfigurationBasedAction.run(ConsoleConfigurationBasedAction.java:65)
at org.eclipse.ui.actions.BaseSelectionListenerAction.runWithEvent(BaseSelectionListenerAction.java:168)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2696)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2660)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
at org.eclipse.equinox.launcher.Main.main(Main.java:1386)
I am starting a project using ICEFaces and Hibernate on a JBoss AplicationServer and now I am stuck getting Hibernate to work.
The problem is that my DAO classes cant get the SassionFacade.
I think Hibernate is not creating one because of same conflicts with the JNDI
What I did so far:
setting up my database with MySQL. I used this tutorial: https://community.jboss.org/wiki/DataSourceConfigurationInAS7
(I installed the JDBC driver as a model) not so sure if it worked because when the server starts it looks like he is loading a other driver
18:30:26,980 INFO [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 26) JBAS010403: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
Than I pretty much used the first side of this tutorial:
http://www.packtpub.com/article/developing-applications-with-jboss-and-hibernate-1
and it looks liked it worked, i see my database and the tables but the flag Session Factory is empty and show me this warning:
2012-01-26 18:36:25,177 WARN Worker-14 org.hibernate.impl.SessionFactoryObjectFactory - Could not bind factory to JNDIjavax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getNameParser(Unknown Source)
at org.hibernate.util.NamingHelper.bind(NamingHelper.java:75)
at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:113)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:378)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
at org.hibernate.console.ConsoleConfiguration$4.execute(ConsoleConfiguration.java:268)
at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:63)
at org.hibernate.console.ConsoleConfiguration.execute(ConsoleConfiguration.java:107)
at org.hibernate.console.ConsoleConfiguration.buildSessionFactory(ConsoleConfiguration.java:263)
at org.hibernate.eclipse.console.workbench.LazySessionFactoryAdapter.getChildren(LazySessionFactoryAdapter.java:43)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.getChildren(BasicWorkbenchAdapter.java:100)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.fetchDeferredChildren(BasicWorkbenchAdapter.java:106)
at org.eclipse.ui.progress.DeferredTreeContentManager$1.run(DeferredTreeContentManager.java:235)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
I remember that at same point I had same Session Factorys there bot don't remember what I changed that they diassapeard.
Because it is just a warning I was going on with the tutorial and successfully generated the classes to my table and the DAOs than I was writing a little test program and used one of the DAOs and get the following error:
18:42:44,232 SCHWERWIEGEND [db.fassaden.hibernate.PlattenHome] (http--127.0.0.1-8080-3) Could not locate SessionFactory in JNDI: javax.naming.NameNotFoundException: SessionFactory -- service jboss.naming.context.java.SessionFactory
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:92)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:177)
at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:87)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:213)
at javax.naming.InitialContext.lookup(Unknown Source) [:1.6.0_17]
at db.fassaden.hibernate.PlattenHome.getSessionFactory(PlattenHome.java:26) [classes:]
at db.fassaden.hibernate.PlattenHome.<init>(PlattenHome.java:22) [classes:]
at fassadenkalkulation.kalkulation.testhibernate(kalkulation.java:21) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_17]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [:1.6.0_17]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [:1.6.0_17]
at java.lang.reflect.Method.invoke(Unknown Source) [:1.6.0_17]
at org.apache.el.parser.AstValue.invoke(AstValue.java:262) [jbossweb-7.0.7.Final.jar:]
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) [jbossweb-7.0.7.Final.jar:]
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at javax.faces.component.UICommand.broadcast(UICommand.java:300) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT]
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.7.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.7.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.7.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.7.Final.jar:]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:151) [jboss-as-web-7.1.0.CR1b.jar:7.1.0.CR1b]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.7.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.7.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.7.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [jbossweb-7.0.7.Final.jar:]
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:897) [jbossweb-7.0.7.Final.jar:]
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:626) [jbossweb-7.0.7.Final.jar:]
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:2033) [jbossweb-7.0.7.Final.jar:]
at java.lang.Thread.run(Unknown Source) [:1.6.0_17]
18:42:44,237 SCHWERWIEGEND [javax.faces.event] (http--127.0.0.1-8080-3) Beim Aufrufen des Aktionszielgeräts 'java.lang.IllegalStateException' für Komponente '#{kalkulation.testhibernate}' wurde '_t8' erhalten. ...
This is the Servers standalone.xml
<datasources> <datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS">
<connection-url>
jdbc:mysql://localhost:3306/fassaden
</connection-url>
<driver>
com.mysql
</driver>
<transaction-isolation>
TRANSACTION_READ_COMMITTED
</transaction-isolation>
<pool>
<min-pool-size>
10
</min-pool-size>
<max-pool-size>
100
</max-pool-size>
<prefill>
true
</prefill>
</pool>
<security>
<user-name>
root
</user-name>
<password>
1taste
</password>
</security>
<statement>
<prepared-statement-cache-size>
32
</prepared-statement-cache-size>
<share-prepared-statements>
true
</share-prepared-statements>
</statement>
</datasource>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>
jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
</connection-url>
<driver>
h2
</driver>
<security>
<user-name>
sa
</user-name>
<password>
sa
</password>
</security>
</datasource>
<drivers>
<driver name="com.mysql" module="com.mysql">
<xa-datasource-class>
com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
</xa-datasource-class>
</driver>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>
org.h2.jdbcx.JdbcDataSource
</xa-datasource-class>
</driver>
</drivers>
</datasources>
My hibernate configuration (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">1taste</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/fassaden</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>
The from Hibernate generated DAO (PlattenHome.java)
package db.fassaden.hibernate;
// Generated 26.01.2012 15:18:18 by Hibernate Tools 3.4.0.CR1
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
/**
* Home object for domain model class Platten.
* @see db.fassaden.hibernate.Platten
* @author Hibernate Tools
*/
public class PlattenHome {
private static final Log log = LogFactory.getLog(PlattenHome.class);
private final SessionFactory sessionFactory = getSessionFactory();
protected SessionFactory getSessionFactory() {
try {
return (SessionFactory) new InitialContext().lookup("SessionFactory");
}
catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException("Could not locate SessionFactory in JNDI");
}
}
...
public Platten findById( java.lang.Integer id) {
log.debug("getting Platten instance with id: " + id);
try {
Platten instance = (Platten) sessionFactory.getCurrentSession()
.get("db.fassaden.hibernate.Platten", 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;
}
}
and how I use it:
public void testhibernate(ActionEvent e){
PlattenHome pdao = new PlattenHome();
Platten platte = new Platten();
platte = pdao.findById(1);
this.setName(platte.getMaterial());
System.out.println("test");
System.out.println(platte.getMaterial());
}
I just noticed that if I rebuild the hibernate configuration now after the server has started it gives me a other Warning
2012-01-26 18:48:55,359 WARN main org.hibernate.impl.SessionFactoryObjectFactory - Could not unbind factory from JNDI
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.unbind(Unknown Source)
at org.hibernate.impl.SessionFactoryObjectFactory.removeInstance(SessionFactoryObjectFactory.java:139)
at org.hibernate.impl.SessionFactoryImpl.close(SessionFactoryImpl.java:981)
at org.hibernate.console.ConsoleConfiguration.closeSessionFactory(ConsoleConfiguration.java:411)
at org.hibernate.console.ConsoleConfiguration.reset(ConsoleConfiguration.java:130)
at org.hibernate.eclipse.console.views.ReloadConfigurationAction.doRun(ReloadConfigurationAction.java:34)
at org.hibernate.eclipse.console.actions.ConsoleConfigurationBasedAction.run(ConsoleConfigurationBasedAction.java:65)
at org.eclipse.ui.actions.BaseSelectionListenerAction.runWithEvent(BaseSelectionListenerAction.java:168)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2696)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2660)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
at org.eclipse.equinox.launcher.Main.main(Main.java:1386)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我找到了解决方案。
删除了 hibernate.cfg.xml 中的名称
我现在
,以便 hibernate 创建自己的 SessionFacade 并且不要尝试使用 JNDI 之一。
我将 DAO 更改为:
ok I found a solution.
I removed the name in the hibernate.cfg.xml
now is
so hibernate creates its own SessionFacade and dont try to use the one of the JNDI.
The DAO I changed to:
我还要注意到,在我从配置文件中删除 name 属性后,我仍然在日志中看到“org.hibernate.internal.SessionFactoryRegistry addSessionFactory WARN:HHH000277:无法将工厂绑定到 JNDI”消息。问题原来是我在 CLASSPATH 中缺少一些必需的 jar 文件(例如 antlr-2.7.6.jar)。这导致了一个没有记录的“Throwable”,我认为警告就是问题所在。添加缺少的 jar 后,hibernate 4 在带有 JNDI 的 Java 独立(非托管)应用程序中运行良好。
I would also note that I still saw "org.hibernate.internal.SessionFactoryRegistry addSessionFactory WARN: HHH000277: Could not bind factory to JNDI" message in the logs after I removed the name attribute from the configuration file. The problem turned out to be that I was missing some of the required jars in the CLASSPATH (e.g. antlr-2.7.6.jar). This caused a "Throwable" that was no logged and I assumed the warning was the problem. Once I added the missing jars, hibernate 4 worked fine in a Java stand-alone (non-managed) application with JNDI.