- This topic has 20 replies, 8 voices, and was last updated 20 years, 3 months ago by
avajrd.
-
AuthorPosts
-
January 21, 2005 at 1:49 pm #223312
dianellanunezParticipantHi, 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!February 16, 2005 at 11:29 am #225292
Al DevMemberI am facing the same problem.
How to fix this problem??March 23, 2005 at 7:57 pm #227071
avajrdMemberI’m chiming in with a “me too”
March 23, 2005 at 8:23 pm #227072
avajrdMemberOK, 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.
March 23, 2005 at 9:30 pm #227078
support-jeffMemberGood 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.
March 24, 2005 at 12:43 pm #227112
avajrdMemberCool. 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
-
AuthorPosts