This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum used for a primary key pair !

 
walter wang
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all

I encounter a problem when i used Enum type for primary key pair
Does anyone have experienc on this ? what is the solution?

<composite-id name="comp_id"
class="RiskGroupPK">
<key-property name="ruleType" column="RULE_TYPE_KEY"
type="RuleTypeUserType">
</key-property>
<key-property name="key" column="KEY"
type="java.lang.String" length="32">
</key-property>
</composite-id>

RiskGroupPK is a primary key pair , it contains enume RuleType and a string key .

I already get exception , but if I only use enum as a normal property. it
works perfect, only when i used enum and other property to combinate as
a primary key name, it will have Exception as below shown

IllegalArgumentException in class:
RiskGroupPK, getter method of property: ruleType
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of RiskGroupPK.ruleType

Any one has idea how to use enum to build a a primary key pair?

Thanks in advance
 
walter wang
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem is :

usually it is ok to use enum to build composite primary key
through your own defined enumUserType . but in below case. it will have problem

For example


Employee : Person is 1:1 ,

Employee refers to Person 's primary Key PersonPK also as its own Primarykey .
then it is ok in Employee.java and Employee.hbm.xml use EmployeePK instead of PersonPK
as below shown

class Person{
PersonPK comp_id;
//......
}

class PersonPK {
//Person 's Composite PrimaryKey
String firstName;
String lastName;
}


Class Employee {
EmployeePK comp_id;

//......
}

class EmployeePK{
// Employee 's Composite PrimaryKey
String firstName;
String lastName;
}

in Employee.hbm.xml

<composite-id name="comp_id"
class="EomployeePK">


But if PersonPK contains the enum type. like

class PersonPK{
familyName familyname;

String givenName;
}

enum familyName{
Laux, Vogel, Schneider,Schmidt,Pah
}

then you should never use try to use EmployeePK in Employee.java or Employee.hbm.xml,
what you should do is in Employee.java use PersonPK ,
in Employee.hbm.xml, also use PersonPK , other wise you will have
Exception for IllegalArgumentException occurred calling getter of PK class

class EmployeePK{
familyName familyname;
String givenName;
}


I know the work-around ,but what is the real reason behind it?
due to false implemention of genaric type of Hibernate ? or false implementation
of Generic class itself? I think through nice implementation of Hibernate layer
could solve this generic type casting problem

Any hints ?

[ September 13, 2006: Message edited by: walter wang ]
[ September 13, 2006: Message edited by: walter wang ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic