如何在JavaDB上与Java EE 7,Glassfish 4.1和Maven一起使用JPA [英] How to use JPA with Java EE 7, Glassfish 4.1 and Maven on JavaDB

查看:138
本文介绍了如何在JavaDB上与Java EE 7,Glassfish 4.1和Maven一起使用JPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Maven Archetype获得了一个简单的Web Java EE 7项目(NetBeans:New Project ... - > Maven - > Archetype项目 - > webapp-javaee 7)和
我希望使用JPA为了将类映射到数据库表。
所以我创建了一个新的JavaDB数据库,并在Glassfish中创建了相应的连接池和JDBC-Ressource。
现在我生成了一个非常简单的实体类,它具有所有必要的注释。
NetBeans给了我一个提示,声明持久化单元没有声明,所以我创建了一个persistence.xml文件,如下所示:

 <?xml version =1.0encoding =UTF-8?> 
< persistence version =2.1xmlns =http://xmlns.jcp.org/xml/ns/persistencexmlns:xsi =http://www.w3.org/2001/XMLSchema-实例xsi:schemaLocation =http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd\">
< persistence-unit name =com.mycompany_mavenproject1_war_1.0-SNAPSHOTPUtransaction-type =JTA>
< jta-data-source> jdbc / testDb< / jta-data-source>
<属性>
< property name =javax.persistence.schema-generation.database.actionvalue =create/>
< / properties>
< / persistence-unit>
< /余辉>

Maven自动添加以下依赖项:

 <依赖性> 
< groupId> org.eclipse.persistence< / groupId>
< artifactId> eclipselink< / artifactId>
< version> 2.5.2< / version>
< scope>提供< / scope>
< /依赖关系>
< dependency>
< groupId> org.eclipse.persistence< / groupId>
< artifactId> org.eclipse.persistence.jpa.modelgen.processor< / artifactId>
< version> 2.5.2< / version>
< scope>提供< / scope>
< /依赖关系>

然后我构建了该项目并将其部署到Glassfish上。即使我没有得到任何错误,它也不起作用,这意味着在数据库中没有创建相应的表。

我很确定与数据库的连接是可以的,因为它可以与NetBeans提供的那些样例项目一起工作。
所以我认为在persistence.xml文件中需要一些额外的maven-dependency或一些特殊的属性。
我尝试了一些我可以在互联网上找到的东西,但似乎没有任何工作...

绝对没有错。看起来,从JPA 2.1 / GlassFish 4.1开始,你需要在创建表之前的某个地方使用PU。我知道它在Glassfish 3.x中不是这样,我起初也有点困惑。在代码中的某个地方使用这些代码应该足够了,即在EJB中:

  @PersistenceContext 
private EntityManager EM;

  @PersistenceContext(unitName =com.mycompany_mavenproject1_war_1.0-SNAPSHOTPU)
private EntityManager em;

另请参阅此答案:实体表格不是使用JPA 2.1创建的


I've got a simple Web Java EE 7 project from Maven Archetype (NetBeans: New Project... -> Maven -> Project from Archetype -> webapp-javaee 7) and I'd like to use JPA in order to map classes to database-tables. So I created a new JavaDB database and created the corresponding Connection Pool and JDBC-Ressource in Glassfish. Now i generated a very simple Entity-Class, having all the necessary annotations. NetBeans gives me a hint, saying that a persistence unit is not declared, so i created a persistence.xml file as follows:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="com.mycompany_mavenproject1_war_1.0-SNAPSHOTPU" transaction-type="JTA">
    <jta-data-source>jdbc/testDb</jta-data-source>
    <properties>
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
    </properties>
  </persistence-unit>
</persistence>

Maven automatically added following dependencies:

<dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
        <version>2.5.2</version>
        <scope>provided</scope>
    </dependency>

Then i built the project and deployed it on Glassfish. Even though I don't get any errors it just doesn't work, meaning that no corresponding table is being created in the database.

I'm pretty sure that the connection with the database is ok, because it does work with those Sample Projects which are delivered with NetBeans. So i presume that one needs some extra maven-dependency or some special property in the persistence.xml file. I tried just about anything i could find on the internet but nothing seems to work...

解决方案

You are doing absolutely nothing wrong. It seems that since JPA 2.1 / Glassfish 4.1 you need to use your PU somewhere before the tables are created. I know it was not like this in Glassfish 3.x and I was also a little confused at first. It should be enough to use this code somewhere in your code, i.e. in an EJB:

@PersistenceContext
private EntityManager em;

or

@PersistenceContext(unitName = "com.mycompany_mavenproject1_war_1.0-SNAPSHOTPU")
private EntityManager em;

See also this answer: Entity Table is not creating using JPA 2.1

这篇关于如何在JavaDB上与Java EE 7,Glassfish 4.1和Maven一起使用JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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