[英]JPA - Check existence of value in another table as Boolean value
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
. SUPER_ADMIN
表只有一列: 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. 我想做的是将
private Boolean superAdmin
添加到Person
中,如果该PERSON_ID
中存在PERSON_ID
,那将是true
。
Is this even possible? 这有可能吗? I am using Hibernate as my JPA provider, so I'm open to proprietary solutions as well.
我将Hibernate用作我的JPA提供程序,因此我也欢迎专有解决方案。
It seems like I should have done more homework. 看来我应该做更多的作业。 After poking around, I see that
@SecondaryTable
does inner joins and not outer joins. 戳一戳之后,我看到
@SecondaryTable
执行内部联接,而不是外部联接。 Therefore, my idea here will not work at all. 因此,我的想法根本行不通。 Thanks to @Elbek for the answer -- it led me to this revelation.
感谢@Elbek的回答-它使我得到了这个启示。
You can use JPA callback methods. 您可以使用JPA回调方法。
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 . 您不能与@SecondaryTable建立可选关系。 You do not have any other choice than using a @OneToOne optional relationship in that case.
在这种情况下,除了使用@OneToOne可选关系外,您别无选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.