简体   繁体   中英

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?

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.

I tried to create views gender_lookup and Bank_lookup and map them directly to entities. 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.

Mapped superclass should have SINGLE_TABLE inheritance and child classes need to declare the discriminator.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM