2016-04-23 83 views
2

我試圖將類層次結構映射到使用休眠和一個表不創建。我添加@Inheritance(戰略= InheritanceType.SINGLE_TABLE),但在基地有單獨的表。SIngle表戰略不工作休眠

@MappedSuperclass 
@Inheritance(strategy=InheritanceType.SINGLE_TABLE) 
public abstract class User implements UserDetails { 
    @Id @GeneratedValue 
    private int id; 
    ... 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 
    ... 
} 




@Entity 
public class Manager extends User{ 

    ... 

} 

@Entity 
public class Administrator extends User{ 

    ... 

} 

怎麼了?

回答

1

User類應該是

@Entity 
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) 
@DiscriminatorColumn(field = "type", discriminatorType = DiscriminatorType.STRING) 
public class User implements UserDetails { 
    /* your original stuff */ 
} 

然後,你需要如下映射擴展類:Manager

@Entity 
@DiscriminatorValue("manager") 
public class Manager extends User { /* stuff */ } 

@Entity 
@DiscriminatorValue("administrator") 
public class Administrator extends User { /* stuff */ } 

這實際上應該創建容納所有字段從User一個表, ,和Administrator使用由Hibernate添加的稱爲type的特殊字段,其將保存值爲manageradministrator

+0

謝謝,它的工作 –