GlassFish 3.1.1的persistence.xml [英] persistence.xml with Glassfish 3.1.1

查看:113
本文介绍了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 =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_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 =http://java.sun.com/xml/ns/persistence
    xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
    xsi:schemaLoc ation =http://java.sun.com/xml/ns/persistence
    http://java.sun.com/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="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_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="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_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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