JPA with pure hibernate

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

JPA with pure hibernate

joch40

Hi,

I am trying to switch my application from a standard tapestry-hibernate version to a tapestry-jpa version. I would like to have a pure hibernate solution and skip any eclipselink dependency.

After spending some time with
                http://tapestry.apache.org/integrating-with-jpa.html
I am more or less lost as the documentation is not working and the comments and remarks from basileChandesris are not really clear.

After I discovered that it seems to be necessary to provide this line:
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
my persistence.xml itself looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
   <persistence-unit name="DemoUnit" transaction-type="RESOURCE_LOCAL">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <properties>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/xxx-test</property>
        <property name="hibernate.connection.username">XX</property>
        <property name="hibernate.connection.password">XXX</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
      </properties>
   </persistence-unit>
</persistence>

As it is not stated in the guide I do not provide a hibernate.cfg.xml file.


The relevant part of the POM file reads:
                               <dependency>
                                               <groupId>org.apache.tapestry</groupId>
                                               <artifactId>tapestry-jpa</artifactId>
                                               <version>${tapestry-release-version}</version>
                               </dependency>

                               <dependency>
                                               <groupId>org.hibernate</groupId>
                                               <artifactId>hibernate-entitymanager</artifactId>
                                               <version>${hibernate-version}</version>
                                               <exclusions>
                                                               <exclusion>
                                                                               <!-- omit Geronimo JPA spec to avoid conflict with Hibernate JPA spec -->
                                                                               <groupId>org.apache.geronimo.specs</groupId>
                                                                               <artifactId>geronimo-jpa_2.0_spec</artifactId>
                                                               </exclusion>
                                               </exclusions>
                               </dependency>


With
                <properties>
                               <tapestry-release-version>5.4.1</tapestry-release-version>
                               <hibernate-version>4.2.6.Final</hibernate-version>
                ....

                </properties>

