为什么我们不能在 Spring Data JPA Final 中也创建一个关系属性?
所有者实体具有@manytoone
- @onetomany
与教师实体的关系。 这样的每个人注释时
@Entity
public class Product {
...
@ManyToOne(cascade = MERGE)
private final Owner owner;
当我在另一侧(在所有者类中)
@Entity
public class Owner {
...
@OneToMany(mappedBy = "owner", cascade = MERGE)
private final List<Product> products;
,现在发生的是“所有者”
在行mappedby =“ lands” 变成红色。悬停在它上,我发现无法找到所有者属性的错误。
错误:找不到逆属性
该解决方案仅仅是为了删除产品类中属性所有者中的final
关键字。
它成为私有所有者
,错误消失了。我不明白为什么添加关键字最终
会导致此问题。
为什么会发生这种情况? 有解决方法吗?我还能使所有者属性最终吗?
Getgo的主要想法是使产品类不变。尽管这是一个挑战,但我设法为大多数事情找到了解决方法,但是我不知道如何解决这个问题。
An Owner entity has a @ManyToOne
- @OneToMany
relationship with the teacher entity. When I annotate each like this
@Entity
public class Product {
...
@ManyToOne(cascade = MERGE)
private final Owner owner;
On the other side, in the Owner Class,
@Entity
public class Owner {
...
@OneToMany(mappedBy = "owner", cascade = MERGE)
private final List<Product> products;
What happens now is that "owner"
in the line mappedBy = "owner"
turns red. Hovering over it, I get the error that the owner attribute cannot be found.
The error: Cannot find inverse attribute
The solution was simply to remove the final
keyword in the attribute owner in the Product class.
It becomes private Owner owner
and the error disappears. I don't understand why adding the keyword final
causes this problem.
Why does this happen?
Is there a workaround? Can I still make the owner attribute final?
The main idea from the getgo was to make the product class immutable. While this is a challenge, I've managed to find a workaround for most things, but I didn't know how to fix this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JPA 不支持不变性。 JPA 实体确实需要属性的默认构造函数以及 getter 和 setter。
并且这些字段一定不是最终的。
从技术上讲,实现一个忽略最终属性的 ORM 是可能的,但为什么要这样做呢?
关键字final表示:这在构造时被分配一个值,并且此后永远不会改变。对于通过 no-args-constructor 构造然后在第二步中填充的 JPA 实体来说,情况并非如此。
如果您正在寻找对不可变类(带参数的构造函数、“wither”方法)有更好支持的 ORM,您可能需要查看 Spring Data JDBC。
全面披露:我是 Spring Data 开发人员,从事 Spring Data JPA 和 Spring Data JDBC 工作。
JPA does not support immutability. A JPA entity does require a default constructor and getters and setters for properties.
And the fields must not be final.
Technically it would be possible to implement an ORM that ignores final attributes but why should it?
The keyword final says: This gets assigned a value at construction time and never changes after that. This is just not true for JPA entities which get constructed via no-args-constructor and then populated in second step.
If you are looking for an ORM that has better support for immutable classes (constructor with arguments, "wither" methods) you might want to check out Spring Data JDBC.
Full disclosure: I'm Spring Data developer working on both Spring Data JPA and Spring Data JDBC.
我认为您对不变性概念的理解是错误的。不变性是 Java 语言强制提出的一个概念。例如,由于安全性、缓存等原因,String 类是不可变的。但在您的情况下,Product 是一个实体类,如果您将其保存在持久层中,它本身就已经是唯一的。因此,即使您使 Product 类不可变,您将如何在两次应用程序加载期间保持一致性?如果您尝试创建仅由一个所有者拥有的产品,请进行数据库检查,而不是尝试使其在内存中不可变。
I think you have understood immutability concept wrong. Immutability is a concept being forced by the Java language. For example String class is immutable because of the security, caching etc. But in your case Product is an entity class and if you save it in a persistent layer, it is already unique on it's own. So even if you make the Product class immutable, how are you going to keep that consistency during two application loads?. If you are trying to make a Product having owned by only one owner, then do a db check rather than trying to make it immutable in memory.