Java 使用列作为鉴别器从单个表加载不同的实体
下表列出了3种类型的人,男孩、男人和女人:Java 使用列作为鉴别器从单个表加载不同的实体,java,database,spring,hibernate,annotations,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
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 {
}
您可以使用继承策略InheritanceType.SINGLE_表。为此,您需要对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 {
...
}
我找到了解决办法:
@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 {
}
我只是在尝试类似的东西,但对我来说不起作用,我总是返回一个空列表。您的查询是什么样子的?不在日志中保留任何查询,我想没有查询不会运行。但是如何加载它们?您可以发布获取实体的代码吗?在我的genericDaoImpl@Override@SuppressWarningUnchecked公共最终列表中,所有listarfinal类类型抛出PersistenceException{Session Session=sessionFactory.getCurrentSession;final Criteria crit=Session.createCriteriatype;List resultado=null;请重试{resultado=crit.list;}捕获HibernateException异常{抛出新的PersistenceExceptionex;}返回resultado;}