Bug 185253 - Persistence Entity Class From Database Generation - @ManyToOne Issue
Persistence Entity Class From Database Generation - @ManyToOne Issue
Status: RESOLVED FIXED
Product: javaee
Classification: Unclassified
Component: Persistence
6.x
All All
: P3 (vote)
: 6.x
Assigned To: Sergey Petrov
issues@javaee
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2010-04-29 12:17 UTC by mpapamichael
Modified: 2010-08-10 20:16 UTC (History)
1 user (show)

See Also:
Issue Type: DEFECT
:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description mpapamichael 2010-04-29 12:17:07 UTC
when iam importing an innodb database
with relationships. there is a small issue with the @ManyToOne relationships attribute naming generation.

For example:

 if there is a @manytoone relationship between between an address entity and a
person entity.

 using Expression Language in jsf file, if we want to access the PersonId with
 out any changes after entity generation we have to write>
address.details.personID.personID but what is the logical way to write is
 address.details.person.personID.

 i just created entities using a relational database i have in innodb and i
 check all the entities generated, i found out that i have an entity with a
 combined primaryID, and all its fields and getter and setters are generated
the
 way its supposed to be, i will paste a couple of lines.

 public class Tarrif implements Serializable {
     private static final long serialVersionUID = 1L;
     @EmbeddedId
     protected TarrifPK tarrifPK;


     @JoinColumn(name = "CallDirectionID", referencedColumnName =
 "CallDirectionID", nullable = false, insertable = false, updatable = false)
     @ManyToOne(optional = false)
     private Calldirection calldirection;
     @JoinColumn(name = "CallEventID", referencedColumnName = "CallEventID",
 nullable = false, insertable = false, updatable = false)
    @ManyToOne(optional = false)
     private Callevent callevent;

    //more many to one attributes 

     public Tarrif() {
     }

     public Tarrif(TarrifPK tarrifPK) {
        this.tarrifPK = tarrifPK;
    }


     public Calldirection getCalldirection() {
         return calldirection;
     }

     public void setCalldirection(Calldirection calldirection) {
        this.calldirection= calldirection;
     }

     public Callevent getCallevent() {
         return callevent;
     }

     public void setCallevent(Callevent callevent) {
         this.callevent = callevent;
     }


     //more manytoone getter and setters


    i will now paste a couple of lines from a normal entity without a
compinekey 

public class Calldirection implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "CallDirectionID", nullable = false)
    private Integer callDirectionID;
    @Basic(optional = false)
    @Column(name = "CallDirectionName", nullable = false, length = 255)
    private String callDirectionName;
    @JoinColumn(name = "NetworkID", referencedColumnName = "NetworkID",
nullable = false)
    @ManyToOne(optional = false)
    private Network networkID;

   public Calldirection() {
    }

    public void setDisplay(Boolean display) {
        this.display = display;
    }

    public Network getNetworkID() {
        return networkID;
    }

..................................

the Calldirection entity's manytoone attributes should be generated the same
way as the tarrif entity.(for example 

private Network networkId => private Network network  isn't that correct? 

thanks in advance
Comment 1 Sergey Petrov 2010-05-04 14:11:14 UTC
Name is generated based on referenced column name, not entity name.
I need to investigate a bit more but there may be side effects if change this approach to entities names usage. And if there are no problems except strange constructions in code to get id like "abc.abc" if referenced column is "abc" it looks more like enhancement for me.
Comment 2 Sergey Petrov 2010-05-04 14:25:20 UTC
But samples from jpa 2.0 specification use entity name in this case, may look if it can be changed in 6.9
Comment 3 mpapamichael 2010-05-06 00:03:20 UTC
thanks,
if it can be changed it will be very helpful. 

(In reply to comment #2)
> But samples from jpa 2.0 specification use entity name in this case, may look
> if it can be changed in 6.9
Comment 4 Sergey Petrov 2010-05-06 10:58:29 UTC
for future reference:
org.netbeans.modules.j2ee.persistence.entitygenerator.DbSchemaEjbGenerator::generateRelationship(ForeignKeyElement key)
initially generate name based on entity type, but right after first generation have comment
"        /* only use database column name if a column is not required by the
           primary key. If a column is already required by the primary key
           then executing this code would cause the cmr-field name to be
           named cmp-fieldname1. Therefore, we do not change the cmr-field
           name and instead use the name of the other ejb (default).
         */
"
and the name is regenerated based on field name. It require additional investigation as it was implemented more then 3 years ago,
Comment 5 Sergey Petrov 2010-05-06 19:46:21 UTC
don't see a reason for special handling in this place, functionality is changed to use ejb name always, change shouldn't have bad side effect in my opinion effects as it was already supposed behavior but only in special cases.

http://hg.netbeans.org/web-main/rev/122c9a147dc2
Comment 6 mpapamichael 2010-05-10 06:49:22 UTC
thanks for your response, i will be working using the most recent dev built. if i notice anything else i will let you know.

regards

michael

(In reply to comment #5)
> don't see a reason for special handling in this place, functionality is changed
> to use ejb name always, change shouldn't have bad side effect in my opinion
> effects as it was already supposed behavior but only in special cases.
> 
> http://hg.netbeans.org/web-main/rev/122c9a147dc2
Comment 7 Quality Engineering 2010-05-10 09:22:30 UTC
Integrated into 'main-golden', will be available in build *201005100200* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main/rev/
User: 
Log:
Comment 8 mpapamichael 2010-05-10 14:47:49 UTC
Hi guys i just made changes to my database, i tried to use the new entities update feature.

the outcome was i got a lot of duplicates, because i renamed all my foreign key attributes to the entity name(class name), it assumed that those attributes where missing.

these are the attributes already there(these i changed to meet my needs).
 
    @JoinColumn(name = "ServiceProviderID", referencedColumnName = "ServiceProviderID")
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    private Serviceprovider serviceProvider;
    @JoinColumn(name = "SimTypeID", referencedColumnName = "SimTypeID")
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    private Simtype simType; 

these are the newly generated onces, as you can see more or less are the same attributes.

    @JoinColumn(name = "ServiceProviderID", referencedColumnName = "ServiceProviderID")
    @ManyToOne(optional = false)
    private Serviceprovider serviceProviderID;
    @JoinColumn(name = "SimTypeID", referencedColumnName = "SimTypeID")
    @ManyToOne(optional = false)
    private Simtype simTypeID;


(In reply to comment #6)
> thanks for your response, i will be working using the most recent dev built. if
> i notice anything else i will let you know.
> 
> regards
> 
> michael
> 
> (In reply to comment #5)
> > don't see a reason for special handling in this place, functionality is changed
> > to use ejb name always, change shouldn't have bad side effect in my opinion
> > effects as it was already supposed behavior but only in special cases.
> > 
> > http://hg.netbeans.org/web-main/rev/122c9a147dc2
Comment 9 Sergey Petrov 2010-05-11 08:44:55 UTC
yes, update feature isn't full yet and will not be full in 6.9 it hande only initially generated code and also some minor changes for now. feel free to file separate issue/enhancement for better update support.


By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo