Katrangan
Ing pawulangan sadurunge, sampeyan ndeleng Hibernate nggunakake kolom DTYPE VARCHAR khusus kanggo nyimpen jeneng kelas Entitas. Kolom kasebut diarani diskriminator . Iki digunakake kanggo nemtokake kelas sing bakal digawe kanggo baris tartamtu ing database.
Sampeyan bisa ngapusi kolom iki nganggo anotasi @DiscriminatorColumn . Tuladha:
@DiscriminatorColumn(name="column_name", discriminatorType = DiscriminatorType.INTEGER)
Miturut spesifikasi JPA, diskriminator bisa duwe jinis ing ngisor iki:
- STRING
- CHAR
- ONGKO
Nanging, Hibernate ngidini sampeyan nggedhekake dhaptar iki rada. Ndhukung jinis Jawa iki: String, char, int, byte, short, boolean.
Yen kita nggunakake jinis INTEGER, banjur carane encode jeneng kelas Entitas ing? Kanggo iki, anotasi liyane digunakake - @DiscriminatorValue .
Deleng conto:
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="user_type", discriminatorType = DiscriminatorType.INTEGER)
@Entity
class User {
int id;
String name;
LocalDate birthday;
}
@Entity
@DiscriminatorValue("1")
class Employee extends User {
String occupation;
int salary;
LocalDate join;
}
@Entity
@DiscriminatorValue("2")
class Client extends User {
String address;
}
Ing conto ing ndhuwur, kita marang Hibernate sing discriminator bakal nggunakake kolom user_type, kang bakal nyimpen nomer. Yen nyimpen nilai 1, tegese jinis baris yaiku Karyawan, yen 2 disimpen, banjur jinis baris kasebut Klien. Prasaja lan ayu.
@DiscriminatorValue
Nanging ora mung kuwi. Sampeyan bisa ngandhani Hibernate carane napsirake jinis senar nalika diskriminator kasebut NULL.
Iku bener banget prasaja. Sampeyan nemtokake nilai null kanggo @DiscriminatorValue anotasi . Contone, kaya iki:
@DiscriminatorValue("null")
Tuladha kelas:
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="user_type", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("null")
@Entity
class User {
int id;
String name;
LocalDate birthday;
}
Kita wis ngandhani Hibernate manawa saben baris tabel sing duwe NULL ing kolom user_type kudu diinterpretasikake minangka obyek saka jinis pangguna.
Nanging ora mung kuwi. Ana nilai liyane sing menarik kanggo anotasi @DiscriminatorValue.
Punika punika:
@DiscriminatorValue("not null")
Tuladha kelas:
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="user_type", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("not null")
@Entity
class User {
int id;
String name;
LocalDate birthday;
}
Kanthi anotasi iki, kita ngandhani Hibernate manawa saben baris tabel kanthi nilai non-NULL ing kolom user_type kudu diinterpretasikake minangka obyek saka jinis pangguna. Nanging iki mung kanggo cilik yen kelas ora ketemu sing wis nomer sing dibutuhake tegas kasebut.
Mangkene carane bisa digunakake kanggo macem-macem nilai diskriminator:
- 0 - nggawe obyek saka jinis Panganggo
- 1 - nggawe obyek saka jinis Employee
- 2 - nggawe obyek saka jinis Klien
- 3 - nggawe obyek saka jinis Panganggo
- 4 - nggawe obyek saka jinis Panganggo
@DiscriminatorFormula
Nanging ora mung kuwi. Kanggo discriminator kita, kita bisa nemtokake rumus integer sing bakal ngetung nilai kanggo anotasi @DiscriminatorValue .
Ana anotasi khusus kanggo iki, diarani @DiscriminatorFormula .
Tuladha:
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="user_type", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorFormula("case when ‘join’ is not null then 1 else 2 end")
@Entity
class User {
int id;
String name;
LocalDate birthday;
}
@Entity
@DiscriminatorValue("1")
class Employee extends User {
String occupation;
int salary;
LocalDate join;
}
@Entity
@DiscriminatorValue("2")
class Client extends User {
String address;
}
Nilai sing dibalekake dening @DiscriminatorFormula bakal dibandhingake karo Hibernate karo nilai sing ditemtokake ing anotasi @DiscriminatorValue . Kanthi, sampeyan bisa nulis skenario sing cukup rumit:
@DiscriminatorFormula(
"case when address is not null " +
"then 'Client' " +
"else (" +
" case when occupation is not null " +
" then 'Employee' " +
" else 'Unknown' " +
" end) " +
"end "
)