• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate - problem with inheritance mapping

 
Vladimir Kroupa
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I hope it's okay to post two questions in two days. I have some trouble with inheritance mapping. I need to map a Java interface named Role and two implementing classes - Waiter and ShiftLeader. I tried different mapping strategies from Hibernate Reference Documentation, right now, I'm trying to use strategy 9.1.1. Table per class hierarchy.

This is what my mapping looks like:
Role.hbm.xml:


I'm using the mapped Role class in another mapping, Person.hbm.xml:


With this mapping and Role interface, I get this error:

Well, since Role is an interface, it cannot have any fields.
Is there any special way to map an interface?

I tried to change Role to abstract class with field id, but that lead me to another error.

I undestand that the update statement has failed, but I don't know how to figure out the cause. The Role database table generated by Hibernate has 4 columns: role_id, discriminator, login, and map_key.

I would be grateful for any hints.

Vladimir
[ December 27, 2008: Message edited by: Vladimir Kroupa ]
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again Vladimir, i have to suggestions that maybe will solve the error

1) you're setting yourself the default-access mode to "field", by default it's property. As you correctly said the Interfaces don't have fields just methods you should set the access mode to the interface to property and in the classes to field as you want.

2) there it's an attribute that tells that the class it's not instantiable. The attribute it's "abstract='true'",

I hope this help you solve the problem,

regards,
 
Vladimir Kroupa
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Angel,
I've tried using the abstract attribute, but I didn't realize that I couldn't use field acces! Thank you for help!

However, the update error is still there. I tried to change the collection roles in Person from map to set to be sure that I hadn't made any mistakes there, but it didn't have any effect. The problem must be connected to the Role class, because my test methods pass when I don't call any methods that manipulate with the roles collection.
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally i don't like to map interfaces with hibernate. The times that i've done it never worked for me. Instead i've created an Abstract Parent class that implements that interface and make the childs extends that Abstract class. With this you still be able to declare your collection with the interface.

It could be like this



Regards,
[ December 29, 2008: Message edited by: Angel Taveras ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic