facebook

Hibernate foreign keys mapping wrong getters and setters

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 15 posts - 1 through 15 (of 19 total)
  • Author
    Posts
  • #239632 Reply

    zoked
    Member

    Hi! 🙂

    I found something weired that took me a while to figure out, but I now do think that it’ sjust some getters and setters generated wrong when mapping via DatabaseExplorer… (?)

    A little bit of explanation:

    There is 3 tables (not finished, butworking for test reasons):

    #239633 Reply

    zoked
    Member
    
    TABLE v_media (
           id BIGINT(20) NOT NULL AUTO_INCREMENT
         , PRIMARY KEY (id)
    ) type=InnoDB;
    
    TABLE v_media_printrun (
           id BIGINT(20) NOT NULL AUTO_INCREMENT
         , media_id BIGINT(20) NOT NULL DEFAULT 0
         , PRIMARY KEY (id)
    ) type=InnoDB;
    
    TABLE v_media_printrun_playlength (
           media_id BIGINT(20) NOT NULL
         , media_printrun_id BIGINT(20) DEFAULT NULL
         , side INT(11) NOT NULL DEFAULT 0
         , playlength TIME
         , PRIMARY KEY (media_id, media_printrun_id, side)
         , FOREIGN KEY (media_id)
                        REFERENCES v_media(id)
         , FOREIGN KEY (media_printrun_id)
                        REFERENCES v_media_printrun(id)
    ) type=InnoDB;
    
    
    #239634 Reply

    zoked
    Member

    So, v_media_printrun_playlength has 2 foreign keys to different tables.

    Mapping those 3 tables via DatabaseExplorer worksprettygoog, everything semms to be extremely good, in AbstractVMedia and AbstractVMediaPrintrun the getters and setters for the set are created as well asthere is an AbstractMediaPrintrunPlaylengthKey to generate a key in in that there also exists a getter and setter for both, the VMedia and the VMdiaPrintrun. Everything really looks great and should be working…

    So I wrote a tiny test class openig hibernate session and trying to fill the tables with values…
    nope.

    I gotan errorlike this:

    
    %%%% Error Creating SessionFactory %%%%
    org.hibernate.PropertyNotFoundException: Could not find a setter for property vMediaPrintrunPlaylengthSet in class de.vinyldealer.hibernate.VMediaPrintrun
        at org.hibernate.property.BasicPropertyAccessor.createSetter(BasicPropertyAccessor.java:156)
        at org.hibernate.property.BasicPropertyAccessor.getSetter(BasicPropertyAccessor.java:150)
        at org.hibernate.mapping.Property.getSetter(Property.java:245)
        at org.hibernate.tuple.PojoTuplizer.buildPropertySetter(PojoTuplizer.java:241)
        at org.hibernate.tuple.AbstractTuplizer.<init>(AbstractTuplizer.java:74)
        at org.hibernate.tuple.PojoTuplizer.<init>(PojoTuplizer.java:54)
        at org.hibernate.tuple.TuplizerLookup.create(TuplizerLookup.java:47)
        at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:218)
        at org.hibernate.persister.entity.BasicEntityPersister.<init>(BasicEntityPersister.java:400)
        at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:104)
        at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
        at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:211)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1005)
        at de.vinyldealer.hibernate.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:49)
        at localhost.CopyVinyls.main(CopyVinyls.java:220)
    
    #239635 Reply

    zoked
    Member

    So, I checked for the getters and setters…but they exist!

    Now I searched the net but didn’t find anything… so I tried my last idea:

    the variables are vMedia and vMediaPrintrun.
    the getters are getVMedia and getVMediaPrintrun.
    I put in other getters: getvMedia and getvMediaPrintrun

    I tried…and: different error: the setters are wrong.
    So, same procedure for the setters.
    Another try…and (I nearly expected it): no getter for vMediaPrintrunPlaylengthSet in VMedia…
    I checked the (existibg) getter there… and again it was getVMediaPrintrunPlaylengthSet().
    I changed it to getvMediaPrintrunPlaylengthSet(), same for the setter, getter in VMediaPrintrun and setter in VMediaPrintrun.

    I tried….
    Yepp!
    It works!

    😯 I hope i’s not too weired, I’m not that good in explaining things…
    My question now is, is there a bug in the generation of the abstracts and classes? maybe one toUpperCase() too much, or somwhere else too few?
    Did somebody else experience this?
    Will it be changed?

    😳

    Regards,

    zoked
    (who is happy with MyEclipse and really likes to work with it!)

    #239636 Reply

    zoked
    Member

    Sorry for the typos… I’m going to improve…promise 🙂

    #239652 Reply

    emylonas
    Member

    Yes, man… conincidently i’m having the same problem today and looking for help. Looks like you post it’s gonna help me.

    This is not the first weird thing i find in MyEclipse, and this is only the second day i’m working with it.

    This morning it just didn’t map Pk’s and FK’s for 17 of my tables when mapping from Database Explorer. I tried to remap thes tables, and only them, and it worked. Don’t know why it didn’t work the first time…

    Anyway, i’ll try to do what you described about this getter/setter problem.

    Thanks for the post

    😉

    #239656 Reply

    emylonas
    Member

    You were right, this one field i was haing a problem had the first letter capitalized on the get/set methods. I wish it had not done that, but at least it’s a minor and fixable bug.

    Thanks a lot for you post, even though you had no doubts and were just trying to help people out.

    I just wanted to let everydoby know i had the same problem, which seems to be a systemic error.

    Thanks a lot

    #240072 Reply

    zoked
    Member

    Oh, good to hear that this helped somebody 😀

    I now wonder if that problem will be fixed in future versions? It’s not really fun to put all that code in by hand if you setup abiggerproject and have a lot of mappings… not talking about re-mappings… 😕

    #240079 Reply

    Riyad Kalla
    Member

    emylonas,
    Please post the name of the tables you had problems with as they are in the DBMS and then the name of the getters/setters as they were generated.

    We are looking at starting some big improvements to hibernate in 4.1 and they will continue through the 5.0 release, this should be ironed out as part of this process.

    #240127 Reply

    zoked
    Member

    Ya!
    That’s good news.
    Thanks a lot!
    8)

    #240520 Reply

    tarantula
    Participant

    zoked,

    Hibernate works with Java objects conforming to the JavaBeans API, so MyEclipse’s code generation produces Java classes that use a specific naming convention for methods and variables.

    In JavaBeans terms, a private variable with a getter and a setter method is a property. For more information about Hibernate and JavaBean properties I suggest you visit the links below.

    http://www.hibernate.org/16.html

    http://java.sun.com/docs/books/tutorial/javabeans/properties/properties.html

    Hope it helps,
    Ian

    #240560 Reply

    zoked
    Member

    Hi hlavatsi.

    I do know, that the naming convention MyEclipse generates is right.
    Still, it doesn’t work.
    The post I started with, comes up with a possible workaround, ’til this issue is fixed within MyEclipse.

    Thanks!

    Greetz,
    zoked

    #242825 Reply

    sriramn
    Member

    I have experienced the same problem with Hibernate 3.0.5. Hibernate compains that getter is not found for a property like ‘aAndB’ eventhough you have correct getter ‘getAAndB’.

    Solution: Do not use property names that starts with a lowercase letter followed by a uppercase letter. 😆

    #242826 Reply

    sriramn
    Member

    Property names like ‘aAndB’ are not valid java bean property names.

    Please read the following.
    ————————————————————————————————————————
    As TREND conforms to the JavaBeans strategy to find the name of an attribute of a business object based on getter and setter method names, the JavaBeans naming convention imposes some restrictions on legal instance variable names in the TREND framework context. JavaBeans imposes the following strategy on how variable names are evaluated based on getter and setter method names. The get- or set- part of the method name is removed and the remaining part is treated as follows:

    If it starts with a single uppercase letter this letter is converted to lowercase.
    It it starts with multiple uppercase letters, the remaining word is remained unchanged.

    This strategy implies, that your instance variables should not start with a single lowercase character followed by one uppercase letter. An examples for an invalid variable name would be for instance xPosition. As a getter method for this variable should be named getXPosition(), rule 2 of the JavaBeans naming strategy would imply, that the name of the instance variable is XPosition. In this particular case we will treat the name of a business object’s attribute as XPosition (starting with an uppercase letter). This does not imply, that you necessarily need to have the corresponding instance variable start with an uppercase letter. Instance variables start always with lowercase letters.

    #242833 Reply

    zoked
    Member

    Yeah…now that gets us closer to the solution….

    So the _real_ issue could be the naming of the database tables!
    I’ll try this out…

    Thanks!

Viewing 15 posts - 1 through 15 (of 19 total)
Reply To: Hibernate foreign keys mapping wrong getters and setters

You must be logged in to post in the forum log in