标签: persistence.xml

用于dev/qa/stage/production的JPA多个持久性单元

我已经为此找到了答案但是却未能找到任何这样的问题来向这个非常适合的社区提问!

  1. 我有一个独立的Java应用程序,可以在许多环境中部署:dev,qa,stage,production.因此,每个环境都有自己的数据源/ db,并且有属性文件根据运行应用程序的环境来管理不同的属性.因此,在我的persistence.xml中,我为dev定义了一个持久性单元.在同一个文件中,我还想为其他环境定义持久性单元.这样做时,Eclipse(Indigo - latest)抱怨如下:"定义多个持久性单元 - 只识别第一个持久性单元".假设我所做的是合法的,这是一个Eclipse问题..任何人都可以确认吗?此外,鉴于我目前的设置,这是最佳实践吗?
  2. 我假设任何标有@Entity注释的实体bean都会被自动获取而不必在persistence.xml文件中明确定义它,如下所示:<class>com.mycompany.model.MyEntityBean</class>.如果我省略在文件中显式包含实体类,则实体bean - 虽然注释 - 会引发错误: "类"com.mycompany.model.MyEntityBean"已映射,但未包含在任何持久性单元中" 我是什么我错了吗?
  3. 我的最后一个问题是关于数据库凭据:最好的做法是将我的数据库凭据以纯文本形式放在persistence.xml文件中吗?有没有更安全的替代方案?

谢谢社区!

ps - 我使用EclipseLink作为JPA供应商而不是它应该重要吗?

这是我的persistence.xml的一个例子

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
    <persistence-unit name="Development">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.mycompany.model.MyEntityBean</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.ibm.db2.jcc.DB2Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:db2://xxxxxxx" />
            <property name="javax.persistence.jdbc.password" value="xxxxxx" />
            <property name="javax.persistence.jdbc.user" value="xxxxxxxx" />
        </properties>
    </persistence-unit>
    <persistence-unit name="QA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.mycompany.model.MyEntityBean</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.ibm.db2.jcc.DB2Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:db2://xxxxxxx" />
            <property name="javax.persistence.jdbc.password" value="xxxxxx" />
            <property name="javax.persistence.jdbc.user" value="xxxxxxxx" />
        </properties> …
Run Code Online (Sandbox Code Playgroud)

jpa persistence.xml eclipselink

7
推荐指数
1
解决办法
6299
查看次数

javax.persistence.PersistenceException:没有名为EntityManager的持久性提供程序

我正在尝试按照我老师的文档提供的信息建立一个简单的jpa 2.0项目.我已经在这几个小时了,但无论我做什么,当我尝试创建一个EntityManagerFactory时,我总是得到这个例外:我发现了很多关于这个例外的类似问题,但没有解决方案,我能够开始工作.我在这做错了什么?

我从Eclipse创建了这个项目(没有命令提示符)

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named course
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
    at message.SaveMessage.main(SaveMessage.java:8)
Run Code Online (Sandbox Code Playgroud)

目录结构

在此输入图像描述

我的persistence.xml

<persistence 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_2_0.xsd"
    version="2.0">
    <persistence-unit name="course" transaction-type="RESOURCE_LOCAL">

        <provider>org.hibernate.ejb.HibernatePersistence</provider>


        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />

            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/StudentDB" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="pasapas2005" />
        </properties>
    </persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)

我的课

package message;

import java.io.Serializable;

import javax.persistence.*;

@Entity
public class Message implements Serializable {

    private long id;
    private …
Run Code Online (Sandbox Code Playgroud)

java hibernate persistence.xml maven

7
推荐指数
1
解决办法
2万
查看次数

为什么EntityManager为空?

在我的Web应用程序中,我在Apache Tomcat(TomEE)/7.0.37服务器上使用OpenJPA.我使用Netbeans自动生成类("实体类来自数据库......"和"会话Bean来自实体类......").在SessionBean(例如UserFacade)我想获得EntityManager:

@Stateless
public class UserFacade extends AbstractFacade<User> {
  @PersistenceContext(unitName = "CollDocPU")
  private EntityManager em;

