使用列作为鉴别器从单个 table 加载不同的实体

Load different Entities from a single table with a column as discriminator

我有以下 table 3 种类型的人,男孩、男人和女人:

CREATE TABLE IF NOT EXISTS person(
  id int unsigned NOT NULL AUTO_INCREMENT,
  type VARCHAR(100),
  name VARCHAR(100),
  description VARCHAR(2000),
  PRIMARY KEY (id) 
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

INSERT INTO person (id, type, name, description) VALUES(1, 'child', 'Omar', '');
INSERT INTO person (id, type, name, description) VALUES(2, 'man', 'john doe', '');
INSERT INTO person (id, type, name, description) VALUES(3, 'woman', 'jennifer lopez', '');

我有以下 classes:

@MappedSuperclass
public class PersonEntity {

    @Id
    @GeneratedValue
    @Column(name="id")      
    private Integer id;

    @Column(name="name")        
    private String name;

    @Column(name="description")         
    private String desciption;

    /**
     * @return the id
     */
    public final Integer getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public final void setId(final Integer id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public final String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public final void setName(final String name) {
        this.name = name;
    }

    /**
     * @return the desciption
     */
    public final String getDesciption() {
        return desciption;
    }

    /**
     * @param desciption the desciption to set
     */
    public final void setDesciption(final String desciption) {
        this.desciption = desciption;
    }

}

@Entity
@Table(name = "person")
public class ChildEntity extends PersonEntity {

    //Value child
    @Column(name="type")        
    private String type;

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

}

@Entity
@Table(name = "person")
public class ManEntity extends PersonEntity {

    //Value man
    @Column(name="type")        
    private String type;

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }   
}

@Entity
@Table(name = "person")
public class WomanEntity extends PersonEntity {

    //Value man
    @Column(name="type")        
    private String type;

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }       
}

我的问题是如何让我的已加载 classes 列表直接由带有注释的人区分。即要收回所有女人,只要来找我那种女人。我这样做是因为它们具有所有三个相同的属性并且不想让一个人 class 和方法按类型搜索。


我找到了解决方案:

@Entity  
@Table(name = "person")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type", discriminatorType = DiscriminatorType.STRING)
public abstract class PersonEntity {

    @Id
    @GeneratedValue
    @Column(name="id")      
    private Integer id;

    @Column(name="name")        
    private String name;

    @Column(name="description")         
    private String desciption;

    @Column(name="type", insertable = false, updatable = false)         
    private String type;    

    //getters and setters
}

@Entity
@Table(name = "person")
@DiscriminatorValue("child")
public class ChildEntity extends PersonEntity { 

}

@Entity
@Table(name = "person")
@DiscriminatorValue(value="man")
public class ManEntity extends PersonEntity {

}

@Entity  
@Table(name = "person")
@DiscriminatorValue(value="woman")
public class WomanEntity extends PersonEntity {

}

您可以使用继承策略InheritanceType.SINGLE_TABLE。为此,您需要按如下方式注释您的 PersonEntity class:

@Entity  
@Table(name = "person")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING) 
public abstract class PersonEntity {
...
}

我认为您也需要将其声明为抽象的,但我不是 100% 确定。

然后你的子classes需要以下注释:

@Entity
@DiscriminatorValue(value="child")  
public class ChildEntity extends PersonEntity {
...
}

@Entity
@DiscriminatorValue(value="man")  
public class ManEntity extends PersonEntity {
...
}

@Entity
@DiscriminatorValue(value="woman")  
public class WomanEntity extends PersonEntity {
...
}

我找到了解决方案:

@Entity  
@Table(name = "person")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type", discriminatorType = DiscriminatorType.STRING)
public abstract class PersonEntity {

    @Id
    @GeneratedValue
    @Column(name="id")      
    private Integer id;

    @Column(name="name")        
    private String name;

    @Column(name="description")         
    private String desciption;

    @Column(name="type", insertable = false, updatable = false)         
    private String type;    

    //getters and setters
}

@Entity
@Table(name = "person")
@DiscriminatorValue("child")
public class ChildEntity extends PersonEntity { 

}

@Entity
@Table(name = "person")
@DiscriminatorValue(value="man")
public class ManEntity extends PersonEntity {

}

@Entity  
@Table(name = "person")
@DiscriminatorValue(value="woman")
public class WomanEntity extends PersonEntity {

}