facebook

Hibernate many-to-one mapping

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

    markcoth
    Member

    I am new to hibernate and have a question regarding the My Eclise Hibernate mapping generation from the DB Browser.

    I generate a hibernate mapping from a simple database (MySQL) with the following schema:

    
    C REATE TABLE person (
      firstName varchar(20),
      lastName varchar(20),
      PRIMARY KEY  (firstName,lastName)
    ) 
    
    C REATE TABLE shoes (
      firstName varchar(20),
      lastName varchar(20),
      brand varchar(20),
      size double,
      PRIMARY KEY  (firstName,lastName,brand),
      CONSTRAINT FK_shoes_1_person FOREIGN KEY (firstName, lastName) REFERENCES person (firstName, lastName)
    )
     

    The many to one mapping from shoes to person generates an AbstractShoes class with a ShoesKey that contains two references to Person and the brand as a string. Why are the Person references not Strings instead that represent firstName and lastName?? In the Shoes.hbm.xml, the two Person attributes in the key are mapped as follows:

            <composite-id name="id" class="ShoesKey">
                <key-property name="brand" column="brand" type="java.lang.String"/>
                <key-many-to-one name="person" column="firstName" class="Person"/>
                <key-many-to-one name="person1" column="lastName" class="Person"/>
            </composite-id>

    I’m new to this, so any help would be appreciated.

    #235661 Reply

    markcoth
    Member

    Just updated topic!!!! [/b]

    #235887 Reply

    Riyad Kalla
    Member

    In Hibernate everything is done with Objects and Hibernate figures out the magic, in your case your key reference are to the firstName and lastName properties of the Person class.

    #235917 Reply

    markcoth
    Member

    OK….. But the Person class contains a firstName and lastName. Why the need for two “Person” instance variables in the Shoes class? It seems there should be 1 instance of Person – the person to whom the shoes belong. The inverse of this would be a Person owning a list of shoes.

    The Shoes class has two getter methods (getPerson and getPerson1). Like I said, it seems there should be 1 Person owning a specific instance of shoes.

    It seems like the mapping of Shoes should be something like:

    
    <composite-id name="id" class="ShoesKey">
                <key-property name="brand" column="brand" type="java.lang.String"/>
                <key-many-to-one name="person" class="Person">
                     <column name="first_name"/>
                     <column name="last_name"/>
                </key-many-to-one> 
    </composite-id>
    

    One person instance. The foreign key is a composite of first and last name. First name can not be used to locate a person by itself. The same goes for last name.

    #235918 Reply

    Riyad Kalla
    Member

    Ahh mark I agree with you, this is likely a limitation of the mapping analysis, I have filed this to make sure it is covered by future enhancements, sorry for the trouble.

    #236953 Reply

    yuan_b
    Member

    I run into this problem, can anyone help me?

    I got following error using hibernate map generated by DB Explore or the map modified according to Mark’s comment: (It failed on create sessionFactory)

    Caused by: org.hibernate.MappingException: Foreign key (FK8AADCF3EFC136132:ACT_ASSAY_QUEUE [COMPOUND_NUMBER])) must have same number of columns as the referenced primary key (ACT_COMPOUND_BATCH [BATCH_NUMBER,COMPOUND_NUMBER])

    Here is my map:
    <hibernate-mapping package=”com.test”>

    <class name=”ActAssayQueue” table=”ACT_ASSAY_QUEUE”>
    <composite-id name=”id” class=”ActAssayQueueKey”>
    <key-many-to-one name=”actAssayDetail” column=”ASSAY_ID” class=”ActAssayDetail”/>
    <key-many-to-one name=”actCompoundBatch” class=”ActCompoundBatch”>
    <column name=”BATCH_NUMBER” />
    <column name=”COMPOUND_NUMBER” />
    </key-many-to-one>
    </composite-id>

    <property name=”balanceWeight” column=”BALANCE_WEIGHT” type=”long” />
    <property name=”queueDate” column=”QUEUE_DATE” type=”timestamp” />
    <property name=”state” column=”STATE” type=”long” />
    <property name=”actionDate” column=”ACTION_DATE” type=”timestamp” />
    <property name=”manuallyAdded” column=”MANUALLY_ADDED” type=”byte” />
    </class>

    </hibernate-mapping>

    Thank you.

    Bin

    #236955 Reply

    Riyad Kalla
    Member

    Bin,
    In the ACT_ASSAY_QUEUE table, what is your PK made up of in the DB?

    #236960 Reply

    yuan_b
    Member

    I appreciate your help.
    In my ACT_ASSAY_QUEUE table, the primary key is made of (assay_id, batch_number, compound_number), where assay_id is the key on ActAssayDetail table, and (batch_number, compound_number) is the composite primary key on Act_Compound_batch table.

    I also included the map for Act_compound_batch table here:

    <?xml version=”1.0″ encoding=’UTF-8′?>
    <!DOCTYPE hibernate-mapping PUBLIC
    “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
    http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd&#8221; >

    <!– DO NOT EDIT: This is a generated file that is synchronized –>
    <!– by MyEclipse Hibernate tool integration. –>
    <!– Created Thu Sep 08 00:35:13 PDT 2005 –>
    <hibernate-mapping package=”com.test”>

    <class name=”ActCompoundBatch” table=”ACT_COMPOUND_BATCH”>
    <composite-id name=”id” class=”ActCompoundBatchKey”>
    <key-property name=”batchNumber” column=”BATCH_NUMBER” type=”string”/>
    <key-property name=”compoundNumber” column=”COMPOUND_NUMBER” type=”string”/>
    </composite-id>

    <property name=”registrationDate” column=”REGISTRATION_DATE” type=”timestamp” />
    <property name=”batchCreatorId” column=”BATCH_CREATOR_ID” type=”long” />
    <property name=”currentAmount” column=”CURRENT_AMOUNT” type=”double” />
    <property name=”currentAmountUnits” column=”CURRENT_AMOUNT_UNITS” type=”string” />
    <property name=”source” column=”SOURCE” type=”long” />
    <property name=”sourceDetail” column=”SOURCE_DETAIL” type=”string” />
    <property name=”batchOwner” column=”BATCH_OWNER” type=”string” />

    <set name=”actAssayQueueSet” inverse=”true”>
    <key column=”COMPOUND_NUMBER”/>
    <one-to-many class=”ActAssayQueue”/>
    </set>

    <set name=”actAssayQueue1Set” inverse=”true”>
    <key column=”BATCH_NUMBER”/>
    <one-to-many class=”ActAssayQueue”/>
    </set>

    <set name=”actAssayResultSet” inverse=”true”>
    <key column=”COMPOUND_NUMBER”/>
    <one-to-many class=”ActAssayResult”/>
    </set>

    <set name=”actAssayResult1Set” inverse=”true”>
    <key column=”BATCH_NUMBER”/>
    <one-to-many class=”ActAssayResult”/>
    </set>

    <set name=”actProjectCompoundSet” inverse=”true”>
    <key column=”COMPOUND_NUMBER”/>
    <one-to-many class=”ActProjectCompound”/>
    </set>

    <set name=”actProjectCompound1Set” inverse=”true”>
    <key column=”BATCH_NUMBER”/>
    <one-to-many class=”ActProjectCompound”/>
    </set>
    </class>

    </hibernate-mapping>

    #236961 Reply

    Riyad Kalla
    Member

    Bin,
    I’ll admit I’m no hibernate master, so this is a bit like magic to me, are you able to modify the generate hbm.xml file such that it no longer throws an exception, if you are, what change did you need to make in order for it to work? This will help if I can approach the devs with a “in this case, this doesn’t work, you need to do this” type of situation if possible. If not testing just takes longer but it’s still workable.

    #243398 Reply

    mostekcm
    Member

    Any idea when the Composite Foreign-Key generation error will be fixed? It is killing me 🙂

    Thanks

    #243400 Reply

    Brian Fernandes
    Moderator

    Hi,

    Have you tried 4.1M2? We changed the mapping generation routines and this problem should be fixed.

    Let us know how it goes,

    Best,
    Brian.

    #244204 Reply

    campbe1
    Member

    Can someone please post what the differences are between what 4.1M2 generates and 3.X?

    We are not premitted to upgrade our environment until after March 31, and so I need to fix these files by hand.

    Thanks!

Viewing 12 posts - 1 through 12 (of 12 total)
Reply To: Hibernate many-to-one mapping

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