MyEclipse: Hibernate Table Mapping

ttrostel - Aug 17, 2004 - 02:04 PM
Post subject: Hibernate Table Mapping
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
ttrostel - Aug 17, 2004 - 03:24 PM
Post subject:
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?
ttrostel - Aug 17, 2004 - 03:59 PM
Post subject:
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
support-rkalla - Aug 17, 2004 - 04:00 PM
Post subject:
I've asked the Hibernate dev to have a look at this, thank you for your patience.
ttrostel - Aug 17, 2004 - 05:03 PM
Post subject:
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.
support-jeff - Aug 17, 2004 - 06:32 PM
Post subject:
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.
ttrostel - Aug 17, 2004 - 08:58 PM
Post subject:
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.

Quote:

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.
support-jeff - Aug 18, 2004 - 04:36 AM
Post subject:
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!
dserodio - Dec 15, 2004 - 09:14 PM
Post subject: Any news for 3.8.3?
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:

Code:
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:

Code:
<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>

support-rkalla - Dec 15, 2004 - 10:05 PM
Post subject:
IIRC those man-to-one's are the foreign key contraints back to your Group and User objects (Tables)... are they not?
support-jeff - Dec 16, 2004 - 01:45 AM
Post subject:
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.
dserodio - Jan 03, 2005 - 12:07 PM
Post subject:
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.
support-jeff - Jan 03, 2005 - 01:19 PM
Post subject:
The composite key contains the only references you will ever get:
Code:
<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.
gtena3 - Jan 14, 2005 - 11:01 AM
Post subject: composite id
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.
support-rkalla - Jan 14, 2005 - 02:10 PM
Post subject:
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.
dianellanunez - Jan 21, 2005 - 07:49 PM
Post subject:
Hi, I have a table with a composite id, that's ok until this composite id is a foreign key in another table. The code generated by the hibernate tool doesn't even work.
<class name="Promotion" table="promotion">
<composite-id name="id" class="PromotionKey">
<key-property name="promoId" column="promo_id" type="java.lang.Integer"/>
<key-property name="subPromoId" column="sub_promo_id" type="java.lang.Short"/>
</composite-id>
........
</class>
This table generates classes Promotion, AbstractPromotion and PromotionKey(this encapsulates the composite PK)

The 2nd table:
<class name="QaNotice" table="qa_notice">
<id name="qaSeq" column="qa_seq" type="java.lang.Integer">
<generator class="assigned"/>
</id>
.....
<many-to-one name="promotion" column="promo_id" class="Promotion" />
<many-to-one name="promotion1" column="sub_promo_id" class="Promotion" />
<many-to-one name="usr" column="user_id" class="java.lang.Integer" not-null="true" />
</class>

generates 2 instances of Promotion in the QANotice class to reference the PK of the same object.
The exception is: Foreign key (qa_notice [promo_id])) must have same number of columns as the referenced primary key (promotion [promo_id,sub_promo_id])
It seems to be the bug that you mentioned already. What can I do to fix it?
Thanks a lot!
alavoor - Feb 16, 2005 - 05:29 PM
Post subject:
I am facing the same problem.
How to fix this problem??
avajrd - Mar 24, 2005 - 01:57 AM
Post subject:
I'm chiming in with a "me too"
avajrd - Mar 24, 2005 - 02:23 AM
Post subject:
OK, I admit I'm new to Hibernate, but I found a way around the problem that is described by dianellanunez above.

Change this definition in the 2nd table hbm.xml file to:

The 2nd table:
<class name="QaNotice" table="qa_notice">
<id name="qaSeq" column="qa_seq" type="java.lang.Integer">
<generator class="assigned"/>
</id>
.....
<many-to-one name="promotion" class="Promotion">
<column name="promo_id">
<column namne="sub_promo_id">
</many-to-one>
<many-to-one name="usr" column="user_id" class="java.lang.Integer" not-null="true" />
</class>


That seemed to get me around my problem of the program blowing up during the "processing one-to-many association mappings" step of configuration. I'm not sure if there are other ramifications yet because my test program is not actually referencing this table yet.

Moderator, shouldn't the DB Explorer be creating this sort of mapping?

BTW, I'm using 8.3.4.
support-jeff - Mar 24, 2005 - 03:30 AM
Post subject:
Good catch. That is definitely a bug. We are working on some massive re-tooling of the hibernate support now, and I will add this to the list of issues to address.
avajrd - Mar 24, 2005 - 06:43 PM
Post subject:
Cool. Would this be part of the 4.0 Beta?

