Redian新闻
>
JPA Criteria API select question
avatar
JPA Criteria API select question# Java - 爪哇娇娃
r*s
1
for example,
public class Projects {
private Long id;
private Accounts accounts;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Account_ID")
public Accounts getAccounts() {
return this.accounts;
}
}
if i just want to get the ID of the associated Accounts,
how can i avoid using join with the accounts table?
for example, now we may have to
Join accounts = projects.join(Projects_.accounts);
then
selectCols.add(accounts.get(Accounts_.id).alias("projectAcct"));
can i get it directly thru:
projects.get(Projects_.accounts.id) or similarly?
I know mapping another account_id directly is another way,
but checking whether there is any easy way to do this.
Thanks.
avatar
t*e
2
You have to choose between a join or a subselect. Bear in mind that JPA
can't go beyond the capacity of SQL.
avatar
r*s
3
you didn't get my point:
I just want the account_id which is available
in the projects table already. I don't need
other accounts columns. that's why I don't
want another join.

【在 t*******e 的大作中提到】
: You have to choose between a join or a subselect. Bear in mind that JPA
: can't go beyond the capacity of SQL.

avatar
t*e
4
Ok, I was fooled by the naming of "Accounts".
One-to-many may be mapped through a join table instead of a FK, that implies
a JOIN is always needed. To avoid a join, the FK needs to be mapped
explicitly like an attribute in the Projects in addition to the many-to-one
relationship, like this.
public class Projects {
private Long id;
private Accounts accounts;
private Long accountsId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Account_ID")
public Accounts getAccounts() {
return this.accounts;
}
@Column(name = "Account_ID", nullable = false, precision = 22, insertable
= false, updatable = false)
public Long getAccountsId() {
return accountsId;
}
}
avatar
r*s
5
thanks,
i've been aware of this method , as mapping "another account_id directly",
i am asking any simpler way with metamodel?
another related question, on the other way, is, if there is
no FK, how can i do join?
for example, if account_id has no FK to Accounts table,
how to write the join?
It doesn't seem to make any sense at the beginning, however, in reality, it
makes sense in some bug fixing case.

implies
one

【在 t*******e 的大作中提到】
: Ok, I was fooled by the naming of "Accounts".
: One-to-many may be mapped through a join table instead of a FK, that implies
: a JOIN is always needed. To avoid a join, the FK needs to be mapped
: explicitly like an attribute in the Projects in addition to the many-to-one
: relationship, like this.
: public class Projects {
: private Long id;
: private Accounts accounts;
: private Long accountsId;
: @ManyToOne(fetch = FetchType.LAZY)

avatar
t*e
6
If I understand you correctly, what you refer to is theta-join. Here is a
SQL example.
SELECT *
FROM employee
JOIN department
ON employee.DepartmentID = department.DepartmentID; 
In JPQL,
SELECT *
FROM employee e, department d
where e.DepartmentID = d.DepartmentID;

it

【在 r*****s 的大作中提到】
: thanks,
: i've been aware of this method , as mapping "another account_id directly",
: i am asking any simpler way with metamodel?
: another related question, on the other way, is, if there is
: no FK, how can i do join?
: for example, if account_id has no FK to Accounts table,
: how to write the join?
: It doesn't seem to make any sense at the beginning, however, in reality, it
: makes sense in some bug fixing case.
:

avatar
r*s
7
SQL/HQL/JPQL我都知道啊,
就是用Criteria/Metamodel咋写啊?
很大的一个Query都是Criteria写的。

【在 t*******e 的大作中提到】
: If I understand you correctly, what you refer to is theta-join. Here is a
: SQL example.
: SELECT *
: FROM employee
: JOIN department
: ON employee.DepartmentID = department.DepartmentID; 
: In JPQL,
: SELECT *
: FROM employee e, department d
: where e.DepartmentID = d.DepartmentID;

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。