-->
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.  [ 6 posts ] 
Author Message
 Post subject: Composite Key with constant foreign key value
PostPosted: Thu Oct 27, 2005 6:19 pm 
Newbie

Joined: Thu Oct 27, 2005 5:57 pm
Posts: 3
I'm trying to create a mapping to a lagacy database and I can't figure out how to get this one situation to work.

Java 1.4.2
DB: Sybase ASA 9
Hibernate 3

Below is my mapping to my flags table. Pretty staight forward.

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="com.imsi.shareholder.business.Flag" table="flags">
      <composite-id>
         <key-property name="tableName" column="table_name"/>
         <key-property name="flagName" column="flag_name"/>
         <key-property name="id" column="integer_value"/>
      </composite-id>
      <property name="description" type="string">
                         <column name="description" length="50" />
                </property>
                <property name="numericDescription" type="long">
         <column name="numeric_desc" precision="10" scale="0" />
      </property>
      <property name="sortOrder" type="long">
         <column name="sort_order" precision="3" scale="0" />
      </property>
      <property name="editable" type="boolean">
                        <column name="editable" precision="1" scale="0" />
                </property>
                <property name="inactive" type="boolean">
                        <column name="inactive" precision="1" scale="0" />
                </property>
   </class>
</hibernate-mapping>



In many other tables I want to make many-to-one references to flags. The problem is that the other tables only contain the integer_value key. The other two keys are basically constants. I was hoping the below mapping would work, but get a BadSqlGrammerException.

Code:
<many-to-one name="method">
    <formula>'FUND'</formula>
    <formula>'METHOD_ID'</formula>
    <column name="method_id"/>         
</many-to-one>


Code:
org.springframework.jdbc.BadSqlGrammarException: Bad SQL grammar [] in task 'Hibernate operation'; nested exception is com.sybase.jdbc2.jdbc.SybSQLException: ASA Error -131: Syntax error near 'FUND' on line 1


Is what I'm trying to do possilbe with the formula tags? Or some other way?

I looked into possibily a custom parameterized user type, but I figured I'd ask here first. If a custom user type is the way to go, can someone point me to a good example?

Thanks,
Brett


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 7:44 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
you can't make foreign key in database for this case, correct ?

you can only if integer_value is unique key - if you have this case then try set many-to-one with unique_key attribute or set that integer_value is id for flag


Top
 Profile  
 
 Post subject: Re: Composite Key with constant foreign key value
PostPosted: Fri Oct 28, 2005 9:33 am 
Newbie

Joined: Thu Oct 27, 2005 5:57 pm
Posts: 3
integer_value is only unique for a give table_name and flag_name. In other words all three are required for the key. The table(s) with the foreign key only contain the integer value. The other two key values are static values, for a given table, and field. My qusetion is how can I set those static keys?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 10:07 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
Quote:
integer_value is only unique for a give table_name and flag_name. In other words all three are required for the key. The table(s) with the foreign key only contain the integer value. The other two key values are static values, for a given table, and field. My qusetion is how can I set those static keys?


what is static field (in relational database) ?

hibernate map relational database concepts to OO concepts - how you define it in your database ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 11:02 am 
Newbie

Joined: Thu Oct 27, 2005 5:57 pm
Posts: 3
Static might be the wrong word, constant is probably better.

For my example above, I have a table with a column called "method_id" that corresponds to the integer value in the flags table. To complete the key I must specify the other two keys, which aren't found in the table that has the "method_id." Theses values are constants, in this case "table_name" = "FUND" and "flag_name" = "METHOD_ID".


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 12:08 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
many-to-one is same foreign key in databases - you can't make foreign key for your case and you can't make many-to-one
I think that is better that you make helper method (in java class) or formula (in mapping)
you can't resolve this case with many-to-one - many-to-one must have primary/unique key in relation table

this is my opinion (i don't sure)


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

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.