简体   繁体   English

如何在JavaDB上将JPA与Java EE 7,Glassfish 4.1和Maven结合使用

[英]How to use JPA with Java EE 7, Glassfish 4.1 and Maven on JavaDB

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. 我有一个来自Maven Archetype的简单Web Java EE 7项目(NetBeans:New Project ...-> Maven->来自Archetype的项目-> webapp-javaee 7),我想使用JPA来映射类到数据库表。 So I created a new JavaDB database and created the corresponding Connection Pool and JDBC-Ressource in Glassfish. 因此,我在Glassfish中创建了一个新的JavaDB数据库并创建了相应的连接池和JDBC-Ressource。 Now i generated a very simple Entity-Class, having all the necessary annotations. 现在,我生成了一个非常简单的Entity-Class,具有所有必要的注释。 NetBeans gives me a hint, saying that a persistence unit is not declared, so i created a persistence.xml file as follows: NetBeans给了我一个提示,说未声明持久性单元,因此我创建了一个persistence.xml文件,如下所示:

<?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: Maven自动添加以下依赖项:

<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. 然后,我构建了该项目并将其部署在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. 我非常确定与数据库的连接是可以的,因为它可以与NetBeans附带的那些示例项目一起使用。 So i presume that one needs some extra maven-dependency or some special property in the persistence.xml file. 因此,我认为一个人在persistence.xml文件中需要一些额外的maven依赖性或一些特殊的属性。 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. 从JPA 2.1 / Glassfish 4.1开始,您似乎需要在创建表之前在某个地方使用PU。 I know it was not like this in Glassfish 3.x and I was also a little confused at first. 我知道在Glassfish 3.x中不是这样的,一开始我也有些困惑。 It should be enough to use this code somewhere in your code, ie in an EJB: 在代码中的某个地方(即在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 另请参见以下答案: 实体表不是使用JPA 2.1创建的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM