我必須對我的一個項目使用JPA項目,但一旦我創建一個EntityManager,在我的SessionBean調用persistencecontect我有此錯誤:問題引用JPA
@Stateless
public class Biblio implements BiblioLocal {
@PersistenceContext(unitName="BiblioJPA")
private EntityManager em;
/**
* Default constructor.
*/
public Biblio() {
// TODO Auto-generated constructor stub
}
@Override
public String succes() {
// TODO Auto-generated method stub
return " beta";
}
public void toevoegenBoek(){
Boeken boek = new Boeken();
boek.setTitel("harry P.");
boek.setAuteur("rowling");
em.persist(boek);
}
}
我的servlet(它調用一個來自這個sessionbean的方法)將不會再加載,並給我這個很長的錯誤,我不明白任何東西。有任何想法嗎? (沒有我servlet加載的entitymanager和persistencecontext,但後來我無法調用與jpa相關的任何函數!)
謝謝!
錯誤運行servlet當我得到:
javax.servlet.ServletException: Error instantiating servlet class servlets.BiblioServlet
org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
java.lang.Thread.run(Unknown Source)
root cause
java.lang.RuntimeException: Unable to inject jndi dependency: env/servlets.BiblioServlet/biblio into property servlets.BiblioServlet.biblio: BiblioEAR not bound
org.jboss.injection.JndiPropertyInjector.lookup(JndiPropertyInjector.java:82)
org.jboss.injection.JndiPropertyInjector.inject(JndiPropertyInjector.java:99)
org.jboss.web.tomcat.service.TomcatInjectionContainer.processInjectors(TomcatInjectionContainer.java:366)
org.jboss.web.tomcat.service.TomcatInjectionContainer.newInstance(TomcatInjectionContainer.java:271)
org.jboss.web.tomcat.service.TomcatInjectionContainer.newInstance(TomcatInjectionContainer.java:265)
org.jboss.web.tomcat.service.TomcatInjectionContainer.newInstance(TomcatInjectionContainer.java:256)
org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
java.lang.Thread.run(Unknown Source)
root cause
javax.naming.NamingException: Could not dereference object [Root exception is javax.naming.NameNotFoundException: BiblioEAR not bound]
org.jnp.interfaces.NamingContext.resolveLink(NamingContext.java:1352)
org.jnp.interfaces.NamingContext.lookup(NamingContext.java:817)
org.jnp.interfaces.NamingContext.lookup(NamingContext.java:686)
org.jboss.ejb3.JndiUtil.lookup(JndiUtil.java:44)
org.jboss.injection.JndiPropertyInjector.lookup(JndiPropertyInjector.java:75)
org.jboss.injection.JndiPropertyInjector.inject(JndiPropertyInjector.java:99)
org.jboss.web.tomcat.service.TomcatInjectionContainer.processInjectors(TomcatInjectionContainer.java:366)
org.jboss.web.tomcat.service.TomcatInjectionContainer.newInstance(TomcatInjectionContainer.java:271)
org.jboss.web.tomcat.service.TomcatInjectionContainer.newInstance(TomcatInjectionContainer.java:265)
org.jboss.web.tomcat.service.TomcatInjectionContainer.newInstance(TomcatInjectionContainer.java:256)
org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
java.lang.Thread.run(Unknown Source)
root cause
javax.naming.NameNotFoundException: BiblioEAR not bound
org.jnp.server.NamingServer.getBinding(NamingServer.java:771)
org.jnp.server.NamingServer.getBinding(NamingServer.java:779)
org.jnp.server.NamingServer.getObject(NamingServer.java:785)
org.jnp.server.NamingServer.lookup(NamingServer.java:396)
org.jnp.interfaces.NamingContext.lookup(NamingContext.java:726)
org.jnp.interfaces.NamingContext.lookup(NamingContext.java:686)
javax.naming.InitialContext.lookup(Unknown Source)
org.jnp.interfaces.NamingContext.resolveLink(NamingContext.java:1346)
org.jnp.interfaces.NamingContext.lookup(NamingContext.java:817)
org.jnp.interfaces.NamingContext.lookup(NamingContext.java:686)
org.jboss.ejb3.JndiUtil.lookup(JndiUtil.java:44)
org.jboss.injection.JndiPropertyInjector.lookup(JndiPropertyInjector.java:75)
org.jboss.injection.JndiPropertyInjector.inject(JndiPropertyInjector.java:99)
org.jboss.web.tomcat.service.TomcatInjectionContainer.processInjectors(TomcatInjectionContainer.java:366)
org.jboss.web.tomcat.service.TomcatInjectionContainer.newInstance(TomcatInjectionContainer.java:271)
org.jboss.web.tomcat.service.TomcatInjectionContainer.newInstance(TomcatInjectionContainer.java:265)
org.jboss.web.tomcat.service.TomcatInjectionContainer.newInstance(TomcatInjectionContainer.java:256)
org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
java.lang.Thread.run(Unknown Source)
在我的控制檯(在Eclipse)錯誤
DEPLOYMENTS MISSING DEPENDENCIES:
Deployment "jboss.j2ee:ear=BiblioEAR.ear,jar=BiblioEJB.jar,name=Biblio,service=EJB3" is missing the following dependencies:
Dependency "<UNKNOWN jboss.j2ee:ear=BiblioEAR.ear,jar=BiblioEJB.jar,name=Biblio,service=EJB3>" (should be in state "Described", but is actually in state "** UNRESOLVED Demands 'persistence.unit:unitName=BiblioEAR.ear/BiblioJPA.jar#BiblioJPA' **")
Deployment "jboss.j2ee:ear=BiblioEAR.ear,jar=BiblioEJB.jar,name=Biblio,service=EJB3_endpoint" is missing the following dependencies:
Dependency "jboss.j2ee:ear=BiblioEAR.ear,jar=BiblioEJB.jar,name=Biblio,service=EJB3" (should be in state "Configured", but is actually in state "PreInstall")
DEPLOYMENTS IN ERROR:
Deployment "persistence.unit:unitName=BiblioEAR.ear/BiblioJPA.jar#BiblioJPA" is in error due to the following reason(s): org.xml.sax.SAXParseException: cvc-complex-type.3.1: Value '2.0' of attribute 'version' of element 'entity-mappings' is not valid with respect to the corresponding attribute use. Attribute 'version' has a fixed value of '1.0'.
Deployment "<UNKNOWN jboss.j2ee:ear=BiblioEAR.ear,jar=BiblioEJB.jar,name=Biblio,service=EJB3>" is in error due to the following reason(s): ** UNRESOLVED Demands 'persistence.unit:unitName=BiblioEAR.ear/BiblioJPA.jar#BiblioJPA' **
編輯:
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="BiblioJPA" transaction-type="JTA">
<jta-data-source>java:/MySqlDSAlfa</jta-data-source>
</persistence-unit>
</persistence>
mysql-ds。 XML
<datasources>
<local-tx-datasource>
<jndi-name>MySqlDSAlfa</jndi-name>
<connection-url>jdbc:mysql://localhost:8080/database</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password></password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
嘿,我覺得真的很愚蠢,但似乎你是對的,我在我的數據庫名稱拼寫錯誤在我的mysql-ds.xml文件。 (它現在已經修復,名稱確實是「數據庫」)但是它仍然無法正常工作,所以我正在更新我的錯誤消息並添加了xml文件。感謝您的幫助! – networkprofile 2010-07-31 00:55:42
@Sled:不客氣。但是你真的更新了錯誤信息嗎? – 2010-07-31 01:09:05
嘿,是的,我做了(這是我目前得到的錯誤和「JPA無法查找數據源」消失了)。我還添加了控制檯錯誤.. – networkprofile 2010-07-31 08:23:16