I have an old application that I did a "role your own" ORM several years ago. I've been wanting to look at hibernate so I ran the DB Explorer hibernate mapping tool on the tables in the database. I found several instances like the one above and a different one. Looks something like this:

CREATE TABLE RISK_ASSESSMENT (
SOFTWARE_BUILD VARCHAR2(25 byte) NOT NULL,
RISK_ASSESSMENT VARCHAR2(4000 byte),
PLAN_NAME VARCHAR2(100 byte) NOT NULL,
PLAN_TYPE VARCHAR2(12 byte),
MODIFIED_BY VARCHAR2(15 byte),
MODIFIED_ON DATE,
CONSTRAINT RISK_ASSESSMENT_PK PRIMARY KEY(PLAN_NAME, SOFTWARE_BUILD)
CONSTRAINT RISK_ASSESSMENT_FK FOREIGN KEY(PLAN_NAME, PLAN_TYPE)
REFERENCES TEST_PLANS(NAME, PLAN_TYPE)
)

generates:

<hibernate-mapping package="com.web.hibernate">

<class name="RiskAssessments" table="RISK_ASSESSMENTS">
<composite-id name="id" class="RiskAssessmentsKey">
<key-many-to-one name="testPlans" column="PLAN_NAME" class="TestPlans"/>
<key-property name="softwareBuild" column="SOFTWARE_BUILD" type="java.lang.String"/>
</composite-id>

<property name="riskAssessment" column="RISK_ASSESSMENT" type="java.lang.String" />
<property name="modifiedBy" column="MODIFIED_BY" type="java.lang.String" />
<property name="modifiedOn" column="MODIFIED_ON" type="java.util.Date" />

<many-to-one name="testPlans1" column="PLAN_TYPE" class="TestPlans" />
</class>

</hibernate-mapping>

This causes the "must have same number of columns " error during the foreign key processing of Configuration.
I have found that the below removes the errors

<hibernate-mapping package="com.web.hibernate">
<class name="RiskAssessments" table="RISK_ASSESSMENTS">
<composite-id name="id" class="RiskAssessmentsKey">
<key-many-to-one name="testPlans" class="TestPlans">
<column name="PLAN_NAME"/>
<column name="PLAN_PLAN_TYPE"/>
</key-many-to-one>
<key-property name="softwareBuild" column="SOFTWARE_BUILD" type="java.lang.String"/>
</composite-id>

<property name="riskAssessment" column="RISK_ASSESSMENT" type="java.lang.String" />
<property name="modifiedBy" column="MODIFIED_BY" type="java.lang.String" />
<property name="modifiedOn" column="MODIFIED_ON" type="java.util.Date" />

<many-to-one name="testPlans" class="TestPlans">
<column name="PLAN_NAME"/>
<column name="PLAN_PLAN_TYPE"/>
</many-to-one>
</class>
</hibernate-mapping>

This could be caused by a poor scheme definition in that the primary key does not include PLAN_TYPE.

I have also seen problems with with composite keys:

<hibernate-mapping package="com.web.hibernate">

<class name="FilterPlan" table="FILTER_PLAN">
<composite-id name="id" class="FilterPlanKey">
<key-many-to-one name="filter" column="FILTER_ID" class="Filter"/>
<key-many-to-one name="testPlans" column="PLAN_NAME" class="TestPlans"/>
<key-many-to-one name="testPlans1" column="PLAN_TYPE" class="TestPlans"/>
<key-many-to-one name="testStrategies" column="STRAT_NAME" class="TestStrategies"/>
</composite-id>

<property name="modifiedBy" column="MODIFIED_BY" type="java.lang.String" />
<property name="modifiedOn" column="MODIFIED_ON" type="java.util.Date" />
</class>

</hibernate-mapping>

This is fixed by:

<hibernate-mapping package="com.web.hibernate">

<class name="FilterPlan" table="FILTER_PLAN">
<composite-id name="id" class="FilterPlanKey">
<key-many-to-one name="filter" column="FILTER_ID" class="Filter"/>
<key-many-to-one name="testPlans" class="TestPlans">
<column name="PLAN_NAME"/>
<column name="PLAN_PLAN_TYPE"/>
</key-many-to-one>
<key-many-to-one name="testStrategies" column="STRAT_NAME" class="TestStrategies"/>
</composite-id>

<property name="modifiedBy" column="MODIFIED_BY" type="java.lang.String" />
<property name="modifiedOn" column="MODIFIED_ON" type="java.util.Date" />
</class>

</hibernate-mapping>

Just wanted to add a few more of my findings.

Thanks, Mike
All times are GMT - 6 Hours
Powered by PNphpBB2 © 2003-2004 The PNphpBB Group
Credits