  @Override
  protected EntityManager getEntityManager() {
    return em;
  }
}
Run Code Online (Sandbox Code Playgroud)

但是当我通过上面的方式得到它时,我得到了null.当我通过:

@Override
protected EntityManager getEntityManager() {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("CollDocPU");
    EntityManager ecm = emf.createEntityManager(); 
    return ecm;
}    
Run Code Online (Sandbox Code Playgroud)

ecm不是null,没关系

我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="CollDocPU" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>model.entity.StudentAddSolution</class>
<class>model.entity.Lecturer</class>
<class>model.entity.Solution</class>
<class>model.entity.Student</class>
<class>model.entity.Course</class>
<class>model.entity.File</class>
<class>model.entity.CourseHasLecturer</class> 
<class>model.entity.Mail</class>
<class>model.entity.StudentAtCourse</class>
<class>model.entity.Roles</class>
<class>model.entity.Task</class>
<class>model.entity.User</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
   <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:11080/myBase?zeroDateTimeBehavior=convertToNull"/>
   <property name="javax.persistence.jdbc.password" value="pass,"/>
   <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
   <property …
Run Code Online (Sandbox Code Playgroud)

java entitymanager openjpa persistence.xml tomee

7
推荐指数
1
解决办法
3万
查看次数

属性文件中的Persistence.xml字段值

求助,我想将我的持久性xml属性值引用到我的db.properties文件中.

这是我的db.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/apsas
jdbc.username=root
jdbc.password=password
Run Code Online (Sandbox Code Playgroud)

这是我当前的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_2_0.xsd"
    version="2.0">
    <persistence-unit name="apsaspu" transaction-type="RESOURCE_LOCAL">
        <provider>
            org.hibernate.ejb.HibernatePersistence
        </provider>
        <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/apsas" />
            <property name="hibernate.connection.username" value="root" />
            <property name="hibernate.connection.password" value="password" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)

我想要做的是将其属性设置为这样

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_2_0.xsd"
    version="2.0">
    <persistence-unit name="apsaspu" transaction-type="RESOURCE_LOCAL">
        <provider>
            org.hibernate.ejb.HibernatePersistence
        </provider>
        <properties>
            <property name="hibernate.connection.driver_class" value="${jdbc.driver}" />
            <property name="hibernate.connection.url" value="${jdbc.url}" />
            <property …
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa properties persistence.xml

7
推荐指数
1
解决办法
6431
查看次数

JPA和toplink create-table如果它们尚不存在?

看起来jpa是让我提出很多问题的东西.

添加了这个

<property name="toplink.ddl-generation" value="create-tables"/>
Run Code Online (Sandbox Code Playgroud)

我的JPA应用程序总是在运行时创建表,这会导致表已存在时出现异常.我希望JPA检查表是否已经存在,如果没有创建它们,但是我找不到上面的属性的值,这样做.

所以如果我把它关闭,有没有办法在某个时候手动告诉JPA创建所有表?

这里更新是我得到的例外

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'tags' already exists
Error Code: 1050
Call: CREATE TABLE tags (ID BIGINT AUTO_INCREMENT NOT NULL, NAME VARCHAR(255), OCCURRENCE INTEGER, PRIMARY KEY (ID))
Run Code Online (Sandbox Code Playgroud)

MySQLSyntaxErrorException?现在这肯定是错的

java orm jpa persistence.xml

6
推荐指数
1
解决办法
8972
查看次数

使用单独的persistence.xml文件进行生产并使用Spring 3.1进行测试

好的,对不起,我几个小时都在寻找答案,但是我为StackOverflow输入了整个问题来冒泡我正在寻找的链接.您可以在此处阅读大量相关信息.


我有一个用Spring Roo创建的Spring项目来使用Hibernate和MySQL.但是,为了进行测试,我想在内存中使用HSQLDB,因为Roo集成测试使用ID(主键)0到10删除数据(而不是使用数据库为他们已创建的数据分配的数据删除数据),这意味着它删除已存在于数据库中的数据,在我的情况下会导致约束违规,然后才能回滚事务.

这有点困难,因为我正在切换整个数据库提供程序,这意味着不同的Hibernate方言以及不同的DDL设置(在生产中验证,在测试中创建 - 丢弃).但它并没有像我期望的那样工作,我为什么难以理解.

如果您知道它为什么不起作用,请说明,即使您没有解决方案.

这是一个Roo项目,我当然正在使用Maven.所以我尝试的第一件事就是拥有一个特定于测试的src/test/resources/META-INF/persistence.xml文件,同样也是一个特定于测试的src/test/resources/META-INF/spring/database.properties文件.这不起作用,因为当我运行mvn test所有内容时,相关消息正在运行

Conflicting persistence unit definitions for name 'persistenceUnit'
Run Code Online (Sandbox Code Playgroud)

为什么mvn test仍然拿起非测试资源?

于是我改名src/test/resources/META-INF/springspring-test并复制applicationContext.xml到它.我将测试类中的上下文配置更改为

