简体   繁体   中英

SIngle table strategy doesn't working Hibernate

I'm trying to map a class hierarchy to a single table using Hibernate and one table not creating. I add @Inheritance(strategy=InheritanceType.SINGLE_TABLE) but in base there are separate tables.

@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{

    ...

}

Whats wrong?

Your User class should be

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

Then you need to map the extended classes as follows:

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

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

This effectively should create a single table which houses all the fields from User , Manager , and Administrator using a special field that is added by Hibernate called type which will hold values of either manager or administrator .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM