facebook

Hibernate Table Mapping

  1. MyEclipse IDE
  2.  > 
  3. General Development
Viewing 15 posts - 1 through 15 (of 21 total)
  • Author
    Posts
  • #212225 Reply

    Thomas Trostel
    Participant

    Ummm … Errr ….

    It seems the way Hibernate mappings are constructed has changed slightly. Can you give me a back of the napkin style run through of how the new tool should be used? I’ll mess around with it for a few and see if I can figure it out in the mean time.

    Thanks!

    Tom

    #212236 Reply

    Thomas Trostel
    Participant

    The new version is having some trouble creating classes with composite keys also … do you have to create the objects in an order if there are one to many relationships?

    #212245 Reply

    Thomas Trostel
    Participant

    YuK … its trying to create this mapping and getting VERY confused

    <class name=”SumRcv” table=”SUM_RCV”>
    <composite-id name=”id” class=”SumRcvKey”>
    <key-many-to-one name=”Invoices” column=”INVOICE_NO” class=”Invoices”/>
    <key-property name=”lineNo” column=”LINE_NO” type=”java.lang.Integer”/>
    </composite-id>

    … then it decides it can’t create the key class

    #212246 Reply

    Riyad Kalla
    Member

    I’ve asked the Hibernate dev to have a look at this, thank you for your patience.

    #212291 Reply

    Thomas Trostel
    Participant

    No problem. I got around the issue coding those classes by hand. The issue is when the composite identity for one table is the foreign link to another table.

    #212352 Reply

    support-jeff
    Member

    Regarding the first question, yes, the mechanism for creating the mappings has changed dramatically – instead of using the old ddl2hbm tooling, we are now using Velocity templates to generate the mappings. This include adding support for composite ids and many-to-one relationships – the previous SQLEditor-based plugin did not do these (well).

    For your second point regarding composite keys, they are supported – the mapping is correct since I take it that the INVOICE_NO column is a foreign key to the INVOICES table and the the LINE_NO column is just a simple attribute that forms the rest of the pk. Correct? If this is supposed to be mapped another way please provide the details.

    Unfortunately, it looks like some fixes committed prior to the GA build did not make it into the build that has been release. damn.

    Also, you have found another bug with this particular use case (comp key is also fk to other table). I will make sure this gets into bugzilla for the first maintenance release (I know one is in the works for other reasons).

    Sorry for the inconvenience. This is a new feature within ME, and has changed significantly since beta2, but I had hoped I had all the test cases down. Bummer.

    #212412 Reply

    Thomas Trostel
    Participant

    No problem Jeff,

    I retooled the entire project with the new classes this afternoon to shake out any other issues which might have been missed. It looks very stable. I would definately say you’re on the right track here. Do you know when you might have a patch release comming out to us? No rush here since we can code around any problems and the current release features have put us *way* ahead of schedule.

    For your second point regarding composite keys, they are supported – the mapping is correct since I take it that the INVOICE_NO column is a foreign key to the INVOICES table and the the LINE_NO column is just a simple attribute that forms the rest of the pk. Correct? If this is supposed to be mapped another way please provide the details.

    Yes thats entirely correct. Unfortunately the Hibernate mapper can’t construct the key class. Everything else gets constructed without a hitch.

    I’ll see if I can make you a small sample code segment which illustrates the problem as a test case for the developers.

    #212490 Reply

    support-jeff
    Member

    A maintenance release is already in the works, and I am working on the bug you discovered as I write this. First there was a typically NPE code problem (bad developer! bad! bad! 😉 but now it gets trickier – what if the composite key is made up of multiple fk’s? It will get resolved. Thanx again for the patience!

    #221231 Reply

    Any news on this bug? I’m using 3.8.3, generated a Hibernate mapping from a simple schema and it doesn’t generate foreign keys references:

    CREATE TABLE group (
           id VARCHAR(32) NOT NULL
         , name VARCHAR(255) NOT NULL
         , PRIMARY KEY (id)
    );
    
    CREATE TABLE user (
           id VARCHAR(32) NOT NULL
         , login VARCHAR(32) NOT NULL
         , passwd VARCHAR(32)
         , PRIMARY KEY (id)
    );
    
    CREATE TABLE user_group (
           user_id VARCHAR(32) NOT NULL
         , group_id VARCHAR(32) NOT NULL
         , PRIMARY KEY (user_id, group_id)
         , CONSTRAINT FK_user_group_1 FOREIGN KEY (user_id)
                      REFERENCES user (id)
         , CONSTRAINT FK_user_group_2 FOREIGN KEY (group_id)
                      REFERENCES group (id)
    );

    Generated:

    <hibernate-mapping package="">
        <class name="UserGroup" table="USER_GROUP">
            <composite-id name="id" class="UserGroupKey">
                <key-many-to-one name="group" column="GROUP_ID" class="Group"/>
                <key-many-to-one name="user" column="USER_ID" class="User"/>
            </composite-id>
        </class>
    </hibernate-mapping>
    #221239 Reply

    Riyad Kalla
    Member

    IIRC those man-to-one’s are the foreign key contraints back to your Group and User objects (Tables)… are they not?

    #221257 Reply

    support-jeff
    Member

    Sho-nuf. Works as advertised. The PK of USER_GROUP consists of two FK refs – one to GROUP and one to USER. The generated class should have a composite key class that contains refs to User and Group class objects.

    In reality, USER_GROUP would be a many-to-many relation that would not be mapped to a class. Instead there would be a m2m defined on one or both entity classes. This kind of mapping will be supported in a future release.

    #222081 Reply

    The PK is correct, but there are no FK’s in the generated mapping… There should be two FK in USER_GROUP, one to USER and another to GROUP.

    #222084 Reply

    support-jeff
    Member

    The composite key contains the only references you will ever get:

    <composite-id name="id" class="UserGroupKey">
            <key-many-to-one name="group" column="GROUP_ID" class="Group"/>
            <key-many-to-one name="user" column="USER_ID" class="User"/>
    </composite-id>

    Are you thinking that you should have separate m2o properties outside of the key? This is not possible – it would really mess things up hibernate-wise.

    Perhaps you should post what you think you should see for a mapping file? This might help clarify for me what you are getting at. According to what you have posted, the mapping is correct.

    #222771 Reply

    gtena3
    Member

    Somehow I’ve got the concept of composite id, but could you please give me a simple java code that would try to retrieve all “TABLE1″ data with USERID=’guest’ and GROUPID=’grp1′

    sample mapping below:

    <class name=”table1″ table=”TABLE1″>
    <composite-id name=”id” class=”table1key”>
    <key-property name=”userid” column=”USERID” type=”java.lang.String”/>
    <key-property name=”groupid” column=”GROUPID” type=”java.lang.String”/>
    </composite-id>

    <property name=”name” column=”NAME” type=”java.lang.Long” />
    <property name=”address” column=”ADDRESS” type=”java.lang.Long” />
    </class>

    thanks.

    #222774 Reply

    Riyad Kalla
    Member

    gtena,
    That is a question for the Hibernate forums and/or reading the Hibernate docs on composite IDs. If you are getting started with Hibernate, the book Hibernate in Action is an absolute must-have.

Viewing 15 posts - 1 through 15 (of 21 total)
Reply To: Hibernate Table Mapping

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