Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

Hibernate and String primary key

Thread view: 
RoB - 14 Dec 2005 10:45 GMT
I have a problem with mapping String primary key in hibernate:
1) I have table countries (country_code varchar(2) PK, ...)
2) I try mapping this primary key to hibernate, I found something like this:
<id name="code" type="string" unsaved-value="null">

<column name="country_code" sql-type="char(2)"/>

<generator class="uuid.hex"/>

</id>

but I would like to set country_code by hand, not by generate, for example,
EN -> England, PL -> Poland etc.
is it posiible?

3) I tested it in junit and with or without <generator...> it does not work.
4) maybe I need add new column country_id int ?

I propably doing something wrong, but I'm a new hibernate not specialist ;-)

best regards
RoB
BartCr - 14 Dec 2005 10:57 GMT
>From the top of my head:

Using 'assigned' as generator class lets the application set the
"natural" key. Search in that direction,

Bart
RoB - 14 Dec 2005 11:32 GMT
> Using 'assigned' as generator class lets the application set the
> "natural" key. Search in that direction,
I found something like that and I try it before You wrote this post :) and
still something is wrong

Now I've simple table countries (country_code varchar(2), country_name
varchar(100)),
My hbm.xml file:
<hibernate-mapping package="...">

<class name="Country" table="countries" lazy="false">

   <id name="code" type="string">

   <column name="country_code" sql-type="char(2)" />

   <generator class="assigned"/>

   </id>

   <property name="name" column="country_name" />

</class>

</hibernate-mapping>

in my test I create new Country object:

and if  I run test, in line CountryDAO.save(country) I have NullPointer, but
if I do country.toString(), i have:

....Country [

code=PL

name=Poland

]

probably problem is in my dao, thanks for Your help

RoB


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.