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 196096 - EntityManager generator generate incorrect code
Summary: EntityManager generator generate incorrect code
Status: VERIFIED FIXED
Alias: None
Product: javaee
Classification: Unclassified
Component: Persistence (show other bugs)
Version: 7.0
Hardware: Other Other
: P2 normal (vote)
Assignee: Sergey Petrov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-02-28 12:56 UTC by Denis Anisimov
Modified: 2011-03-01 17:58 UTC (History)
2 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
server stactrace (16.46 KB, text/plain)
2011-02-28 14:05 UTC, Denis Anisimov
Details
deployment exception (3.28 KB, text/plain)
2011-02-28 14:20 UTC, Denis Anisimov
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Denis Anisimov 2011-02-28 12:56:07 UTC
The problem in the J2EE persistence API implementation.
Steps to reproduce.
1) Add GF 3.1 ( or 3.0.1 )
2) Create JEE6 Web Project .
3) Create RESTful WS via wizard.
4) Don't change anything in generated classes.
5) Deploy and Test RESTful WS.

As result there is an error.

The problem is presence of injected attribute in the service classes:
@Resource
private javax.transaction.UserTransaction utx;

It appears on j2e persistence API method usage :
EntityManagerGenerator generator = new EntityManagerGenerator(facade,
entityFQN);
List<GenerationOptions> methodOptions = getMethodOptions(entityFQN,
variableName);
for (GenerationOptions each : methodOptions){
            generator.generate(each, ContainerManagedJTAInjectableInEJB.class);
}

The latter method should generate ONLY EntityManager injection in the issue 
situation. But it generates also utx field .
Comment 1 Sergey Petrov 2011-02-28 13:07:21 UTC
see http://netbeans.org/bugzilla/show_bug.cgi?id=188821

only if it's EJB there should be no UserTransaction. Is it EJB in this case?
Comment 2 Sergey Petrov 2011-02-28 13:07:44 UTC
(Comment #10 From David Konecny 2010-08-16 00:21:21)
Comment 3 Denis Anisimov 2011-02-28 13:21:41 UTC
As I understand yes. The generated service class is Stateless session bean.
But I have provided to you the usecase.
Go through the steps and check it .

In case of wrong J2EE persistence API usage please let me know WHAT exactly 
is incorrect in this usage. I'm not an expert in the JPA area.

This code has not been changed for ages ( in the REST area). Some time ago
the generated code was correct and all works fine for me.

I've realized the problem only with the latest j2ee.persistence version.
Comment 4 Denis Anisimov 2011-02-28 14:04:29 UTC
Actually the situation is not so simple and is VERY strange.
I'm able to reproduce it randomly.

I've checked some previous NB versions and UserTransaction field has been 
generated in early versions also.

After this I've checked the project with GF3.1
It DOES work.

Then I switched to GF3.0.1
After this the same application throw an error on GET HTTP request :

exception

javax.servlet.ServletException: javax.ejb.CreateException: Could not create stateless EJB

root cause

javax.ejb.CreateException: Could not create stateless EJB

root cause

com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Env-Prop: service.CustomerFacadeREST/utx@Field-Injectable Resource. Class name = service.CustomerFacadeREST Field name=utx@java.lang.String@java:comp/UserTransaction@@ into class service.CustomerFacadeREST

root cause

javax.naming.NamingException: Lookup failed for 'java:comp/env/service.CustomerFacadeREST/utx' in SerialContext  [Root exception is javax.naming.NamingException: Lookup failed for 'java:comp/UserTransaction' in SerialContext  [Root exception is javax.naming.NameNotFoundException: Lookup of java:comp/UserTransaction not allowed for Container managed Transaction beans]]

root cause

javax.naming.NamingException: Lookup failed for 'java:comp/UserTransaction' in SerialContext  [Root exception is javax.naming.NameNotFoundException: Lookup of java:comp/UserTransaction not allowed for Container managed Transaction beans]

root cause

javax.naming.NameNotFoundException: Lookup of java:comp/UserTransaction not allowed for Container managed Transaction beans

See also attached exception.

After removal UserTransaction service starts working.
Then I switch to the GF3.1 and RESTful ws is not available at all.
Add/remove UserTransaction attribute doesn't help.

It seems this is still has a relation to GF bug.
But I cannot understand the exact reasons of it......
Comment 5 Denis Anisimov 2011-02-28 14:05:35 UTC
Created attachment 106547 [details]
server stactrace
Comment 6 Denis Anisimov 2011-02-28 14:19:12 UTC
One more notion about GF3.1.
In my case this is b40.

All subsequent tries to deploy THE SAME web project which works for GF3.0.1 are 
failed.
The project is deployed ( it is seen in the Services tab under J2EE server node )
but it doesn't work. I don't now the reasons . The server log is empty except 
deployment Exception (see attachment).  BUT the project is deployed .
The same exception appears on "Test WS" UI action.

Note that it is about activation.jar. The latter jar is THE SAME AS GF3.0.1 case.
There are not any exception neither on deployment nor on WS access with GF3.0.1.
At the same time the exception appears each time with GF3.1
Actually I don't know why GF3.1 cares at all about activation.jar which is inside
project build path not inside war file.
Probably this is absolutely different issue and it could be a deployment problem.
Comment 7 Denis Anisimov 2011-02-28 14:20:13 UTC
Created attachment 106550 [details]
deployment exception
Comment 8 Sergey Petrov 2011-02-28 14:26:35 UTC
ok, it seems there should be no UserTransaction and if I remove all code from xxxFacadeREST and try to insert "Use Entity Manager" it will be generated without UserTransaction. Can't say anything about this "api" usage yet.
Comment 9 Denis Anisimov 2011-02-28 14:43:43 UTC
Let's discuss here only GF3.0.1 behavior.

It seems GF3.1 has a different bug ( at least b40 ) which I have described 
int the original issue #192733 about problem with WS itself 
( not methods invocation ). In the GF3.1 case RESTful WS is not loaded at all.
It is the case when WADL file is not generated on the server side.
Comment 10 Sergey Petrov 2011-02-28 15:03:13 UTC
WebJPATargetInfo.getType (it's used in generation) do not recognize this class as EJB it may be cause by class not being scanned/added to ejb model yet.
It's likely the reason as it works later with insert code action.
Comment 11 Sergey Petrov 2011-02-28 15:09:44 UTC
may be the easiest fix will be to rollback issue 189559, will try to find a better solution.
Comment 12 Sergey Petrov 2011-02-28 15:21:11 UTC
will try runReadActionWhenReady for issue 189559, it may case some delays. corresponding generation shouldn't be called from awt I hope.
Comment 13 Denis Anisimov 2011-02-28 16:10:17 UTC
I don't know how WebJPATargetInfo.getType() works because this is internal impl
of j2ee.persistence. 
But problem could be in absence of @Stateless annotation on moment of 
EntityManager code injection generation.

So I put the EM generation code AFTER @Stateless annotation addition.

This correction doesn't help actually.
And one thing which I also don't understand: why UserTransaction code is 
generated at all? Because "utx" is not used by generated code.
Client API requests only EntityManager injection and its accessor.

changeset:   189603:d4768c1bf101
Comment 14 Sergey Petrov 2011-02-28 16:24:23 UTC
thanks, it still will not resolve complex cases with consequent generation with "old" model, but it can be used (if no transaction is required it doesn't matter if it's EJB or not).
Comment 15 Sergey Petrov 2011-02-28 16:33:02 UTC
Also code to get EM accessors was implemented just for facade from entity, rest from entity, it may be the only case where such generation is possible. So there is a fix: http://hg.netbeans.org/web-main/rev/cf433ccfc9b1 do not create utx member for such cases.
Comment 16 Quality Engineering 2011-03-01 17:58:00 UTC
Integrated into 'main-golden', will be available in build *201103011142* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main/rev/d4768c1bf101
User: Denis Anisimov <ads@netbeans.org>
Log: Correction for probable issue with BZ#196096 -  EntityManager generator generate incorrect code