I use 4.2.6.Finale of hibernate as 4.3.1.Final (what is stated as the correct version for Tapestry 5.4. in
                http://tapestry.apache.org/release-notes-54.html
gives me an error.


AppModule.java  contains:

       @Match("*Dao")
          public static void adviseTransactionally(
                JpaTransactionAdvisor advisor,
                MethodAdviceReceiver receiver) {

             advisor.addTransactionCommitAdvice(receiver);
          }


Although it is not mentioned in

                http://tapestry.apache.org/integrating-with-jpa.html

I also added the following statement in the AppModule.java :

                @Contribute(EntityManagerSource.class)
                   public static void configurePersistenceUnitInfos(MappedConfiguration<String,PersistenceUnitConfigurer> cfg) {

                      PersistenceUnitConfigurer configurer = new PersistenceUnitConfigurer() {
                         public void configure(TapestryPersistenceUnitInfo unitInfo) {
                                unitInfo.addManagedClass(User.class);
                         }
                     };
                     cfg.add("DemoUnit", configurer);
                   }

This crashes the App on start up  with the following Error Massage:
org.apache.tapestry5.ioc.internal.OperationException: javax.persistence.PersistenceException: [PersistenceUnit: DemoUnit] Unable to build EntityManagerFactory

Deep down in the Exception Stack there
Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set

This is all not really helpful at least to me.


If I omit the statement  configurePersistenceUnitInfos ...

The App starts. But as soon as it calls the entityManager in the following class via add(User user);
public class UserDAOImpl implements UserDAO
{
                @PersistenceContext(unitName = "DemoUnit")
     private EntityManager entityManager;

    @Override
    @PersistenceContext(unitName = "DemoUnit")
    public void add(final User user)
    {
        entityManager.persist(user);
    }

    @Override
    @SuppressWarnings(
    { "unchecked" })
    public List<User> findAll()
    {
        return entityManager.createQuery("select u from User u order by u.id desc").getResultList();
    }

    @Override
    public void delete(final User... users)
    {
        for (final User user : users)
            entityManager.remove(user);
    }

    @Override
    public void deleteAll()
    {
        for (final User u : findAll())
        {
            entityManager.remove(u);
        }
    }
}

I get the error message:

Unable to locate a single EntityManager. You must provide ...


From the comments and other posts it is clear that the documentation is outdated and not really working. But there seems to be no really helpful alternative. I would be very grateful if someone could give me a hint where to look or explain to me what is going on here.


Cheers

Janko







Reply | Threaded
Open this post in threaded view
|

Re: JPA with pure hibernate

Dmitry Gusev
Hey,

tapestry-jpa supports latest Hibernate 5.x, at least we have it running in
production with 5.2.5.Final
You don't have to provide hibernate.cfg.xml, although the settings you have
in your persistence.xml are not correct for 5.2.x Hibernate

Here's a working persistence.xml that we use in our apps:

<?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://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="1.0">

    <persistence-unit name="my-pu" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <mapping-file>META-INF/orm.xml</mapping-file>
        <properties>
            <property name="hibernate.hikari.dataSource.url"
value="jdbc:postgresql://localhost/dbname"/>
            <property name="hibernate.hikari.dataSource.user" value="username"/>
            <property name="hibernate.hikari.dataSource.password"
value="password"/>
            <property name="hibernate.hbm2ddl.auto" value="validate"/>
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.connection.provider_class"
value="org.hibernate.hikaricp.internal.HikariCPConnectionProvider"/>
            <property name="hibernate.auth" value="Container" />
            <property name="hibernate.hikari.connectionTimeout"
value="300000" />
            <property name="hibernate.hikari.maximumPoolSize" value="50" />
            <property name="hibernate.hikari.minimumIdle" value="5" />
            <property name="hibernate.hikari.dataSourceClassName"
value="org.postgresql.ds.PGSimpleDataSource" />
        </properties>
    </persistence-unit>
</persistence>

Above configuration uses HikariCP for connection pooling.

Some relevant dependencies:

compile ("org.hibernate:hibernate-core:5.2.5.Final") {
    exclude group: 'commons-collections'

}

compile ("org.hibernate:hibernate-hikaricp:5.2.5.Final") {
    exclude group: "com.zaxxer", module: "HikariCP-java6"
}

compile 'com.zaxxer:HikariCP:2.5.1'

compile "org.postgresql:postgresql:9.4.1212"


The file META-INF/orm.xml has no declarations, not sure if it's still
necessary, but some earlier versions of Hibernate complained without it:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
    version="2.0">
</entity-mappings>


Hope this helps.

On Wed, Jan 18, 2017 at 2:19 PM, Jochimsen, Janko <
[hidden email]> wrote:

>
> Hi,
>
> I am trying to switch my application from a standard tapestry-hibernate
> version to a tapestry-jpa version. I would like to have a pure hibernate
> solution and skip any eclipselink dependency.
>
> After spending some time with
>                 http://tapestry.apache.org/integrating-with-jpa.html
> I am more or less lost as the documentation is not working and the
> comments and remarks from basileChandesris are not really clear.
>
> After I discovered that it seems to be necessary to provide this line:
>    <provider>org.hibernate.ejb.HibernatePersistence</provider>
> my persistence.xml itself looks like this:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
>    <persistence-unit name="DemoUnit" transaction-type="RESOURCE_LOCAL">
>    <provider>org.hibernate.ejb.HibernatePersistence</provider>
>        <properties>
>         <property name="hibernate.connection.driver_class">org.postgresql.
> Driver</property>
>         <property name="hibernate.connection.url">jdbc:postgresql://
> localhost:5432/xxx-test</property>
>         <property name="hibernate.connection.username">XX</property>
>         <property name="hibernate.connection.password">XXX</property>
>         <property name="hibernate.dialect">org.hibernate.dialect.
> PostgreSQLDialect</property>
>         <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
>       </properties>
>    </persistence-unit>
> </persistence>
>
> As it is not stated in the guide I do not provide a hibernate.cfg.xml file.
>
>
> The relevant part of the POM file reads:
>                                <dependency>
>
>  <groupId>org.apache.tapestry</groupId>
>                                                <artifactId>tapestry-jpa</
> artifactId>
>                                                <version>${tapestry-release-
> version}</version>
>                                </dependency>
>
>                                <dependency>
>                                                <groupId>org.hibernate</
> groupId>
>                                                <artifactId>hibernate-
> entitymanager</artifactId>
>
>  <version>${hibernate-version}</version>
>                                                <exclusions>
>                                                                <exclusion>
>
>      <!-- omit Geronimo JPA spec to avoid conflict with Hibernate JPA spec
> -->
>
>      <groupId>org.apache.geronimo.specs</groupId>
>
>      <artifactId>geronimo-jpa_2.0_spec</artifactId>
>                                                                </exclusion>
>                                                </exclusions>
>                                </dependency>
>
>
> With
>                 <properties>
>                                <tapestry-release-version>5.4.
> 1</tapestry-release-version>
>                                <hibernate-version>4.2.6.
> Final</hibernate-version>
>                 ....
>
>                 </properties>
>
> I use 4.2.6.Finale of hibernate as 4.3.1.Final (what is stated as the
> correct version for Tapestry 5.4. in
>                 http://tapestry.apache.org/release-notes-54.html
> gives me an error.
>
>
> AppModule.java  contains:
>
>        @Match("*Dao")
>           public static void adviseTransactionally(
>                 JpaTransactionAdvisor advisor,
>                 MethodAdviceReceiver receiver) {
>
>              advisor.addTransactionCommitAdvice(receiver);
>           }
>
>
> Although it is not mentioned in
>
>                 http://tapestry.apache.org/integrating-with-jpa.html
>
> I also added the following statement in the AppModule.java :
>
>                 @Contribute(EntityManagerSource.class)
>                    public static void configurePersistenceUnitInfos(
> MappedConfiguration<String,PersistenceUnitConfigurer> cfg) {
>
>                       PersistenceUnitConfigurer configurer = new
> PersistenceUnitConfigurer() {
>                          public void configure(TapestryPersistenceUnitInfo
> unitInfo) {
>                                 unitInfo.addManagedClass(User.class);
>                          }
>                      };
>                      cfg.add("DemoUnit", configurer);
>                    }
>
> This crashes the App on start up  with the following Error Massage:
> org.apache.tapestry5.ioc.internal.OperationException: javax.persistence.PersistenceException:
> [PersistenceUnit: DemoUnit] Unable to build EntityManagerFactory
>
> Deep down in the Exception Stack there
> Caused by: org.hibernate.HibernateException: Connection cannot be null
> when 'hibernate.dialect' not set
>
> This is all not really helpful at least to me.
>
>
> If I omit the statement  configurePersistenceUnitInfos ...
>
> The App starts. But as soon as it calls the entityManager in the following
> class via add(User user);
> public class UserDAOImpl implements UserDAO
> {
>                 @PersistenceContext(unitName = "DemoUnit")
>      private EntityManager entityManager;
>
>     @Override
>     @PersistenceContext(unitName = "DemoUnit")
>     public void add(final User user)
>     {
>         entityManager.persist(user);
>     }
>
>     @Override
>     @SuppressWarnings(
>     { "unchecked" })
>     public List<User> findAll()
>     {
>         return entityManager.createQuery("select u from User u order by
> u.id desc").getResultList();
>     }
>
>     @Override
>     public void delete(final User... users)
>     {
>         for (final User user : users)
>             entityManager.remove(user);
>     }
>
>     @Override
>     public void deleteAll()
>     {
>         for (final User u : findAll())
>         {
>             entityManager.remove(u);
>         }
>     }
> }
>
> I get the error message:
>
> Unable to locate a single EntityManager. You must provide ...
>
>
> From the comments and other posts it is clear that the documentation is
> outdated and not really working. But there seems to be no really helpful
> alternative. I would be very grateful if someone could give me a hint where
> to look or explain to me what is going on here.
>
>
> Cheers
>
> Janko
>
>
>
>
>
>
>
>


--
Dmitry Gusev

AnjLab Team
http://anjlab.com
Reply | Threaded
Open this post in threaded view
|

AW: JPA with pure hibernate

joch40
Hey Dmitry,

thank you for your input and the direction.

Sadly it doesn’t solves the problem but it seems we are getting closer.

First of all there seems to be a ‘problem’ in your persistence.xml. The version you send me starts with

<?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://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="1.0">

The last line make my eclipse complain and I googled the problem. It seems that the last line must read:
 version="2.1">
see: http://stackoverflow.com/questions/20818737/can-not-find-the-declaration-of-element-persistence

The next problem is the
<mapping-file>orm.xml</mapping-file>

My eclipse complains that
vc-complex-type.2.4.a: Invalid content was found starting with element 'mapping-file'. One of '{"http://xmlns.jcp.org/xml/ns/persistence":shared-cache-
 mode, "http://xmlns.jcp.org/xml/ns/persistence":validation-mode, "http://xmlns.jcp.org/xml/ns/persistence":properties}' is expected.

I didn’t really found a solution for this problem at a first approach. Therefor I try to omit this line. If I do this and include the

configurePersistenceUnitInfos(MappedConfiguration<String,PersistenceUnitConfigurer> cfg)

method. I still get another but more concrete error message.


Now the output says:
 
[INFO] util.LogHelper HHH000204: Processing PersistenceUnitInfo [
        name: DemoUnit
        ...]
[INFO] hibernate.Version HHH000412: Hibernate Core {5.2.2.Final}
[INFO] cfg.Environment HHH000206: hibernate.properties not found
[INFO] cfg.Environment HHH000021: Bytecode provider name : javassist
[INFO] common.Version HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
[WARN] internal.ConnectionProviderInitiator HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
[WARN] internal.JdbcEnvironmentInitiator HHH000342: Could not obtain connection to query metadata : The application must supply JDBC connections
[ERROR] ioc.Registry org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
[ERROR] ioc.Registry Operations trace:
[ERROR] ioc.Registry [ 1] Invoking startup method org.apache.tapestry5.jpa.modules.JpaModule.startupEarly(EntityManagerManager, boolean).
[ERROR] failed app

The first lines suggest that now the setup seems to work. But then the the jdbc connection fails. Weird

Thanks anyhow for the input.



Janko

-----Ursprüngliche Nachricht-----
Von: Dmitry Gusev [mailto:[hidden email]]
Gesendet: Mittwoch, 18. Januar 2017 13:39
An: Tapestry users <[hidden email]>
Betreff: Re: JPA with pure hibernate

Hey,

tapestry-jpa supports latest Hibernate 5.x, at least we have it running in production with 5.2.5.Final You don't have to provide hibernate.cfg.xml, although the settings you have in your persistence.xml are not correct for 5.2.x Hibernate

Here's a working persistence.xml that we use in our apps:

<?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://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="1.0">

    <persistence-unit name="my-pu" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <mapping-file>META-INF/orm.xml</mapping-file>
        <properties>
            <property name="hibernate.hikari.dataSource.url"
value="jdbc:postgresql://localhost/dbname"/>
            <property name="hibernate.hikari.dataSource.user" value="username"/>
            <property name="hibernate.hikari.dataSource.password"
value="password"/>
            <property name="hibernate.hbm2ddl.auto" value="validate"/>
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.connection.provider_class"
value="org.hibernate.hikaricp.internal.HikariCPConnectionProvider"/>
            <property name="hibernate.auth" value="Container" />
            <property name="hibernate.hikari.connectionTimeout"
value="300000" />
            <property name="hibernate.hikari.maximumPoolSize" value="50" />
            <property name="hibernate.hikari.minimumIdle" value="5" />
            <property name="hibernate.hikari.dataSourceClassName"
value="org.postgresql.ds.PGSimpleDataSource" />
        </properties>
    </persistence-unit>
</persistence>

Above configuration uses HikariCP for connection pooling.

Some relevant dependencies:

compile ("org.hibernate:hibernate-core:5.2.5.Final") {
    exclude group: 'commons-collections'

}

compile ("org.hibernate:hibernate-hikaricp:5.2.5.Final") {
    exclude group: "com.zaxxer", module: "HikariCP-java6"
}

compile 'com.zaxxer:HikariCP:2.5.1'

compile "org.postgresql:postgresql:9.4.1212"


The file META-INF/orm.xml has no declarations, not sure if it's still necessary, but some earlier versions of Hibernate complained without it:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
    version="2.0">
</entity-mappings>


Hope this helps.

On Wed, Jan 18, 2017 at 2:19 PM, Jochimsen, Janko < [hidden email]> wrote:

>
> Hi,
>
> I am trying to switch my application from a standard
> tapestry-hibernate version to a tapestry-jpa version. I would like to
> have a pure hibernate solution and skip any eclipselink dependency.
>
> After spending some time with
>                 http://tapestry.apache.org/integrating-with-jpa.html
> I am more or less lost as the documentation is not working and the
> comments and remarks from basileChandesris are not really clear.
>
> After I discovered that it seems to be necessary to provide this line:
>    <provider>org.hibernate.ejb.HibernatePersistence</provider>
> my persistence.xml itself looks like this:
>
> <?xml version="1.0" encoding="UTF-8"?> <persistence
> xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
>    <persistence-unit name="DemoUnit" transaction-type="RESOURCE_LOCAL">
>    <provider>org.hibernate.ejb.HibernatePersistence</provider>
>        <properties>
>         <property name="hibernate.connection.driver_class">org.postgresql.
> Driver</property>
>         <property name="hibernate.connection.url">jdbc:postgresql://
> localhost:5432/xxx-test</property>
>         <property name="hibernate.connection.username">XX</property>
>         <property name="hibernate.connection.password">XXX</property>
>         <property name="hibernate.dialect">org.hibernate.dialect.
> PostgreSQLDialect</property>
>         <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
>       </properties>
>    </persistence-unit>
> </persistence>
>
> As it is not stated in the guide I do not provide a hibernate.cfg.xml file.
>
>
> The relevant part of the POM file reads:
>                                <dependency>
>
>  <groupId>org.apache.tapestry</groupId>
>                                                
> <artifactId>tapestry-jpa</
> artifactId>
>                                                
> <version>${tapestry-release- version}</version>
>                                </dependency>
>
>                                <dependency>
>                                                
> <groupId>org.hibernate</
> groupId>
>                                                <artifactId>hibernate-
> entitymanager</artifactId>
>
>  <version>${hibernate-version}</version>
>                                                <exclusions>
>                                                                
> <exclusion>
>
>      <!-- omit Geronimo JPA spec to avoid conflict with Hibernate JPA
> spec
> -->
>
>      <groupId>org.apache.geronimo.specs</groupId>
>
>      <artifactId>geronimo-jpa_2.0_spec</artifactId>
>                                                                </exclusion>
>                                                </exclusions>
>                                </dependency>
>
>
> With
>                 <properties>
>                                <tapestry-release-version>5.4.
> 1</tapestry-release-version>
>                                <hibernate-version>4.2.6.
> Final</hibernate-version>
>                 ....
>
>                 </properties>
>
> I use 4.2.6.Finale of hibernate as 4.3.1.Final (what is stated as the
> correct version for Tapestry 5.4. in
>                 http://tapestry.apache.org/release-notes-54.html
> gives me an error.
>
>
> AppModule.java  contains:
>
>        @Match("*Dao")
>           public static void adviseTransactionally(
>                 JpaTransactionAdvisor advisor,
>                 MethodAdviceReceiver receiver) {
>
>              advisor.addTransactionCommitAdvice(receiver);
>           }
>
>
> Although it is not mentioned in
>
>                 http://tapestry.apache.org/integrating-with-jpa.html
>
> I also added the following statement in the AppModule.java :
>
>                 @Contribute(EntityManagerSource.class)
>                    public static void configurePersistenceUnitInfos(
> MappedConfiguration<String,PersistenceUnitConfigurer> cfg) {
>
>                       PersistenceUnitConfigurer configurer = new
> PersistenceUnitConfigurer() {
>                          public void
> configure(TapestryPersistenceUnitInfo
> unitInfo) {
>                                 unitInfo.addManagedClass(User.class);
>                          }
>                      };
>                      cfg.add("DemoUnit", configurer);
>                    }
>
> This crashes the App on start up  with the following Error Massage:
> org.apache.tapestry5.ioc.internal.OperationException: javax.persistence.PersistenceException:
> [PersistenceUnit: DemoUnit] Unable to build EntityManagerFactory
>
> Deep down in the Exception Stack there Caused by:
> org.hibernate.HibernateException: Connection cannot be null when
> 'hibernate.dialect' not set
>
> This is all not really helpful at least to me.
>
>
> If I omit the statement  configurePersistenceUnitInfos ...
>
> The App starts. But as soon as it calls the entityManager in the
> following class via add(User user); public class UserDAOImpl
> implements UserDAO {
>                 @PersistenceContext(unitName = "DemoUnit")
>      private EntityManager entityManager;
>
>     @Override
>     @PersistenceContext(unitName = "DemoUnit")
>     public void add(final User user)
>     {
>         entityManager.persist(user);
>     }
>
>     @Override
>     @SuppressWarnings(
>     { "unchecked" })
>     public List<User> findAll()
>     {
>         return entityManager.createQuery("select u from User u order
> by u.id desc").getResultList();
>     }
>
>     @Override
>     public void delete(final User... users)
>     {
>         for (final User user : users)
>             entityManager.remove(user);
>     }
>
>     @Override
>     public void deleteAll()
>     {
>         for (final User u : findAll())
>         {
>             entityManager.remove(u);
>         }
>     }
> }
>
> I get the error message:
>
> Unable to locate a single EntityManager. You must provide ...
>
>
> From the comments and other posts it is clear that the documentation
> is outdated and not really working. But there seems to be no really
> helpful alternative. I would be very grateful if someone could give me
> a hint where to look or explain to me what is going on here.
>
>
> Cheers
>
> Janko
>
>
>
>
>
>
>
>


--
Dmitry Gusev

AnjLab Team
http://anjlab.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: JPA with pure hibernate

Dmitry Gusev
There must be a cause why the connection has failed, could be a typo in
JDBC url, username or password.

If you can copy full stack trace it may help.

On Wed, Jan 18, 2017 at 8:07 PM, Jochimsen, Janko <
[hidden email]> wrote:

> Hey Dmitry,
>
> thank you for your input and the direction.
>
> Sadly it doesn’t solves the problem but it seems we are getting closer.
>
> First of all there seems to be a ‘problem’ in your persistence.xml. The
> version you send me starts with
>
> <?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://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
> version="1.0">
>
> The last line make my eclipse complain and I googled the problem. It seems
> that the last line must read:
>  version="2.1">
> see: http://stackoverflow.com/questions/20818737/can-not-
> find-the-declaration-of-element-persistence
>
> The next problem is the
> <mapping-file>orm.xml</mapping-file>
>
> My eclipse complains that
> vc-complex-type.2.4.a: Invalid content was found starting with element
> 'mapping-file'. One of '{"http://xmlns.jcp.org/xml/ns/persistence
> ":shared-cache-
>  mode, "http://xmlns.jcp.org/xml/ns/persistence":validation-mode, "
> http://xmlns.jcp.org/xml/ns/persistence":properties}' is expected.
>
> I didn’t really found a solution for this problem at a first approach.
> Therefor I try to omit this line. If I do this and include the
>
> configurePersistenceUnitInfos(MappedConfiguration<String,PersistenceUnitConfigurer>
> cfg)
>
> method. I still get another but more concrete error message.
>
>
> Now the output says:
>
> [INFO] util.LogHelper HHH000204: Processing PersistenceUnitInfo [
>         name: DemoUnit
>         ...]
> [INFO] hibernate.Version HHH000412: Hibernate Core {5.2.2.Final}
> [INFO] cfg.Environment HHH000206: hibernate.properties not found
> [INFO] cfg.Environment HHH000021: Bytecode provider name : javassist
> [INFO] common.Version HCANN000001: Hibernate Commons Annotations
> {5.0.1.Final}
> [WARN] internal.ConnectionProviderInitiator HHH000181: No appropriate
> connection provider encountered, assuming application will be supplying
> connections
> [WARN] internal.JdbcEnvironmentInitiator HHH000342: Could not obtain
> connection to query metadata : The application must supply JDBC connections
> [ERROR] ioc.Registry org.hibernate.service.spi.ServiceException: Unable
> to create requested service [org.hibernate.engine.jdbc.
> env.spi.JdbcEnvironment]
> [ERROR] ioc.Registry Operations trace:
> [ERROR] ioc.Registry [ 1] Invoking startup method org.apache.tapestry5.jpa.
> modules.JpaModule.startupEarly(EntityManagerManager, boolean).
> [ERROR] failed app
>
> The first lines suggest that now the setup seems to work. But then the the
> jdbc connection fails. Weird
>
> Thanks anyhow for the input.
>
>
>
> Janko
>
> -----Ursprüngliche Nachricht-----
> Von: Dmitry Gusev [mailto:[hidden email]]
> Gesendet: Mittwoch, 18. Januar 2017 13:39
> An: Tapestry users <[hidden email]>
> Betreff: Re: JPA with pure hibernate
>
> Hey,
>
> tapestry-jpa supports latest Hibernate 5.x, at least we have it running in
> production with 5.2.5.Final You don't have to provide hibernate.cfg.xml,
> although the settings you have in your persistence.xml are not correct for
> 5.2.x Hibernate
>
> Here's a working persistence.xml that we use in our apps:
>
> <?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://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
> version="1.0">
>
>     <persistence-unit name="my-pu" transaction-type="RESOURCE_LOCAL">
>         <provider>org.hibernate.jpa.HibernatePersistenceProvider</
> provider>
>         <exclude-unlisted-classes>true</exclude-unlisted-classes>
>         <mapping-file>META-INF/orm.xml</mapping-file>
>         <properties>
>             <property name="hibernate.hikari.dataSource.url"
> value="jdbc:postgresql://localhost/dbname"/>
>             <property name="hibernate.hikari.dataSource.user"
> value="username"/>
>             <property name="hibernate.hikari.dataSource.password"
> value="password"/>
>             <property name="hibernate.hbm2ddl.auto" value="validate"/>
>             <property name="hibernate.show_sql" value="false" />
>             <property name="hibernate.connection.provider_class"
> value="org.hibernate.hikaricp.internal.HikariCPConnectionProvider"/>
>             <property name="hibernate.auth" value="Container" />
>             <property name="hibernate.hikari.connectionTimeout"
> value="300000" />
>             <property name="hibernate.hikari.maximumPoolSize" value="50"
> />
>             <property name="hibernate.hikari.minimumIdle" value="5" />
>             <property name="hibernate.hikari.dataSourceClassName"
> value="org.postgresql.ds.PGSimpleDataSource" />
>         </properties>
>     </persistence-unit>
> </persistence>
>
> Above configuration uses HikariCP for connection pooling.
>
> Some relevant dependencies:
>
> compile ("org.hibernate:hibernate-core:5.2.5.Final") {
>     exclude group: 'commons-collections'
>
> }
>
> compile ("org.hibernate:hibernate-hikaricp:5.2.5.Final") {
>     exclude group: "com.zaxxer", module: "HikariCP-java6"
> }
>
> compile 'com.zaxxer:HikariCP:2.5.1'
>
> compile "org.postgresql:postgresql:9.4.1212"
>
>
> The file META-INF/orm.xml has no declarations, not sure if it's still
> necessary, but some earlier versions of Hibernate complained without it:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
>     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
> http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
>     version="2.0">
> </entity-mappings>
>
>
> Hope this helps.
>
> On Wed, Jan 18, 2017 at 2:19 PM, Jochimsen, Janko < janko.jochimsen@urios-
> beratung.de> wrote:
>
> >
> > Hi,
> >
> > I am trying to switch my application from a standard
> > tapestry-hibernate version to a tapestry-jpa version. I would like to
> > have a pure hibernate solution and skip any eclipselink dependency.
> >
> > After spending some time with
> >                 http://tapestry.apache.org/integrating-with-jpa.html
> > I am more or less lost as the documentation is not working and the
> > comments and remarks from basileChandesris are not really clear.
> >
> > After I discovered that it seems to be necessary to provide this line:
> >    <provider>org.hibernate.ejb.HibernatePersistence</provider>
> > my persistence.xml itself looks like this:
> >
> > <?xml version="1.0" encoding="UTF-8"?> <persistence
> > xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
> >    <persistence-unit name="DemoUnit" transaction-type="RESOURCE_LOCAL">
> >    <provider>org.hibernate.ejb.HibernatePersistence</provider>
> >        <properties>
> >         <property name="hibernate.connection.
> driver_class">org.postgresql.
> > Driver</property>
> >         <property name="hibernate.connection.url">jdbc:postgresql://
> > localhost:5432/xxx-test</property>
> >         <property name="hibernate.connection.username">XX</property>
> >         <property name="hibernate.connection.password">XXX</property>
> >         <property name="hibernate.dialect">org.hibernate.dialect.
> > PostgreSQLDialect</property>
> >         <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
> >       </properties>
> >    </persistence-unit>
> > </persistence>
> >
> > As it is not stated in the guide I do not provide a hibernate.cfg.xml
> file.
> >
> >
> > The relevant part of the POM file reads:
> >                                <dependency>
> >
> >  <groupId>org.apache.tapestry</groupId>
> >
> > <artifactId>tapestry-jpa</
> > artifactId>
> >
> > <version>${tapestry-release- version}</version>
> >                                </dependency>
> >
> >                                <dependency>
> >
> > <groupId>org.hibernate</
> > groupId>
> >                                                <artifactId>hibernate-
> > entitymanager</artifactId>
> >
> >  <version>${hibernate-version}</version>
> >                                                <exclusions>
> >
> > <exclusion>
> >
> >      <!-- omit Geronimo JPA spec to avoid conflict with Hibernate JPA
> > spec
> > -->
> >
> >      <groupId>org.apache.geronimo.specs</groupId>
> >
> >      <artifactId>geronimo-jpa_2.0_spec</artifactId>
> >
> </exclusion>
> >                                                </exclusions>
> >                                </dependency>
> >
> >
> > With
> >                 <properties>
> >                                <tapestry-release-version>5.4.
> > 1</tapestry-release-version>
> >                                <hibernate-version>4.2.6.
> > Final</hibernate-version>
> >                 ....
> >
> >                 </properties>
> >
> > I use 4.2.6.Finale of hibernate as 4.3.1.Final (what is stated as the
> > correct version for Tapestry 5.4. in
> >                 http://tapestry.apache.org/release-notes-54.html
> > gives me an error.
> >
> >
> > AppModule.java  contains:
> >
> >        @Match("*Dao")
> >           public static void adviseTransactionally(
> >                 JpaTransactionAdvisor advisor,
> >                 MethodAdviceReceiver receiver) {
> >
> >              advisor.addTransactionCommitAdvice(receiver);
> >           }
> >
> >
> > Although it is not mentioned in
> >
> >                 http://tapestry.apache.org/integrating-with-jpa.html
> >
> > I also added the following statement in the AppModule.java :
> >
> >                 @Contribute(EntityManagerSource.class)
> >                    public static void configurePersistenceUnitInfos(
> > MappedConfiguration<String,PersistenceUnitConfigurer> cfg) {
> >
> >                       PersistenceUnitConfigurer configurer = new
> > PersistenceUnitConfigurer() {
> >                          public void
> > configure(TapestryPersistenceUnitInfo
> > unitInfo) {
> >                                 unitInfo.addManagedClass(User.class);
> >                          }
> >                      };
> >                      cfg.add("DemoUnit", configurer);
> >                    }
> >
> > This crashes the App on start up  with the following Error Massage:
> > org.apache.tapestry5.ioc.internal.OperationException: javax.persistence.
> PersistenceException:
> > [PersistenceUnit: DemoUnit] Unable to build EntityManagerFactory
> >
> > Deep down in the Exception Stack there Caused by:
> > org.hibernate.HibernateException: Connection cannot be null when
> > 'hibernate.dialect' not set
> >
> > This is all not really helpful at least to me.
> >
> >
> > If I omit the statement  configurePersistenceUnitInfos ...
> >
> > The App starts. But as soon as it calls the entityManager in the
> > following class via add(User user); public class UserDAOImpl
> > implements UserDAO {
> >                 @PersistenceContext(unitName = "DemoUnit")
> >      private EntityManager entityManager;
> >
> >     @Override
> >     @PersistenceContext(unitName = "DemoUnit")
> >     public void add(final User user)
> >     {
> >         entityManager.persist(user);
> >     }
> >
> >     @Override
> >     @SuppressWarnings(
> >     { "unchecked" })
> >     public List<User> findAll()
> >     {
> >         return entityManager.createQuery("select u from User u order
> > by u.id desc").getResultList();
> >     }
> >
> >     @Override
> >     public void delete(final User... users)
> >     {
> >         for (final User user : users)
> >             entityManager.remove(user);
> >     }
> >
> >     @Override
> >     public void deleteAll()
> >     {
> >         for (final User u : findAll())
> >         {
> >             entityManager.remove(u);
> >         }
> >     }
> > }
> >
> > I get the error message:
> >
> > Unable to locate a single EntityManager. You must provide ...
> >
> >
> > From the comments and other posts it is clear that the documentation
> > is outdated and not really working. But there seems to be no really
> > helpful alternative. I would be very grateful if someone could give me
> > a hint where to look or explain to me what is going on here.
> >
> >
> > Cheers
> >
> > Janko
> >
> >
> >
> >
> >
> >
> >
> >
>
>
> --
> Dmitry Gusev
>
> AnjLab Team
> http://anjlab.com
>



--
Dmitry Gusev

AnjLab Team
http://anjlab.com