GlassFish 3.1.1的persistence.xml

编程入门 行业动态 更新时间:2024-10-22 20:42:31
本文介绍了GlassFish 3.1.1的persistence.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我对玻璃鱼,JPA等非常陌生,在设置这些方面我遇到了很多问题。我打算做的是一个带有持久后端的简单RESTful服务。我使用glassfish3作为应用程序服务器,并且已经使用泽西库部署了一个简单的REST服务。现在我想通过JPA提供对数据库的访问。 Glassfish随JavaDB / derby和EclipseLink提供,是吗?所以,我想使用它: - )

我在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

  • glassfish 3.1.1
  • 内建JavaDB / derby数据库:jdbc / __ default
  • glassfish的JPA,它是eclipselink
  • (JAX RS:Jersey, li>

    因此,您必须在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

    本文发布于:2023-10-25 14:06:19,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:GlassFish   xml   persistence

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!