【问题标题】:Use of persistence.xml and hibernate.cfg.xmlpersistence.xml 和 hibernate.cfg.xml 的使用
【发布时间】:2016-06-24 11:19:13
【问题描述】:

我使用 JPA 2.0 + Hibernate 4.3.4

我的persistence.xml:

<persistence-unit name="movie-unit">
    <class>com.epam.rudenkov.controller.BookStore</class>

    <properties>
        <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
    </properties>
</persistence-unit>

我的hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">postgres</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</property>

        <property name="connection_pool_size">1</property>

        <property name="hbm2ddl.auto">create</property>

        <property name="show_sql">true</property>

        <mapping class="com.epam.rudenkov.model.Book"/>

    </session-factory>
</hibernate-configuration>

目前我得到异常:

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: movie-unit] Unable to build EntityManagerFactory
Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set"}}

问题:

  1. 是否需要两个 xml 来建立连接?
  2. 如果我不需要 hibernate.cfg.xml,persistence.xml 应该是什么样子?

【问题讨论】:

  • 由于休眠配置文件不在 JPA 规范中,您可以放心地假设您不需要这些东西。

标签: java hibernate jpa


【解决方案1】:
<persistence-unit name="movie-unit">
<class>com.epam.rudenkov.controller.BookStore</class>

<properties>
    <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>

    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.connection.password">postgres</property>
    <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</property>

    <property name="connection_pool_size">1</property>

    <property name="hbm2ddl.auto">create</property>

    <property name="show_sql">true</property>

    <mapping class="com.epam.rudenkov.model.Book"/>
</properties></persistence-unit>
  1. 您只需使用 persistence.xml 文件即可。

  2. persistence.xml 应该类似于上面的代码

【讨论】:

  • 感谢您的回答,无论如何我得到了:DeploymentUnitProcessingException: JBAS011448: Failed to parse ".../META-INF/persistence.xml"
  • persistence.xml 的 XML 格式可能有一些错误。
猜你喜欢
  • 2018-11-22
  • 1970-01-01
  • 1970-01-01
  • 2019-05-07
  • 1970-01-01
  • 2020-08-28
  • 2010-10-03
  • 1970-01-01
  • 2023-03-16
相关资源
最近更新 更多