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 192981 - JPA Controller Classes from Entity Classes generate code which throws an exception
Summary: JPA Controller Classes from Entity Classes generate code which throws an exce...
Status: VERIFIED FIXED
Alias: None
Product: javaee
Classification: Unclassified
Component: Persistence (show other bugs)
Version: 7.0
Hardware: All All
: P3 normal (vote)
Assignee: Sergey Petrov
URL:
Keywords:
Depends on:
Blocks: 189723
  Show dependency tree
 
Reported: 2010-12-07 13:40 UTC by Denis Anisimov
Modified: 2010-12-09 08:54 UTC (History)
2 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
stacktrace (4.44 KB, text/plain)
2010-12-07 13:40 UTC, Denis Anisimov
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Denis Anisimov 2010-12-07 13:40:14 UTC
Created attachment 103675 [details]
stacktrace

This issue is result of investigations for bug #189723 .
- Create a WEB JEE5 project with target server GF2.
- Create entities for table(s) ( I have used sample Derby database and 
tables: customer and discount code ).
- Create JPA controller classes from entities .
- This step is just interface to call JPA controller methods:
create RESTful web service and method of WS which delegates "create()" method
call of JPA controller.
- Use the following code to access to JPA controller :
private CustomerJpaController getJpaController() {
        try {
            EntityManagerFactory emf = (EntityManagerFactory) new
InitialContext().lookup("java:comp/env/persistence-factory");
            UserTransaction utx = (UserTransaction) new
InitialContext().lookup("java:comp/UserTransaction");
            return new CustomerJpaController(utx, emf);
        } catch (NamingException ex) {
            throw new RuntimeException(ex);
        }
    }
- Add XML element into web.xml file :
<persistence-unit-ref>       
        <persistence-unit-ref-name>persistence-factory</persistence-unit-ref-name>
        <persistence-unit-name>WebApplicationPU</persistence-unit-name>
</persistence-unit-ref>
- TopLink Essentials is used as persistence provider .
- Deploy project to GF server.
- Call web service operation ( simple operation could be invoked via plain 
HTTP access via entering URL in browser OR use test RESTful WS action of NB ).
One can use any other way to call "create()" method of generated JPA 
controller as well . Only "create()" method invocation is important.
As result an exception will be thrown .
See attachment.
Comment 1 Sergey Petrov 2010-12-07 14:54:40 UTC
current behavior will generate usage of getTransaction() if class isn't in container managed environment or isn't managed itself, if it's possible to use such controller as unmanaged it's not an issue but just a feature to generate just one of possible controllers instead of choose between managed and unmanaged.
anyway  "managed" is passed  as a parameter so it shouldn't be an issue for 189723.
Comment 2 Sergey Petrov 2010-12-07 15:09:06 UTC
for ref:
in 6.9 next code was used:
    public HelpCategoryJpaController() {
        emf = Persistence.createEntityManagerFactory("WebApplication18PU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(HelpCategory helpCategory) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();

in 7.0 EntityManagerFactory and UserTransaction are used as constructor parameters.

in my opinion even if current code can be used with some configurations, it's more likely to use jpacontroller with container managed project with JTA resources, and default generation should generate appropriate code, need a bit more investigation.
Comment 3 David Konecny 2010-12-07 22:46:07 UTC
(In reply to comment #1)
> current behavior will generate usage of getTransaction() if class isn't in
> container managed environment or isn't managed itself

web project is "container managed environment", right? so getTransaction should not be used. It looks to me like my comment

http://netbeans.org/bugzilla/show_bug.cgi?id=189455#c8

never got implemented. I just generated XxxJpaController classes via "JPA Controller Classes from Entities" wizard and also via "JSF Pages from Entities". And they different which is wrong. One generated for JSF is OK - it uses UserTransaction; first one is wrong as reported by Denis - it uses EntityManager.getTransaction() which cannot be used in JTA transaction type.
Comment 4 Sergey Petrov 2010-12-07 23:17:32 UTC
I need to check how tomcat is reported, also as I know in theory even gf2,3 like servers may support resource_local (according to persistence spec) but in practice do not allow and require JTA pu only.
Comment 5 Sergey Petrov 2010-12-08 12:03:16 UTC
ok, current behavior was introduced based on https://netbeans.org/bugzilla/show_bug.cgi?id=170402 but it wasn't real fix, also there is no entity manager factory etc creation in controller itself, so I'll rollback changeset from issue 170402
Comment 6 Sergey Petrov 2010-12-08 13:52:31 UTC
old default behavior is reverted, only container or not is used now
http://hg.netbeans.org/web-main/rev/dc64c5535f53

also as I see 170402 affect default constructor realization (with inner factory and transaction objects creation).
Comment 7 Quality Engineering 2010-12-09 06:16:24 UTC
Integrated into 'main-golden', will be available in build *201012090001* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main/rev/dc64c5535f53
User: Sergey B. Petrov <sj-nb@netbeans.org>
Log: fix #192981 behavior is reverted, only conteiner or not is used now without additional parameters