-->
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: Specifying type of a foreign key association (not integer)
PostPosted: Wed Dec 15, 2004 7:22 am 
Newbie

Joined: Wed Dec 01, 2004 12:27 pm
Posts: 11
Hibernate version:
2.x

Name and version of the database you are using:
Postgresql 7.4

Hello

Let's say I have a parent table with two fields : id as primary key,parent_name varchar as unique key, and a child table with two fields : id, parent_name varchar with a foreign key contraint referencing the column 'parent_name' in the parent table.

I know using a varchar instead of an int as a key is quite dirty (I could just use the parent id instead of the parent name in the child table), but I'm trying to figure out if the resulting problem is a hibernate bug or really a side effect from this bad table modelisation design :

If I try a getHibernateTemplate().find("from Child where parent_name = 'foo'"), I am getting the following exception :

UncategorizedSQLException: (Hibernate operation): encountered SQLException [Bad Integer foo]; nested exception is org.postgresql.util.PSQLException: Bad Integer foo

Ok, so afaik it assumes the table field parent_name is of type integer, however in the child hbm.xml, I have this :

<many-to-one
name="Parent"
class="Parent"
not-null="true"
>
<column name="parent_name" />
</many-to-one>

ant in the parent hbm.xml, I have this :

<property
name="parentName"
type="java.lang.String"
column="parent_name"
unique="true"
length="255"
/>

and :

<set
name="childs"
lazy="true"
inverse="true"
cascade="none"
>
<key>
<column name="parent_name"/>
</key>
<one-to-many
class="Child"
/>
</set>

it looks like it does not understand the <key> column is not an integer, but instead a string. I tried <column name=".." type="java.lang.String"> or a plain <type> marker, but the hbm.xml is getting parsing errors. If I try a straight object hql query (like getHibernateTemplate().find("from Child where this.Parent.parentName = 'foo'")), it does exactly the same integer error.

why is it assuming the parent_name should be an integer, and not a string ?

thanks :)


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.