JPA使用复合字段在@entity上存储多个@column
我有一个用户表,我正在使用Hibernate将其读为用户实例。我想在复合>对象下收集许多相关列以传递。因此,我想做类似的事情:
@Entity
@NoArgsConstructor
public class User {
@Id
private long id;
@Column
private String name;
// A bunch of columns
...
private Statistics stats;
}
现在,我想在我的统计对象中的字段中阅读相同用户表中的一些列。
public class Statistics {
@Column
private int x;
@Column
private int y;
// A bunch of columns
...
}
有没有办法实现这一目标?由于某些性能问题,我不喜欢使用单独的表进行统计并使用加入。显然,我可以将字段从统计信息类移至用户类,但我想使用复合对象来改进设计。
I have a user table and I am using Hibernate to read it into User instances. I want to collect a number of related columns under a composite object to pass it around. So, I want to do something like this:
@Entity
@NoArgsConstructor
public class User {
@Id
private long id;
@Column
private String name;
// A bunch of columns
...
private Statistics stats;
}
Now I want to read some of the columns in the same user table into the fields in my Statistics object.
public class Statistics {
@Column
private int x;
@Column
private int y;
// A bunch of columns
...
}
Is there a way to achieve that? I don't prefer to use a separate table for statistics and use joins because of some performance concerns. I can obviously move the fields from the Statistics class into the User class but I want to improve the design by using composite objects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以检查@embeddable和@embedded注释。
正如我从头到头记得的那样,它应该是:
You can check for @Embeddable and @Embedded annotations.
As I remember from top of my head, it should be something like: