-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: constraint check on many-to-one seems to be ignored
PostPosted: Thu Feb 02, 2006 8:26 pm 
Beginner
Beginner

Joined: Thu Apr 15, 2004 5:12 pm
Posts: 21
I am trying to retrieve an entity that has a relation to 1 of 2 other entities, depending on the value of a column in the top-level entity.

There is an OrderPartyRelation table, with a JOIN_NAME column and a JOIN_ID_VALUE column (I have no control over this). If for a given OrderPartyRelation, the JOIN_NAME is equal to 'VENDOR', then a many-to-one relation to VENDOR should occur; if it is equal to 'CUSTOMER', then a many-to-one relation to CUSTOMER should occur.

Any alternate ways approaching this would be very much appreciated, but the approach I'm experimenting with at the moment is to have 2 many-to-relations, and to use a constraint like "JOIN_NAME='VENDOR'", but this does not seem to be working.

All info is below.

Thanks for any pointers or information...

Hibernate version: 3.1.2

Mapping documents:

Code:
<hibernate-mapping>
    <class entity-name="OrderPartyRelation" table="ORDER_PARTY_RELATIONS" mutable="false">
        <composite-id name="id" unsaved-value="undefined">
            <key-property name="orderId" type="java.math.BigDecimal">
                <column name="ORDER_ID" not-null="true" sql-type="NUMERIC"/>
            </key-property>
            <key-property name="joinName" type="string">
                <column name="JOIN_NAME" not-null="true" sql-type="VARCHAR"/>
            </key-property>
            <key-property name="joinIdValue" type="string">
                <column name="JOIN_ID_VALUE" not-null="true" sql-type="VARCHAR"/>
            </key-property>
        </composite-id>
        <many-to-one name="Vendor" entity-name="Vendor" cascade="save-update" insert="false" update="false" not-null="true" not-found="ignore" fetch="join">
            <column name="JOIN_ID_VALUE" check="'VENDOR'=JOIN_NAME"/>
            <!-- the check should ensure that there will only be a related vendor when JOIN_NAME='VENDOR' -->
        </many-to-one>
        <many-to-one name="Customer" entity-name="Customer" cascade="save-update" insert="false" update="false" not-null="true" not-found="ignore" fetch="join">
            <column name="JOIN_ID_VALUE" check="'CUSTOMER'=JOIN_NAME"/>
            <!-- the check should ensure that there will only be a related customer when JOIN_NAME='CUSTOMER' -->
        </many-to-one>
    </class>
    <class entity-name="Customer" table="CUSTOMER" mutable="false">
        <cache usage="read-only"/>
        <id name="id" type="java.math.BigDecimal">
            <column name="CUSTOMER_ID" not-null="true" sql-type="NUMERIC"/>
            <generator class="assigned"/>
        </id>
        <property name="customerName" type="string">
            <column name="CUSTOMER_NAME" sql-type="VARCHAR"/>
        </property>
    </class>
    <class entity-name="Vendor" table="VENDOR" mutable="false">
        <cache usage="read-only"/>
        <id name="id" type="java.math.BigDecimal">
            <column name="VENDOR_ID" not-null="true" sql-type="NUMERIC"/>
            <generator class="assigned"/>
        </id>
        <property name="vendorName" type="string">
            <column name="VENDOR_NAME" sql-type="VARCHAR"/>
        </property>
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Code:
Session session = sessionFactory.openSession().getSession(ENT_MODE);
String qs = "from OrderPartyRelation";
Query query = session.createQuery(qs);
ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);
while (results.next()) {
    System.out.println(results.get()[0]);
}


Full stack trace of any exception that occurs:

n/a

Name and version of the database you are using:

Derby 10.1.2.1

The generated SQL (show_sql=true):


select orderparty0_.ORDER_ID as ORDER1_0_, orderparty0_.JOIN_NAME as JOIN2_0_, orderparty0_.JOIN_ID_VALUE as JOIN3_0_ from ORDER_PARTY_RELATIONS orderparty0_

select vendor0_.VENDOR_ID as VENDOR1_2_0_, vendor0_.VENDOR_NAME as VENDOR2_2_0_ from VENDOR vendor0_ where vendor0_.VENDOR_ID=?

select customer0_.CUSTOMER_ID as CUSTOMER1_1_0_, customer0_.CUSTOMER_NAME as CUSTOMER2_1_0_ from CUSTOMER customer0_ where customer0_.CUSTOMER_ID=?

Debug level Hibernate log excerpt:
n/a

There are no warnings or errors, and no exceptions, but the output that the following is the output:

{$type$=OrderPartyRelation, Customer={$type$=Customer, customerName=Joe Bob Briggs, id=1}, id={joinName=CUSTOMER, orderId=1, joinIdValue=1}, Vendor={vendorName=ThatOne Company, $type$=Vendor, id=1}}

{$type$=OrderPartyRelation, Customer={$type$=Customer, customerName=Joe Bob Briggs, id=1}, id={joinName=VENDOR, orderId=1, joinIdValue=1}, Vendor={vendorName=ThatOne Company, $type$=Vendor, id=1}}

{$type$=OrderPartyRelation, Customer=null, id={joinName=VENDOR, orderId=2, joinIdValue=2}, Vendor={vendorName=Troove, $type$=Vendor, id=2}}

{$type$=OrderPartyRelation, Customer={$type$=Customer, customerName=Joe Bob Briggs, id=1}, id={joinName=CUSTOMER, orderId=2, joinIdValue=1}, Vendor={vendorName=ThatOne Company, $type$=Vendor, id=1}}


From looking at the sql that is generated and the results, it seems that the @check attribute is being ignored.

My data is as follows for that table:
OrderPartyRelation
Code:
ORDER_ID   JOIN_NAME   JOIN_ID_VALUE
1   CUSTOMER   1
1   VENDOR   1
2   VENDOR   2
2   CUSTOMER   1


I want to be able to retrieve 2 entities for ORDER_ID=1, one of which would have a non-null vendor and null customer, and the other of which would have a non-null customer, and ditto for vendor.

Should this work with the check I have above? Any alternate ideas for achieving this?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.