英文:
Spring Data JDBC fetch one to one
问题
我有这个领域模型:
```java
final class Project {
private final @Id
@With
long projectId;
private final String projectType;
@With
private final ProjectRecipient projectRecipient;
@With
private final Set<PreCalculationCosts> preCalculationCosts;
@With
private final Set<PostCalculationCosts> postCalculationCosts;
}
问题:
当我调用CrudRepository
中的findById
方法时,属性projectRecipient
会被实例化。
我甚至在日志中看到了为此发出的所有必要的SQL语句。
当我使用自己的Query
时,只有一对多的属性被实例化(没有为与之关联的projectRecipient发出select语句):
select p.* from project p
inner join projectrecipient pr on pr.project = p.projectid
where p.projectid = :projectId
编辑
当我调试findById
方法并将生成的SQL作为Query
值使用时,它以正确的方式被实例化。但问题在于,我的Project
表有大量的列,所以Query
值字符串在我的IDE(A)中有5行这样的长度...
另一方面,我不能使用findById
方法,因为我需要一些特定于Postgres的similar to
子句...
<details>
<summary>英文:</summary>
I have this domain model:
final class Project {
private final @Id
@With
long projectId;
private final String projectType;
@With
private final ProjectRecipient projectRecipient;
@With
private final Set<PreCalculationCosts> preCalculationCosts;
@With
private final Set<PostCalculationCosts> postCalculationCosts;
}
The problem:
When i call the `findById` method from the `CrudRepository` the property `projectRecipient` gets materialized.
I even see all the issued sql statements being necessary for that in the logs.
When i use my own `Query` only the One to Many properties are getting materialized (there is no select statement issued for the one to one related projectrecipient):
```sql
select p.* from project p
inner join projectrecipient pr on pr.project = p.projectid
where p.projectid = :projectId
EDIT
When i debug the findById
method and use this generated SQL as Query
value, it gets materialized the right way. Problem with that is, that my Project
table has tons of columns, so the Query
value string is kind of 5 lines in my IDE(A) ...
On the other hand, i cant use the findById
method because i need some postgres specific similar to
clause ...
答案1
得分: 1
目前还没有替代的方法,必须拼写出完整的SQL语句,包括以关系名称为前缀的引用实体的列和下划线。如果不包括这些列,引用实体将被视为null
。
Spring Data团队正在考虑一种方法,可以提供查询的除选择子句以外的所有内容,但需要相当长的时间才能将这些想法转化为代码,因为还需要一些其他重要的更改。
请注意,您可以将查询或其部分提取为static final
值,这可能会使其更易于理解。
还要注意,您可以将*
表示法与显式列组合在一起(至少在我所使用的数据库中是这样的),所以像这样的写法是可以的:
select p.*, pr.id as recipient_id
当然,这是否是一个好主意可能会引起一些争论。
英文:
Currently there is no alternative to spelling out the full SQL statement including the columns for the referenced entity prefixed with the relationship name and an _
.
If you don't include these columns the referenced entity will be considered to be null
The Spring Data team is thinking about enabling a way to provide everything but the select clause of a query, but it will take quite some time until these thoughts turn into code since the need some other substantial other changes.
Note that you can extract the query or parts thereof into static final
values, which might make it easier to digest.
Also note that you can combine *
notation with explicit columns (at least in the databases I worked with, so something like this would be fine:
select p.*, pr.id as recipient_id
Of course, the question if this is a good idea might cause some debate.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论