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 170402 - Unusable code generated by 'JPA Controller Classes from Entity Classes'
Summary: Unusable code generated by 'JPA Controller Classes from Entity Classes'
Status: RESOLVED WORKSFORME
Alias: None
Product: javaee
Classification: Unclassified
Component: Persistence (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: Sergey Petrov
URL:
Keywords:
: 171668 (view as bug list)
Depends on:
Blocks:
 
Reported: 2009-08-14 23:18 UTC by Petr Jiricka
Modified: 2010-12-08 20:59 UTC (History)
1 user (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Petr Jiricka 2009-08-14 23:18:20 UTC
1. Create a Java EE 6 webapp with GF v3 as the target
2. Create 'Entity classes from database', e.g. using the DISCOUNT_CODE table in the samples database
3. Create 'JPA Controller Classes from Entity Classes' for this entity class (use 'p' as the package name)
4. Add the following code to index.jsp: <%= new p.DiscountCodeJpaController().findDiscountCodeEntities() %>
5. Run the application

The following exception is thrown:
java.lang.NullPointerException
        at p.DiscountCodeJpaController.getEntityManager(DiscountCodeJpaController.java:31)
        at p.DiscountCodeJpaController.findDiscountCodeEntities(DiscountCodeJpaController.java:123)
        at p.DiscountCodeJpaController.findDiscountCodeEntities(DiscountCodeJpaController.java:115)
        at org.apache.jsp.index_jsp._jspService(index_jsp.java from :59)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:109)
        ...

This is on the line with the following code: return emf.createEntityManager(); 

Where emf is declared as:
    @PersistenceUnit(unitName = "WebApplication103PU")
    private EntityManagerFactory emf = null;

Basically, the @PersistenceUnit annotation is not processed by the server, because the controller is not a managed object.
Comment 1 Sergey Petrov 2009-08-15 09:06:02 UTC
it's not really unusable in my opinion but its up to user to configure usage, do you have any suggestions?
Comment 2 Sergey Petrov 2009-08-15 18:49:04 UTC
My suggestion - radiobutton group/checkbox with option "use annotations" can be added to the wizard,
if user will select "use annotations" it will generate the same class as in current realization, otherwise will use
lookup or static methods from Persistence.
Comment 3 David Konecny 2009-08-18 04:37:40 UTC
Re. "checkbox 'use annotations'" - first the checkbox name should be "create as managed bean" because that's exactly
what you are doing. Adding just annotations without turning the controller into managed bean is not an option as it
would not solve anything. Managed bean option should be available only if controller wizard is executed on Web project
with JSF though.

In scenario given in this issue the user has to do EntityManagerFactory lookup. Perhaps generated controller class could
have extra static method named ala "lookupController()" which would perform lookup. Lookup method should be generated
only for projects which are deployed to a container (that is not in J2SE case).
 
With WebBeans being part of EE6 user should be able to get EntityManagerFactory injected without declaring controller
class as managed bean in EE6 Web project.
Comment 4 Sergey Petrov 2009-08-18 05:38:10 UTC
Usage of EntityManagerFactory passes to a constructor requires first to find this EntityManagerFactory in usage place,
I'm not sure it's easier for user then put some lines in config file. But it's easiest and most general solution.
Lookups solution looks better for me but I'm not sure if there will be no problem with different project types, may be
different servers.

There is no need for checkbox in jsf wizard as annotations work (at least until we have faces-config.xml, not sure how
to made class managed without faces-config). How "With WebBeans being part of EE6 user should be able to get
EntityManagerFactory injected without declaring controller class as managed bean in EE6 Web project." can be done?

There is no need for checkbox in jpa controller wizard in my opinion, because if generation of annotations is possible I
see no much sense to allow generation of smth else.
Comment 5 Sergey Petrov 2009-09-08 10:47:09 UTC
*** Issue 171668 has been marked as a duplicate of this issue. ***
Comment 6 Sergey Petrov 2009-09-18 12:55:43 UTC
If I generate persist method in jpa controller (or in any class) it contain next comment
        /* Add this to the deployment descriptor of this module (e.g. web.xml, ejb-jar.xml):
         * <persistence-context-ref>
         * <persistence-context-ref-name>persistence/LogicalName</persistence-context-ref-name>
         * <persistence-unit-name>WebApplication3PU</persistence-unit-name>
         * </persistence-context-ref>
         * <resource-ref>
         * <res-ref-name>UserTransaction</res-ref-name>
         * <res-type>javax.transaction.UserTransaction</res-type>
         * <res-auth>Container</res-auth>
         * </resource-ref> */

as I see it, persist will not work without addition of this information to deployment descriptor. the same with
jpacontroller. It's possible to add comment to generated jpa controller also if it'snot generated from jsf wizard and
is't added to faces-config.
One more solution to create web.xml and add this information to web.xml in case of jpa controller generation.

Also I can create entity manager factory with 'emf = Persistence.createEntityManagerFactory("WebApplication3PU");' and
it will works the same as in j2se, but it will also require to add entity classes to persistence.xml manually (or
automatically). I'm currently looking into this way, but without addition of entities all queries will fail as entities
will not be found. I can push this change as part fix but I don't see perfect solution for the problem.
Comment 7 Quality Engineering 2009-09-19 10:59:46 UTC
Integrated into 'main-golden', will be available in build *200909190201* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main-golden/rev/c1b368b5f4ce
User: sergeyp@netbeans.org
Log: fix #170402 - modify behavour, now jpa controller is expected to be unmanaged even if created in container managed project and is generated accordingly, may be part fix
Comment 8 Sergey Petrov 2009-09-25 09:54:30 UTC
finally I have no idea on good behavior - > INCOMPLETE. please suggest any realization I will implement, in my opinion
all except implementation of more complex wizard with some options like "include information in deployment descriptor,
pass entity manager to constructor, use plain j2se approach and add class to new persistence unit in persistence xml")
isn't  good enough for replacement of current behavior (or new one I made recently).
after found how persist method is generated my suggestion is to rollback my last commit and just add help text in wizard
for jpa controller, so user will know he will need to do something with generated code.
Comment 9 David Konecny 2010-12-08 20:59:56 UTC
I think this works correctly now. Generated JPA Controller does not have default constructor anymore and so user has to change step 4. Which they can do for example by enabling CDI and creating named bean:

@Named
public class Pokus {

    @Resource
    private UserTransaction utx;
    
    @PersistenceUnit
    private EntityManagerFactory emf;
    
    public List dooo() {
        return new DiscountCodeJpaController(utx,
                       emf).findDiscountCodeEntities();
    }
}

and by calling ${pokus.dooo()} from index.jsp.