PDA

View Full Version : problem using EJB3 JPA with Hibernate


vlaugier
10-30-2009, 05:21 PM
Hello,

I am a new user of Resin. I want to create a new JEE connector for the opensource project Objectwiz (http://objectwiz.org) (for the moment Objectwiz only support Jboss).

For the moment I am trying to do what I am used to doing on other JEE server (Jboss for instance) :

create a war projet with an EJB3 entity bean in it
declare a datasource (a postgresl DB) and a persistence unit (with hibernate)
deploy the all thing...and see what happen.



We have followed :

* the Hibernate wiki : http://wiki.caucho.com/Hibernate
* and Amber documentation :
http://localhost:8080/resin-doc/examples/amber-basic/index.xtp

Here is what we have done :

1. create the postgresql database 'test'
2. add the postgresql jdbc jar in the lib directory of the server
3. create the entity bean : fr.helmet.test.Account
4. declare the data-source in WEB-INF/resin-web.xml (
<database><name>jdbc/test</name><driver>[...]</driver></database>)
5. tell Amber to look for perstence.xml in the class path
(<ejb-server data-source="jdbc/test" />)
6. create the WEB-INF/classes/META-INF/persistence.xml
7. create the servlet fr.helmet.test.servlet.FirstServlet (where we
try to use the entity manager) and declare it in resin-web.xml

We get the following error at deployment :

[09-10-28 18:58:04.304] {main} WebApp[http://default/test_resin1] error
[09-10-28 18:58:04.317] {main} org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager
[09-10-28 18:58:04.317] {main} at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:361)
[09-10-28 18:58:04.317] {main} at org.hibernate.cfg.Configuration.buildSessionFactor y(Configuration.java:1327)
[09-10-28 18:58:04.317] {main} at org.hibernate.cfg.AnnotationConfiguration.buildSes sionFactory(AnnotationConfiguration.java:867)
[09-10-28 18:58:04.317] {main} at org.hibernate.ejb.Ejb3Configuration.buildEntityMan agerFactory(Ejb3Configuration.java:669)
[...]
(see error_page_1.html for complete log)

We have found on the web (http://www.abcseo.com/tech/java/jboss-ebj-troubleshooting) that this occurs when "accessing an Entity bean from outside of the J2EE environment".
Therefore in persistence.xml we have added 'transaction-type="RESOURCE_LOCAL"' in the persistence-unit tag and declared the data-source as '<non-jta-data-source>jdbc/test</non-jta-data-source>'

This solves the deployment problem.

But when invoking the servlet we get the following error :
fr.helmet.test.servlet.FirstServlet._manager: @PersistenceContext cannot find any persistence contexts. No JPA persistence-units have been deployed.

I have put in attachment the zipped projet without the lib/jars (listed below)

[vlaugier@vince ~]$ ls -1 /home/vlaugier/WORKSPACES/workspace_optic-neo/test_resin1/WebContent/WEB-INF/lib
antlr-2.7.6.jar
antlr.jar
asm-attrs.jar
asm.jar
cglib-2.2.jar
cglib.jar
commons-collections-3.1.jar
commons-collections.jar
dom4j-1.6.1.jar
dom4j.jar
ejb3-persistence.jar
hibernate3.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-core.jar
hibernate-entitymanager.jar
javassist-3.9.0.GA.jar
javassist.jar
jta-1.1.jar
jta.jar
junit.jar
log4j.jar
slf4j-api-1.5.8.jar
slf4j-api.jar
slf4j-log4j12.jar


Thanks for your help

regards

Vincent

reza
11-03-2009, 05:12 PM
Vincent,

I'll take a look ASAP and let you know.

Thanks,
Reza

reza
11-03-2009, 07:31 PM
Vincent,

Can you please post your current code? I can't seem to get a handle to your attachment.

Thanks,
Reza

reza
11-03-2009, 11:16 PM
Vincent,

I was able to confirm both bugs you reported - @PersistenceUnit not resolving any persistence units and the container managed Hibernate entity manager injected via @PersistenceContext not able to locate the Resin JTA transaction manager as it should. I'll enter bug reports for both.

Keep in mind, Resin is not yet Java EE certified, so these problems are not entirely unexpected. We will be Java EE 6 certified soon, so these problems should go away at that point.

Per se, there is nothing wrong with your code. If you need a workaround for now, I would suggest using Hibernate/JPA as you would in a Java SE application and not relying on Resin injection functionality beyond Servlets/JSP.

Otherwise, I can look into how to make the Resin transaction manager work with Hibernate for you.

Sorry and hope this helps,
Reza

reza
11-11-2009, 08:16 PM
Vincent,

You can get @PersistenceContext to work correctly if you explicitly set hibernate.transaction.manager_lookup_class like so:

<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.ResinTransactionManagerL ookup" />

I'm attaching my persistence.xml for reference here.

Hope it helps,
Reza

vlaugier
11-12-2009, 09:19 AM
Hello Reza,

that helped, it is nearly working:

The persistence unit is somehow detected at deployment : there is no exception, the database schema is updated according to the entity beans.

Nevertheless we still have the '500 Servlet Exception' when invoking the servlet that tries to use the entityManager.
"fr.helmet.test.servlet.FirstServlet._manager: @PersistenceContext cannot find any persistence contexts. No JPA persistence-units have been deployed"

I have put in attachment the files that may be relevant :
WEB-INF/resin-web.xml
WEB-INF/classes/META-INF/persistence.xml
WEB-INF/classes/fr/helmet/test/servlet/FirstServlet.class

I have also set up log4 to display all logs, is is in attachment (deployment_resin.log).

Thank you.

Regards.

Vincent

vlaugier
11-12-2009, 09:24 AM
I cannot upload files (invalid file error) :

Here is the resin-web.xml :

<web-app xmlns="http://caucho.com/ns/resin">

<database>
<jndi-name>jdbc/test</jndi-name>
<driver>
<type>org.postgresql.Driver</type>
<url>jdbc:postgresql://127.0.0.1:5432/test</url>
<user>test</user>
<password>test</password>
</driver>
</database>

<!-- server configuration -->
<ejb-server data-source="jdbc/test" />

<!-- Set up the example servlet -->
<!-- <servlet servlet-name="firstservlet" servlet-class="fr.helmet.test.servlet.FirstServlet" />-->
<servlet-mapping url-pattern="/firstservlet" servlet-class="fr.helmet.test.servlet.FirstServlet" />

</web-app>

vlaugier
11-12-2009, 09:25 AM
Here is the persistence.xml :

<?xml version="1.0" encoding="UTF-8"?>
<!-- Persistence deployment descriptor for dev profile -->
<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_1_0.xsd"
version="1.0">

<persistence-unit name="test_resin1PersistenceUnit"
transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/test</jta-data-source>
<properties>
<!-- <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />-->

<property name="hibernate.dialect"
value="org.hibernate.dialect.SQLServerDialect" />

<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.ResinTransactionManagerL ookup" />
</properties>
</persistence-unit>


</persistence>

vlaugier
11-12-2009, 09:26 AM
Here is the servlet :

package fr.helmet.test.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.helmet.test.AccountBis;

public class FirstServlet extends HttpServlet{

// Resin IoC will inject this
@PersistenceContext(name="test_resin1PersistenceUnit")
private EntityManager _manager;

public void service(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
PrintWriter out = response.getWriter();
response.setContentType("text/html");

out.println("EntityManager = " + _manager + "<br/>");

AccountBis accountBis = new AccountBis();
accountBis.setLogin("toto");

out.println("account detail : " + accountBis.getLogin());

}

}

vlaugier
11-12-2009, 09:29 AM
in attachment

vlaugier
11-12-2009, 09:35 AM
Is the 'ejb-server' really necessary ?

<ejb-server data-source="jdbc/test" />

reza
11-12-2009, 01:46 PM
Vincent,

As far as I know, the <ejb-server> part will enable Amber (the default persistence provider), so might cause problems. I would remove it. Other than that, your code is very similar to mine. Just to make sure, I'll get it working in my environment and let you know what happens.

Thanks,
Reza

vlaugier
11-12-2009, 02:19 PM
Hello Reza,

same problem when removing '<ejb-server data-source="jdbc/test" />'


Maybe there is a conflict with my webapp libraries.

Here is the list of the jars in WEB-INF/lib (there are all taken from Hibernate project) :

antlr-2.7.6.jar
antlr.jar
asm-attrs.jar
asm.jar
cglib-2.2.jar
cglib.jar
commons-collections-3.1.jar
commons-collections.jar
dom4j-1.6.1.jar
dom4j.jar
ejb3-persistence.jar
hibernate3.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-core.jar
hibernate-entitymanager.jar
javassist-3.9.0.GA.jar
javassist.jar
jta-1.1.jar
jta.jar
junit.jar
log4j.jar
slf4j-api-1.5.8.jar
slf4j-api.jar
slf4j-log4j12.jar

The application is running on resin-pro-4.0.1.

regards

vlaugier
11-12-2009, 02:23 PM
Hello again,

I have put in attachment the deployed project (without the jars)

regards

reza
11-12-2009, 07:54 PM
Vincent,

Your code appears to be working fine in 3.1.9 (the latest stable version). As a next step, you should probably try running my code - it is attached. I am using SQL Server, but that should not be an issue.

Also, just to make sure there are no class loader issues here, you should move all jars to RESIN_HOME/lib. Make sure to include the database driver jars there as well.

Let me know how it goes. A remote possibility is that there might be a bug that is specific to the version you are using.

Hope it helps,
Reza

vlaugier
11-13-2009, 09:08 AM
indeed, it works fine with resin pro 3.1.9

we will work with this version instead

thank you very much for your help

reza
11-13-2009, 01:13 PM
Vincent,

You are most welcome. I'll test it on the development version and enter a bug just to make sure this does not slip through the cracks (that's probably unlikely since there should be tests in the spec compatibility kit covering this).

Cheers,
Reza

yfhsu
12-01-2011, 12:26 AM
Hi,

I'm using resin pro 4.0.24 with default JPA provider. I'm getting
Unknown @PersistenceContext when unitName is supplied and @PersistenceContext cannot find any persistence contexts. No JPA persistence-units have been deployed without unitName.

I have only one persistence-unit in the system and it's defined in the WEB-INF/classes/META-INF/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="mac" transaction-type="RESOURCE_LOCAL">
<class>Entity1</class>
<class>Entity2</class>

<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://test:5432/mac"/>
<property name="javax.persistence.jdbc.user" value="testname"/>
<property name="javax.persistence.jdbc.password" value="testpwd"/>
</properties>

</persistence-unit>
</persistence>


Is there any configuration required to make Resin aware of the persistence unit?

Regards,
Yen-Fen

johnsonandrews5
12-03-2011, 01:38 PM
I was able to confirm both bugs you reported - @PersistenceUnit not resolving any persistence units and the container managed Hibernate entity manager injected via @PersistenceContext not able to locate the Resin JTA transaction manager as it should. I'll enter bug reports for both.





Urban City Rentals Inc. offers a complete range of Rental Property Management services in Vancouver for your investment property.Property Management Langley (http://www.urbancityrentals.com/property/management/langley)

johnsonandrews5
12-10-2011, 07:06 AM
Per se, there is nothing wrong with your code. If you need a workaround for now, I would suggest using Hibernate/JPA as you would in a Java SE application and not relying on Resin injection functionality beyond Servlets/JSP.







Urban City Rentals Inc. offers a complete range of Rental Property Management services in Vancouver for your investment property. Langley Property Management (http://www.urbancityrentals.com/property/management/langley)

johnsonandrews5
12-18-2011, 07:34 AM
there is nothing wrong with your code. If you need a workaround for now, I would suggest using Hibernate/JPA as you would in a Java SE application and not relying on Resin injection functionality beyond Servlets/JSP.







Urban City Rentals Inc. offers a complete range of Rental Property Management services in Vancouver for your investment property.Richmond Property Management (http://www.urbancityrentals.com/property/management/richmond)

jockbenny
02-27-2012, 06:03 AM
The topic was very interesting. There are ,many advantages with EJB3with Hibernate as well as their exists some problems with it. I believe that we can also use "USE", "UPDATE" as options. That way if there are any schema changes, it can be applied or redeployed instead of recreating from scratch. Could you please provide some more links regarding the topic?

c9rmt
02-28-2012, 06:06 AM
This will leave an intense hold on your name brand. Basically burning your brand on their memory is ones own advertising objective.c9 rmt (http://www.rmtvip.jp/rmt/c9.html)
ルーセントハート rmt (http://www.rmt-mmo.com/rmt/17_Lucent_Heart.html)
アイオン rmt (http://www.rmtvip.jp/)
タルタロス rmt (http://www.rmtvip.jp/rmt/tartaros.html)
信長 rmt (http://www.rmt-mmo.com/rmt/13_nobunaga.html)