This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 144016 - Can't use RESTfull service with Hibernate as JPA provider
Summary: Can't use RESTfull service with Hibernate as JPA provider
Status: RESOLVED FIXED
Alias: None
Product: webservices
Classification: Unclassified
Component: REST (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker with 1 vote (vote)
Assignee: Milan Kuchtiak
URL:
Keywords:
Depends on: 145589 233726
Blocks:
  Show dependency tree
 
Reported: 2008-08-15 09:27 UTC by tprochazka
Modified: 2013-08-07 13:54 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
exception stracktrace (138.26 KB, image/png)
2008-09-04 00:39 UTC, Ayub Khan
Details
Oops wrong file attached earlier, this has the exception (5.58 KB, text/plain)
2008-09-04 00:47 UTC, Ayub Khan
Details

Note You need to log in before you can comment on or make changes to this bug.
Description tprochazka 2008-08-15 09:27:03 UTC
This problem is't in REST support, but in hibernate class library which Netbeans define.
This library contain standard Hibernate dependencies:
asm-attrs.jar
asm.jar
cglib-2.1.3.jar

Jersey use ASM library also, but in 3.1 version which is not compatible with version from hibernate.

I think, that best solution is remove asm-attrs.jar, asm.jar and cglib-2.1.3.jar from Hibernate class library and use 
cglib-nodep-2.1_3.jar.

Steps to reproduce this problem:
1. Create web project
2. Create Persitence Unit with Hibernate and obtain some entity class from Database
3. Generate RESTfull web services from this entity class
4. Run application

Tested with Build 20080814104752
Comment 1 japod 2008-08-15 11:26:26 UTC
Tomas Prochazka (the reporter) says the following workaround works for him on Tomcat
(based on information from CZJUG mailing list):

initialization of entity manager needs to be rewritten as follows:
 emf = Persistence.createEntityManagerFactory(DEFAULT_PU);
 em = emf.createEntityManager();
 utx = em.getTransaction();
Comment 2 Peter Liu 2008-08-15 19:29:11 UTC
Ayub, please take care of this one since you tried to get things to work with hibernate before.  BTW, I think the lazy
instantiation problem is gone now since we no longer close the persistence factory after each request.

Comment 3 Ayub Khan 2008-09-04 00:38:36 UTC
On Glassfish I see two issues 

1. The lib issue is already mentioned, for which I have filed an issue 145589 against hibernatelibs module
2. Getting exception accessing collections (after locally fixing hibernatelib module with the suggested fix from 145589)

failed to lazily initialize a collection of role: customerdb.DiscountCode.customerCollection, no session or session was
closed (See attachment for stacktrace)

Steps to reproduce.

1. Checkout NB 6.5 from mercurial, do build (cd nbbuild; ant)
2. Locally fix hibernatelib module as suggested in 145589, do build hibernatelib (cd hibernatelib; ant)
3. Run nbbuild/netbeans/bin/netbeans
4. Create 'CustomerDB' REST web services sample app
5. Add "Hibernate" as framework to this app (by right click on the CustomerDB prject node and selecting 'Properties'
6. Change /Configuration Files/persistence.xml as follows

<!--<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="CustomerDBPU" transaction-type="JTA">
        <jta-data-source>jdbc/sample</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties/>
    </persistence-unit>
</persistence>-->
<persistence version="1.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_1_0.xsd">
  <persistence-unit name="CustomerDBPU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/sample</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
  </persistence-unit>
</persistence>

7. Now run 'Test RESTful Web Services' action, you will see a browser open up with Test client to test RESTful Web Services.
8. Click on 'customers' link on the left side menu, then click 'Test' button on the right side.
9. Expand node 'customers', click on customerId node, then enter '1' for customerId, then click 'Test' button. This time
you should see the exception attached.
Comment 4 Ayub Khan 2008-09-04 00:39:31 UTC
Created attachment 68996 [details]
exception stracktrace
Comment 5 Ayub Khan 2008-09-04 00:47:24 UTC
Created attachment 68997 [details]
Oops wrong file attached earlier, this has the exception
Comment 6 Peter Liu 2008-09-04 22:23:31 UTC
Basically, the issue with lazy fetching is that it needs to be done instead of a hibernate session or transaction. In
our case, the serialization of the entities are done outside of the transaction (after the entities are returned from
the get method). It is already too late. See the following hibernate documentation for detail:

http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#performance-fetching-lazy

According to the hibernate documentation, there are two way to work around the issue, one is to have the http session
close the transaction at the very end or explicitly fetch all the necessary data before closing the transaction.  For
the first approach, we have no control over the session. The second approach requires us to generate code to explicitly
fetch all the relationship objects down to the level specified by the expandLevel flag. We can probably generate some
code in our converter to do this.  I'll give that a try.


Comment 7 Peter Liu 2008-09-05 18:51:20 UTC
I just checked in the fixes for the lazy instantiation problem. I also got things to work with Spring+Hibernate.
However, there are still some issues related to tomcat. I'll look into it further after I close some other issues first.



Comment 8 Milan Kuchtiak 2013-07-31 14:34:19 UTC
REST services still not working with Hibernate JPA 2.0.
See the issue 233726.
Comment 9 Milan Kuchtiak 2013-08-07 13:54:05 UTC
Closed, as both related bugs (bug 145589 and bug 233726) were marked as fixed.
At least with GlassFish 3 Hibernate 2.0 works now.