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 143523

Summary: JSF CRUD: always uses the first PU from the persistence.xml
Product: javaee Reporter: Dongmei Cao <dongmeic>
Component: PersistenceAssignee: Sergey Petrov <sj-nb>
Status: RESOLVED WONTFIX    
Severity: blocker    
Priority: P3    
Version: 6.x   
Hardware: All   
OS: All   
Issue Type: ENHANCEMENT Exception Reporter:
Attachments: exception
Sample project to reproduce the bug

Description Dongmei Cao 2008-08-11 20:01:53 UTC
To reproduce:
- create a Java Web Application
- New > Persistence Unit for jdbc/sample
- New > Persistence Unit for jdbc/travel
- New > Entity Classes from Database and select jdbc/travel
- New > JSF Pages from Entity Classes and select the entity classes from the previous step
- Run the CRUD app and click on the Show All Person Item link. It fails with HTTP Status 500 (See attached exception)

The reason for the failure is because in PersonJpaController.java, the incorrect persistence unit is used (should be
"WebApplication73PU2"):

    @PersistenceUnit(unitName = "WebApplication73PU")
    private EntityManagerFactory emf = null;
Comment 1 Dongmei Cao 2008-08-11 20:02:59 UTC
Created attachment 67072 [details]
exception
Comment 2 Dongmei Cao 2008-08-11 20:05:22 UTC
Created attachment 67073 [details]
Sample project to reproduce the bug
Comment 3 Matthew Bohm 2008-08-12 04:53:25 UTC
Unfortunately, by the time the entity2jpa or entity2jsf wizard is run in such a case, there is no trace of the mapping
of entities to persistence units. I'm not sure if we want to modify the db2entity wizard somehow to leave one, if we
want to connect to the database(s) (in the event that there's more than one persistence unit) when generating jpa
controllers to deduce the mapping, or if this represents enough of a corner case to let the user manually edit the
annotations. If the user is to edit them manually, we could provide a message on the last panel of the entity2jpa or
entity2jsf wizard to the effect that jpa controllers will refer to the first persistence unit found (shown only when
multiple persistence units exist).
Comment 4 Andrei Badea 2008-08-12 11:52:06 UTC
Not sure what you mean by

> there is no trace of the mapping of entities to persistence units

In case you mean that you don't have access to the persistence.xml file in PersistenceClientEntitySelectionVisual, that
is caused by the usage of EntityClassScope instead of PersistenceScope, as I wrote in issue 142747. PersistenceScope has
a getPersistenceXml() returning the persistence.xml file. You can use Persistence.createGraph() to get all persistence
units. You then need to present the user with the list of units and use the one that (s)he chose throughout the code.

-1 on the suggestion to just display a warning message that the first unit is always used.
Comment 5 Matthew Bohm 2008-08-12 16:48:59 UTC
It is possible for entity classes to exist in a project that were generated from multiple data sources. Each jpa
controller needs to know which persistence unit its associated entity maps to. I cannot expect the user to provide this
information on an entity-by-entity basis; I think that would clutter the UI too much. Therefore, it would seem the
db2entity wizard has to leave some trace of the mapping if we are to cover the use case. The individual entity classes
are not always explicitly listed in the persistence unit, for instance when transaction-type="JTA". I've observed that
PersistenceUnit.getClass2() returns an empty array in these cases. Is there another way of determining which persistence
unit a given entity maps to?
Comment 6 Andrei Badea 2008-08-13 21:01:08 UTC
You cannot say which persistence unit an entity "maps" to because an entity class can be included in more than one
persistence unit. But in the wizard you can ask the user which persistence unit to use for generation. Then you would
only display entities from this persistence unit, which you obtain through PersistenceScope.getEntityMappingsModel(). 

It may happen (although not at all often) that this would also display entities that conceptually are not in the
persistence unit selected by the user, but in another (sibling) one. But this is consistent with what happens at runtime
(all entities on the classpath are included in the persistence unit in Java EE contexts), and also the user can select
which entity classes to generate the controllers for.
Comment 7 Matthew Bohm 2008-09-05 20:12:25 UTC
*** Issue 79675 has been marked as a duplicate of this issue. ***
Comment 8 Matthew Bohm 2008-10-29 02:24:44 UTC
Changing to enhancement.
Comment 9 Sergey Petrov 2010-06-24 09:15:19 UTC
It may have sense to add pu selection combobox in ui if more then one exists
Comment 10 Martin Balin 2016-07-07 08:54:22 UTC
This old bug may not be relevant anymore. If you can still reproduce it in 8.2 development builds please reopen this issue.

Thanks for your cooperation,
NetBeans IDE 8.2 Release Boss