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

Mapping interfaces in Hibernate

 
Cristian Vrabie
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I know this must be a very common situation, but surprisingly i can't find a clear explanation or sample. I'm new to Hibernate and Java in general so if you can please help me to get started I would appreciate it.

Basically this is about mapping an interface rather than a class because I have interfaces for all my business objects. So, I have the User interface

and the UserImpl class

I use the interface through the code. Even in my UserDao class.

The documentation says that I can map an interface like a class so I wrote this User.hbm.xml

All my tries from this startup setup failed. First of all it says that it cannot find a setter for the id field in the User class (so it sees it as class not interface). I would like to keep that setter private so that's one problem. Even though I make the setter public i get all sort of other problems.

What is the standard way to tackle this situation? Any advice, no matter how small is highly appreciated. Thanks!
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, and I definitely could be wrong, but you might have to tell hibernate to use field access for properties and not setters. The thing with an interface is that you can't define instance variables, as that is only in implementation classes.

try adding access="field" to your id tag and see what happens.

Not really sure if that helps you at all.

Mark
 
Cristian Vrabie
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Spritzler:
try adding access="field" to your id tag and see what happens

Hi Mark. I will give it a go but i don't think it will work since the User interface does not have any fields (it's an interface). On top of that this, from what i understand it will force Hibernate to use reflection which is somewhat slower than using setter/getters.

However, I gave it some thought and it actually makes sense to provide my interface with setter and getters for all fields since Hibernate can't possible know what is the name of the field in the class that implements the interface. I can hide the methods I want by creating a proxy object when I need to.

I made it work by specifying the implementing classes (followed an example on the internet):


However what is bugging me is that i have to specify a discriminator even if I have only one implementing class.
 
Cristian Vrabie
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you know. i removed the subclass declaration and the discriminator and it seems to work just fine. So all that was required is that all fields to have getter and setter in the interface. My setup must at had something else wrong earlier.

Mark, I will try your method of making a field accessible by field and see if it works. This way i would theoretically be able to remove the getter setters.
 
Cristian Vrabie
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, it doesn't work. It fails with property not found exception in the "user" class, which is an interface. It seems hibernate does not make the difference. However it might work with supplementary setup. It's work investigating.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic