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

时间:2015-05-23 11:45:17

标签: java database spring hibernate annotations

我的下表有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', '');

我有以下课程:

@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;
    }       
}

我的问题是如何使我的加载类列表直接被带注释的人区分。即恢复所有女性,只是来找我一个女人。我这样做是因为它们具有三个相同的属性,并且不希望按类型只进行一个人类和方法搜索。

我找到了解决方案:

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

}

2 个答案:

答案 0 :(得分:0)

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

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

我认为你也需要宣布它是抽象的,但我并非100%肯定。

然后您的子类需要遵循注释:

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

答案 1 :(得分:0)

我找到了解决方案:

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

}