我在META-INF中创建了一个persistence.xml:
<?xml version =1.0encoding =UTF-8?> < persistence version =1.0 xmlns =java.sun/xml/ns/persistence xmlns:xsi =http:// www。 w3/2001/XMLSchema-instance xsi:schemaLocation =java.sun/xml/ns/persistence java.sun/xml/ NS /持久性/ persistence_1_0.xsd> < persistence-unit name =myPUtransaction-type =JTA> < provider> org.eclipse.persistence.jpa.PersistenceProvider< / provider> < exclude-unlisted-classes> false< / exclude-unlisted-classes> <属性> < property name =javax.persistence.jdbc.drivervalue =org.apache.derby.jdbc.ClientDataSource/> <! - org.apache.derby.jdbc.EmbeddedDriver - > < property name =javax.persistence.jdbc.urlvalue =jdbc:derby:// localhost:1527 / sample; create = true/> < property name =javax.persistence.jdbc.uservalue =APP/> < property name =javax.persistence.jdbc.passwordvalue =APP/> < property name =eclipselink.ddl-generationvalue =create-tables/> < / properties> < / persistence-unit> < /余辉>然后,我在资源中创建了一个字段,用于访问/存储som数据:
@PersistenceUnit(unitName =myPU) EntityManagerFactory emf;但是emf始终为NULL: - (
)我猜我的persistence.xml没有配置好。
如果有人有提示,我做错了什么, / p>
谢谢!
解决方案这里是相应的配置: $ ul
因此,您必须在src文件夹中创建文件夹META-INF,并将persistence.xml放在该文件夹中:
<?xml version =1.0encoding =UTF-8?> < persistence version =1.0 xmlns =java.sun/xml/ns/persistence xmlns:xsi =www.w3/2001/XMLSchema-instance xsi:schemaLoc ation =java.sun/xml/ns/persistence java.sun/xml/ns/persistence/persistence_1_0.xsd\"> < persistence-unit name =myPUtransaction-type =JTA> < provider> org.eclipse.persistence.jpa.PersistenceProvider< / provider> < jta-data-source> jdbc / __ default< / jta-data-source> < exclude-unlisted-classes> false< / exclude-unlisted-classes> <属性> < property name =eclipselink.ddl-generationvalue =drop-and-create-tables/> < / properties> < / persistence-unit> < /余辉>我之前在WebContent的META-INF中创建了.xml,这是错误的。 您也不需要引用任何额外的库,因为您已经添加了glassfish模块。
现在我创建了一个JavaBean,在那里注入PersistenceUnit:
@Stateless public class StorageService { @PersistenceContext(unitName = myPU) EntityManager em; ... }这个注入在Jersey-Servlets的资源类中:
@Path(/ someres) @Produces( MediaType.APPLICATION_XML) @Stateless public class SomeRes { @EJB StorageService storageService; }注射只能起作用如果这些类标记为@Stateless。
I am very new to glassfish, JPA and so on and I have really problems with setting that up. What I am planning to do is a simple RESTful service with a persistent backend. I am using glassfish3 as application server and already deployed a simple REST service with the jersey-library. Now I want to provide access to a database via JPA. Glassfish is shipped with JavaDB/derby and EclipseLink, is that right? So, I want to use that :-)
I created a persistence.xml in META-INF:
<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0" xmlns="java.sun/xml/ns/persistence" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="java.sun/xml/ns/persistence java.sun/xml/ns/persistence/persistence_1_0.xsd"> <persistence-unit name="myPU" transaction-type="JTA"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDataSource" /> <!-- org.apache.derby.jdbc.EmbeddedDriver --> <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/sample;create=true" /> <property name="javax.persistence.jdbc.user" value="APP" /> <property name="javax.persistence.jdbc.password" value="APP" /> <property name="eclipselink.ddl-generation" value="create-tables" /> </properties> </persistence-unit> </persistence>Then I created a field in my resource, where I want to access/store som data:
@PersistenceUnit(unitName = "myPU") EntityManagerFactory emf;But "emf" is always NULL :-(
I guess that my persistence.xml is not configured appropriate.
Would be really glad if someone has a hint, what I am doing wrong...
thanks!
解决方案I have the solution now for my problem. Here is the corresponding configuration:
- glassfish 3.1.1
- built-in JavaDB/derby database: jdbc/__default
- glassfish's JPA, which is eclipselink
- (JAX RS: Jersey, which is shipped with glassfish)
So, you have to create the folder "META-INF" wihtin your src folder and put the persistence.xml there:
<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0" xmlns="java.sun/xml/ns/persistence" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="java.sun/xml/ns/persistence java.sun/xml/ns/persistence/persistence_1_0.xsd"> <persistence-unit name="myPU" transaction-type="JTA"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <jta-data-source>jdbc/__default</jta-data-source> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> </properties> </persistence-unit> </persistence>I created the .xml previously in the META-INF of WebContent, and that is wrong. You also do not have to reference any additional libraries, since you have the glassfish modules added.
Now I have created a JavaBean, where I do inject the PersistenceUnit:
@Stateless public class StorageService { @PersistenceContext(unitName = "myPU") EntityManager em; ... }And this one is injected in my Resource-Classes of the Jersey-Servlets:
@Path("/someres") @Produces(MediaType.APPLICATION_XML) @Stateless public class SomeRes { @EJB StorageService storageService; ... }The injections do only work if the classes are marked as "@Stateless".
更多推荐
GlassFish 3.1.1的persistence.xml
发布评论