@ContextConfiguration(locations = "classpath:/META-INF/spring-test/applicationContext*.xml")

完成(或者我认为)分离,我做了几处编辑spring-test/applicationContext.xml:

更改了属性文件的路径:

<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
Run Code Online (Sandbox Code Playgroud)

<context:property-placeholder location="classpath*:META-INF/spring-test/*.properties"/>
Run Code Online (Sandbox Code Playgroud)

更改了持久性单元的名称:

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="testPersistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

并且我在持久性单元名称中进行了相应的更改 src/test/resources/META-INF/persistence.xml

好吧,好吧,现在没有冲突,但Hibernate失去了实体映射(例如Product实体),我得到:

org.springframework.dao.InvalidDataAccessApiUsageException:
org.hibernate.hql.ast.QuerySyntaxException: Product is not mapped …
Run Code Online (Sandbox Code Playgroud)

testing spring persistence.xml

6
推荐指数
1
解决办法
1万
查看次数

通过没有persistence.xml的maven生成JPA 2.0元模型?

有没有办法通过maven生成JPA 2.0元模型而没有persistence.xml文件.我正在使用eclipselink.

在我的Java EE项目中,我正在做类似以下的工作,因为在这种情况下我有一个persistence.xml.

<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <version>2.0.5</version>
    <executions>
        <execution>
            <id>process</id>
            <goals>
                <goal>process</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
                <!-- there must no line break between the two compiler arguments! -->
                <compilerArguments>-Aeclipselink.persistencexml=${basedir}/src/main/resources/META-INF/persistence.xml</compilerArguments>
                <processors>
                    <processor>org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor</processor>
                </processors>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa.modelgen</artifactId>
            <version>2.3.2</version>
        </dependency>
    </dependencies>
</plugin>
Run Code Online (Sandbox Code Playgroud)

现在我有一个spring项目,它通过spring上下文配置jpa.是创建元模型以创建persistence.xml的唯一方法,还是我可以在spring上下文中保留配置?

spring jpa persistence.xml metamodel

6
推荐指数
1
解决办法
1569
查看次数

Hibernate persistance.xml:由于连接为空而禁用上下文 LOB 创建

我的persistence.xml

<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_2_0.xsd"
  version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.ibm.apiscanner.DTO.BaselineDTO</class>
    <properties>
      <property name="hibernate.connection.driver_class" value="com.ibm.db2.jcc.DB2Driver" />
      <property name="hibernate.connection.url"    value="jdbc:db2://localhost:{PORT}/{DB}" />
      <property name="hibernate.connection.username" value="{user}" />
      <property name="hibernate.connection.password" value="{password}" />
      <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect"/>
      <property name="show_sql" value="true"/>
      <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
    </properties>
  </persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)

我收到以下信息:

Jan 22, 2015 9:16:48 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.DB2Dialect
Jan 22, 2015 9:16:48 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000422: Disabling contextual LOB creation as connection was null
Run Code Online (Sandbox Code Playgroud)

通过基本 JDBC 连接时,可以使用相同的 url、用户和密码组合。

有人有建议吗?

java xml hibernate jpa persistence.xml

5
推荐指数
1
解决办法
1万
查看次数

没有 persistence.xml 的 JPA

我正在尝试开始使用 Guice Persist 和 JPA,它建议通过 persistence.xml 使用配置。来自以编程方式获取配置的本机 Hibernate 背景,是否有一种简单的方法可以在没有 persistence.xml 文件的情况下配置 JpaPersistModule,或者是否始终必须存在 rump persistence.xml?

如果不存在这样的选项,那么我可能不得不使用 PersistenceProvider (假设“默认”以某种方式解析 persistence.xml )。有关于使用 JPA SPI 的教程吗?

java orm hibernate jpa persistence.xml

5
推荐指数
1
解决办法
7488
查看次数

JPA 2.1 不生成模式脚本

我使用 Hibernate 启动 JPA 2.1,并在 persistence.xml 中包含以下几行

<property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.scripts.create-target" value="META-INF/sampleCreate.ddl"/>
<property name="javax.persistence.schema-generation.scripts.drop-target" value="META-INF/sampleDrop.ddl"/>
Run Code Online (Sandbox Code Playgroud)

这些表生成到我的 postgres 数据库中。

但是,我的META-INF文件夹中没有sampleCreate.dll,也没有错误。

这里缺少什么?是生成到其他文件夹了吗?

谢谢。

hibernate jpa persistence.xml jpa-2.1

5
推荐指数
1
解决办法
3063
查看次数