Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of persistence.xml and hibernate.cfg.xml

I use JPA 2.0 + Hibernate 4.3.4

my 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>

my 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>

Currently I get exception:

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"}}

Questions:

  1. Do I need both xmls to establish connection?
  2. If I don't need hibernate.cfg.xml how should persistence.xml look like?
like image 588
Rudziankoŭ Avatar asked Jun 24 '16 11:06

Rudziankoŭ


People also ask

What is the use of persistence xml in Hibernate?

xml File. This file is used to override the default Hibernate settings and to add support for database types that are not out of the box (OOB database types are Oracle Server, Microsoft SQL Server, and MySQL).

What is persistent xml?

The persistence. xml file is a standard configuration file in JPA. It has to be included in the META-INF directory inside the JAR file that contains the entity beans.

What is the role of the persistence xml file in JPA?

The persistence. xml configuration file is used to configure a given JPA Persistence Unit. The Persistence Unit defines all the metadata required to bootstrap an EntityManagerFactory , like entity mappings, data source, and transaction settings, as well as JPA provider configuration properties.

What is Hibernate xml?

Hibernate facilitates to provide the configurations either in an XML file (like hibernate. cfg. xml) or properties file (like hibernate. properties). An instance of Configuration class allows specifying properties and mappings to applications.


1 Answers

<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. You can do this with only persistence.xml file.

  2. persistence.xml should looks like code above

like image 142
J.Arranz Avatar answered Sep 22 '22 07:09

J.Arranz