• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic of a Generic

 
Michael Schembri
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using sample code from a spring persistence book that uses 2 interfaces: DomainObject and GenericDao

The problem is that the interfaces assume that the id of the domain object is always a long integer. In my case I have a domain object that has a composite key (OrderLinePK). I thought that I could fix this by implementing generics in DomainObject

The problem is how am I going to amend GenericDao to use DomainObject's generic parameter.

I know I could easily scrap the DomainObject and GenericDao interfaces, but would like to see how i can adapt the code for my requirements.
 
Stephan van Hulst
Bartender
Pie
Posts: 5590
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I think a generic type is hardly appropriate for an ID. Why do you want to return such a composite key as an ID?
 
Winston Gutkowski
Bartender
Pie
Posts: 10257
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Well, I think a generic type is hardly appropriate for an ID. Why do you want to return such a composite key as an ID?

Totally agree. And furthermore, in database terms, a composite key cannot be an ID. What it can be is a primary key, which is something completely different.

Winston
 
Michael Schembri
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, here's some background info. GenericDao is later implemented as a generic hibernate data access object that uses hibernateTemplate. the get method in hibernateTemplate accepts a serializable parameter as an ID.

The composite key OrderLinePK is a serializable class used to represent the composite key in OrderLine. I would like to amend GenericDao to support keys other than Long ids.

 
Michael Schembri
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is not related to the hibernate/database aspect, I only mentioned that to explain what I would like to achieve.

see comments in the code below

 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without having tested it, I'd try something like this:

What I think I'd really do, if I had enough control over the database, would be to get rid of the composite key, add a surrogate Long primary key (autoincrementing field, for example), and add the composite as an additional unique key.
 
Michael Schembri
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I tried it out and it works

Ok: GenericDao<OrderLinePK, OrderLine>
Error: GenericDao<OrderLinePK, Customer>

 
Michael Schembri
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looked into your suggestion to go for a surrogate pk, makes more sense, tnx.. the unique constraint for composite can be enforced at hibernate level using @UniqueConstraint

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic