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