Display two classes in one table

Let i have a table

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

And I want to display this table in 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 an HQL: request SELECT m FROM Mag m WHERE m.login = ?, I will get a Mag instance and if I send an HQL: request SELECT w FROM Warrior w WHERE w.login = ?, I will get a Warrior instance
I'm trying to do 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 the discriminator column inherited by the inherited, but I don't have the discriminator.

+5
source share
2 answers

You are looking for a MappedSuperClass , which allows subclasses to inherit annotations from a superclass without requiring a discriminator.

+8

, . ,

utype char (1)

utype = m, Mag

utype = w, Warrior

, .

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "utype", discriminatorType =  DiscriminatorType.CHAR)
@Table(name = "User")  
class User   
{  
   long id;  
   String login;  
}  

@Entity
@DiscriminatorValue("m")  
class Mag extends User  
{  
   String magic;  
}  

@Entity  
@DiscriminatorValue("w")  
class Warrior extends User   
{  
   String weapon;  
}  
+2

All Articles