Sélectionner l'objet avec un max id pour un type donnée
Salut
J'ai ces deux classes
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| @Entity
public class AccountOperation {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long accountOperationId;
@ManyToOne
@JoinColumn(name = "lodger_id")
private Lodger lodger;
private BigDecimal balanceAccountValue;
} |
Code:
1 2 3 4 5 6 7 8 9 10 11
| @Entity
public class Lodger implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long lodgerId;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "lodger", orphanRemoval = true)
private List<AccountOperation> accountOperationList;
} |
Je voudrais aller chercher la dernière opération pour chaque lodger
en Sql je fais
Code:
1 2 3 4 5
| select * from account_operation ao1
where ao1.account_operation_id in (
select max(ao2.account_operation_id) as maxAoId
from account_operation ao2
) |
In a table account_operation, I have columns: balance, operation_value, transaction_type, transaction_date, lodger_id
AVec les données suivant
Citation:
balance, lodger_id
20 , 20, 1
10 , 30, 1
50 , 50, 2
70 , 20, 2
60 , 10, 2
j'aurais
Citation:
balance, operation_value, lodger_id
10 , 30, 1
60 , 10, 2
En jpa,
j'ai réussi avec
Code:
select ao1 from AccountOperation ao1 where ao1.accountOperationId in (select max(ao2.accountOperationId) from AccountOperation ao2)
Pourquoi ça fonctionne alors que je n'ai pas fait de jointure?
je me demande si on peut faire mieux?