我初学 jpa,照猫画虎,做了一个 jpa + hibernate 程序片段。但好几天了,数据库始终连不上。错误信息如下:(resin 3.1)
Caused by: java.lang.NullPointerException
at com.caucho.amber.manager.AmberPersistenceProvider.createEntityManagerFactory(AmberPersistenceProvider.java:65)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at com.darta.collate.wicket.CollateApplication.init(CollateApplication.java:68)
错误信息表明, Persistence.createEntityManagerFactory 空指针。
本段程序如下:
.......
private EntityManagerFactory emf;
// 构建.
public CollateApplication() {
}
public EntityManagerFactory getEntityManagerFactory() {
return emf;
}
@Override
protected void init() {
super.init();
System.out.println("shell emf");
emf = Persistence.createEntityManagerFactory("collatePU");
System.out.println("********************** emf OK!!");
控制台只显示"shell emf".
jpa 配置文件 persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
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_1_0.xsd" version="1.0">
<persistence-unit name="collatePU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name = "hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name = "hibernate.connection.driver_class" value = "com.mysql.jdbc.Driver"/>
<property name = "hibernate.connection.url" value = "jdbc:mysql://localhost:3306/collate?useUnicode=true&characterEncoding=utf-8"/>
<property name = "hibernate.connection.username" value = "admin"/>
<property name = "hibernate.connection.password" value = "aaaaaaa"/>
<property name = "hibernate.hbm2ddl.auto" value = "update"/>
</properties>
</persistence-unit>
数据库在非 jpa 时可以连通。我想同实体暂时无关吧?代码就不贴了。
衷心请求帮助,万分感谢!
[b]问题补充:[/b]
谢谢lovewhzlq的回答。persistence.xml 文件不是放在 /WEB-INF/classes/META-INF 下吗?程序中还要做什么工作?
我把它发布到 tomcat6 下面,错误信息如下,请帮助看一看:
信息: Deploying web application archive collate.war(这是我的war文件)
shell emf (程序中显示到控制台的)
shell emf (为什么显示两次?)
2009-8-19 18:50:28 org.apache.catalina.core.StandardContext start
严重: Error filterStart
2009-8-19 18:50:28 org.apache.catalina.core.StandardContext start
严重: Context [/collate] startup failed due to previous errors
似乎和 web.xml 文件有关
CollateApplication
org.apache.wicket.protocol.http.WicketFilter
applicationClassName
com.darta.collate.wicket.CollateApplication
<filter-mapping>
<filter-name>CollateApplication</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
问题在那里?
[b]问题补充:[/b]
[b][size=medium]万分感激 lovewhzlq 的热情帮助。[/size][/b]
filter有问题就是
emf = Persistence.createEntityManagerFactory("collatePU");
这一行。去掉它,一切正常。就是 createEntityManagerFactory 出错。
有什么建议?
[b]问题补充:[/b]
[size=medium][b]万分感激 lovewhzlq !!!
我下个教程看看,不清楚的再请教。[/b][/size]
[b]问题补充:[/b]
[size=medium][color=darkred]非常非常感谢 lovewhzlq,按照您推荐的资料,问题解决了。
我过去用过 Hibernate 3.2,照猫画虎成功过一次,但不甚了了。这次用了新版的 3.3.1, 依赖库发生了变化,造成依赖库冲突。为了避免其他朋友和我一样走弯路,现将我可以使用的依赖库发在下面:
antlr-2.7.6.jar
c3p0-0.9.1.jar
cglib-2.2.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
ejb3-persistence.jar
hibernate-annotations.jar 3.4GA
hibernate-commons-annotations.jar 3.4GA
hibernate-entitymanager.jar 3.4GA
hibernate3.jar 3.3.1
javassist-3.9.0.GA.jar
jboss-cache-1.4.1.GA.jar
jbosscache-core-3.1.0.GA.jar
jta-1.1.jar
log4j-1.2.15.jar
oscache-2.1.jar
proxool-0.8.3.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
swarmcache-1.0RC2.jar
可能有些不是必需的,但肯定不冲突。
再次感谢 lovewhzlq 的指点![/color][/size]