JPA使用复合字段在@entity上存储多个@column

发布于 2025-02-10 13:19:27 字数 624 浏览 2 评论 0原文

我有一个用户表,我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待天淡蓝洁白时 2025-02-17 13:19:27

您可以检查@embeddable和@embedded注释。

正如我从头到头记得的那样,它应该是:

@Embeddable
public class PhoneNumber{
}

@Entity
public class Customer
{
    @Embedded
    private PhoneNumber phoneNumber;
}

You can check for @Embeddable and @Embedded annotations.

As I remember from top of my head, it should be something like:

@Embeddable
public class PhoneNumber{
}

@Entity
public class Customer
{
    @Embedded
    private PhoneNumber phoneNumber;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文