I have a (abbreviated) class that looks like this:
@Entity
@Table
@SecondaryTable(
name = "SUPER_ADMIN",
pkJoinColumns = @PrimaryKeyJoinColumn(
name = "PERSON_ID",
referencedColumnName = "PERSON_ID"))
public class Person {
@Id
@Column(name = "PERSON_ID")
private Long personId;
// getters/setters omitted for brevity
}
The SUPER_ADMIN
table has only one column: PERSON_ID
. What I would like to do is add private Boolean superAdmin
to Person
where it would be true
if the PERSON_ID
is present in that table.
Is this even possible? I am using Hibernate as my JPA provider, so I'm open to proprietary solutions as well.
It seems like I should have done more homework. After poking around, I see that @SecondaryTable
does inner joins and not outer joins. Therefore, my idea here will not work at all. Thanks to @Elbek for the answer -- it led me to this revelation.
You can use JPA callback methods.
public class Person {
@Id
@Column(name = "PERSON_ID")
private Long personId;
@Transient
private transient Boolean superAdmin = false;
// This method will be called automatically when object is loaded
@PostLoad
void onPostLoad() {
// BTW, personId has to be present in the table since it is id column. Do you want to check if it is 1?
superAdmin = personId == 1;
}
}
or you can create easy getter method.
public class Person {
@Id
@Column(name = "PERSON_ID")
private Long personId;
boolean isSuperAdmin() {
return personId == 1;
}
}
You can't have an optional relationship with a @SecondaryTable . You do not have any other choice than using a @OneToOne optional relationship in that case.
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.