我有以下基本类结构;
@Entity
@Table(name = "users")
public class User{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
...
}
@Entity
@Table(name = "payees")
public class Payee{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch=FetchType.LAZY, targetEntity = User.class)
@JoinColumn(name="user_id", nullable=false)
private User user;
...
}
@Entity
@Table(name = "bank_accounts")
@NamedQuery(name="[jpql-query-name]", query="[the-jpql-query]")
public class BankAccount{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch=FetchType.LAZY, targetEntity = User.class)
@JoinColumn(name="user_id")
private User user;
@ManyToOne(fetch=FetchType.LAZY, targetEntity = Payee.class)
@JoinColumn(name="payee_id")
private Payee payee;
...
}
实体的用例是BankAccount
可以属于User
或Payee
- 但不能同时属于Payee
或User
。此外,ID NAME
---------------------------------
1 Titilade Dangote
2 Kenneth Effiong
3 Hassan Bonnyface
始终属于ID NAME USER_ID
--------------------------------------------------
1 Ngozika Tijani 2
2 Tobiloba Da'Costa 3
3 Johnson Snowden 1
4 Farouk Nnamani 3
。
参见下面的示例数据;
(用户表)
ID PAYEE_ID USER_ID OWNER_NAME
------------------------------------------------------
1 2 null Tobiloba Da'Costa
2 null 3 Hassan Bonnyface
3 1 null Ngozika Tijani
4 null 1 Titilade Dangote
5 4 null Farouk Nnamani
(Payees Table)
id=:bankAccountId
(BankAccounts表)
true
现在的目的是找到id=userId
的银行帐户,如果它由拥有user.id=userId
的用户拥有或由收款人拥有,则返回 false
属于[the-jpql-query]
的用户。如果不是,则返回 SELECT (CASE
WHEN (b.user.id = :userId
OR b.payee.user.id = :userId)
THEN (true)
ELSE (false)
END)
FROM BankAccount b
WHERE b.id = :bankAccountId
。
所以我写了以下查询到那个效果
请参阅下面的(bankAccountId = 2 and userId = 3)
;
ID PAYEE_ID USER_ID OWNER_NAME | Result
------------------------------------------------------ | --------
2 null 3 Hassan Bonnyface | (true)
以下是我预期结果的样本;
(bankAccountId = 1 and userId = 3)
ID PAYEE_ID USER_ID OWNER_NAME | Result
------------------------------------------------------ | --------
1 2 null Tobiloba Da'Costa | (true)
(bankAccountId = 4 and userId = 1)
ID PAYEE_ID USER_ID OWNER_NAME | Result
------------------------------------------------------ | --------
4 null 1 Titilade Dangote | (true)
(bankAccountId = 3 and userId = 2)
ID PAYEE_ID USER_ID OWNER_NAME | Result
------------------------------------------------------ | --------
3 1 null Ngozika Tijani | (true)
[the-jpql-query]
false
但是我的查询(b.payee.user.id=:userId
)一直返回 BankAccount
。我怀疑这可能是由于每当收款人为空时无法处理查询的id=:bankAccountId
部分
重申;
目的是找到
true
User
和id=userId
返回user.id=userId
,如果它由拥有false
的{{1}}拥有或归拥有 属于具有if(date1.getDayOfMonth() == 1 && date1.getDayOfWeek() == SUNDAY) { count++; } }
的用户的收款人。否则, 然后返回for (int i = 1901; i < 2001; i++) { for(int mon =0; mon<12; mon++){ LocalDate date1= LocalDate.of(i,mon,1); if(date1.getDayOfWeek() == SUNDAY) { count++; } } }
请有人指出我如何实现这个目标的正确方向?
答案 0 :(得分:0)
关于null的jpql和sql中的条件行为是非常值得怀疑的。你应该总是尝试使用is null
。
尝试类似:
WHEN (b.USER.id is null) THEN (b.PAYEE.USER.id = :user_id)
ELSE (b.USER.id = :user_id)