Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping two classes on Single Table

Let's I have table

USER
-id : long
-login : varchar
-weapon: varchar
-magic: varchar

And I want map this Table on two classes (using Hibernate/JPA)

class Mag  
{   
   long id;  
   String login;  
   String weapon;  
}  

and

class Warrior  
{  
   long id;  
   String login;  
   String magic;  
}  

And if I send HQL query : SELECT m FROM Mag m WHERE m.login = ? then I get Mag instance
and if I send HQL query : SELECT w FROM Warrior w WHERE w.login = ? then I get Warrior instance
I try make something like this

@Entity  
@Table(name = "User")  
class User   
{  
   long id;  
   String login;  
}  

@Entity  
class Mag extends User  
{  
   String magic;  
}  

@Entity  
class Warrior extends User   
{  
   String weapon;  
}   

But @Inheritance requared discriminator column, but I haven't discriminator.

like image 481
Ilya Avatar asked May 03 '12 07:05

Ilya


1 Answers

You're looking for MappedSuperClass, which allows the subclasses to inherit the annotations from the superclass without needing a discriminator.

like image 173
beerbajay Avatar answered Sep 19 '22 16:09

beerbajay