I have the following table with 3 types of people, boy, man and woman:
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', '');
And I have the following 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;
}
}
My question is how can I make my list of loaded classes directly discriminated by the kind of person with annotations. Ie to recover all women, just come to me kind of woman. I do so because they have all three the same attributes and do not want to make just one person class and method search by type.
I found the solution:
@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 {
}