[英]Mapping database lookups to jpa entities
I have the following database tables: lookup and employee. 我有以下数据库表:查找和雇员。
Lookup Table: structure with sample data. 查找表:带有样本数据的结构。
class_name value description
GENDER_CODE 1 Male
GENDER_CODE 2 Female
BANK_CODE 1 HSBC
BANK_CODE 2 CityBank
Employee Table: structure with sample data. 员工表:带有样本数据的结构。
id name gender_code bank_code
1 Yusuf 1 1
2 Maher 1 2
3 Suzan 2 1
What is the best way to map them into JPA entities? 将它们映射到JPA实体的最佳方法是什么?
I tried to map an abstract class to lookup table and use class_name
column as discriminator for the subclasses Gender and Bank and reference the bank and gender as ManyToOne in the employee object.. but I'm getting a class cast exception when the gender_code
and bank_code
has the same value. 我试图将抽象类映射到查找表,并使用
class_name
列作为Gender和Bank子类的判别器,并在employee对象中将bank和sex引用为ManyToOne。.但是,当gender_code
和bank_code
出现类gender_code
bank_code
具有相同的值。
I tried to create views gender_lookup
and Bank_lookup
and map them directly to entities. 我尝试创建视图
gender_lookup
和Bank_lookup
并将它们直接映射到实体。 Again hibernate complains that he can't find a table with such name. 冬眠再次抱怨说他找不到这样的名字的桌子。
I would try to map the lookuptable as n+1 separated entities, one abstract and n childs. 我会尝试将lookuptable映射为n + 1个分离的实体,一个抽象和n个孩子。
Mapped superclass should have SINGLE_TABLE inheritance and child classes need to declare the discriminator. 映射的超类应具有SINGLE_TABLE继承,子类需要声明鉴别符。
Something like this: 像这样:
@MappedSuperclass
@DiscriminatorColumn(name = "class_name")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public abstract class LookupTable{
@Id
private Long vale;
@Column(nullable = false)
private String description;
// Getters and setters
}
@Entity
@DiscriminatorValue("GENDER_CODE")
public class GenderCode extends LookupTable() {
}
@Entity
@DiscriminatorValue("BANK_CODE")
public class BankCode extends LookupTable() {
}
@Entity
public class Employee{
@Id
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private GenderCode genderCode;
@Column(nullable = false)
private BankCode bankCode;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.