休眠映射帮助!

发布于 2024-10-20 17:21:35 字数 1173 浏览 10 评论 0原文

我对 Hibernate 还很陌生,所以我不完全确定我想做的事情是否可能(或者做正确的事情),所以如果不可能,请随意建议其他方法来实现我正在寻找的东西。

我有 2 个实体:竞赛和赛程。他们之间是一对多的关系。我知道如何设置注释来实现此目的,但我正在努力使关系成为双向的。如果我的应用程序运行很长时间,属于比赛的灯具数量就会增加。我访问 Fixture 对象的最常见方法是通过指定特定日期的竞赛(例如 getFixtures(today))。我想我可以通过在Competition类中使用一个Map来实现这一点,该Map将固定装置的日期映射到该日期的固定装置的集合。但我不知道如何在 Hibernate 中进行设置。

下面是一组简化的 POJO 和注释,说明了我到目前为止所拥有的内容:

@Entity
public class Competition {

    // This is what I would like to have.
    private Map<Date, Collection<Fixture>> fixtureMap;

    public Collection<Fixture> getFixtures(Date day) {
        return fixtureMap.get(day);
    }

    // This is all I know how to do.
    private Collection<Fixture> fixtureList;

    @OneToMany(mappedBy = "competition")
    public Collection<Fixture> getFixtureList() {
        return fixtureList;
    }
}

@Entity
public class Fixture {

    private Competition competition;

    @ManyToOne
    public Competition getCompetition() {
        return competition;
    }
}

正如您所看到的,我可以设置关系来访问比赛的整套固定装置。但要获取特定日期的灯具,我必须迭代整个灯具集合并检查每个灯具的日期。有什么方法可以让 Hibernate 处理从日期到夹具集合的映射吗?如果我能做到这一点,那么我将能够使用日期作为地图中的键来简单地访问当天的灯具集合。

I am quite new to Hibernate so I am not entirely sure if what I want to do is even possible (or the right thing to do) so if it is not, feel free to suggest other ways of achieving what I am looking for.

I have 2 entities, Competition and Fixture. There is a One-to-Many relationship between them. I know how to set up the annotations to achieve this, but what I am struggling with is making the relationship bidirectional. If my application were to run for a long time, the number of Fixtures belonging to a competition would build up. The most common way I will be accessing the Fixture objects is via a Competition by specifying a particular date (e.g. getFixtures(today)). I thought I could implement this by having a Map in the Competition class that maps the date of the Fixture to a collection of Fixtures for that date. I have no idea how to set this up in Hibernate though.

Here is a simplified set of POJOs and Annotations that illustrate what I have so far:

@Entity
public class Competition {

    // This is what I would like to have.
    private Map<Date, Collection<Fixture>> fixtureMap;

    public Collection<Fixture> getFixtures(Date day) {
        return fixtureMap.get(day);
    }

    // This is all I know how to do.
    private Collection<Fixture> fixtureList;

    @OneToMany(mappedBy = "competition")
    public Collection<Fixture> getFixtureList() {
        return fixtureList;
    }
}

@Entity
public class Fixture {

    private Competition competition;

    @ManyToOne
    public Competition getCompetition() {
        return competition;
    }
}

As you can see from this I can set the relationship up to access the entire set of Fixtures for a competition. But to get the Fixtures for a specific day I would have to iterate over the entire collection of Fixtures and check the dates of each. Is there any way I can get Hibernate to handle the mapping from Date to a Collection of Fixtures? If I could do that then I would be able to simply access the collection of Fixtures for that day using the Date as the key in the Map.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

浪菊怪哟 2024-10-27 17:21:35

没有直接的注释可以提供帮助。您必须编写一个查询来获取特定日期的灯具。

No direct annotation can help. You will have to write a query for fetching fixtures of particular date.

亣腦蒛氧 2024-10-27 17:21:35

我相信你可以使用 Hibernate Entity Maps。以下线程将很有用:How to persist Map with Annotations
您还可以在 Hibernate Cheatsheet 上查找实体映射

I believe you can use Hibernate Entity Maps. The following thread will be useful: How to persist Map with Annotations
You can also lookup Entity Map on the Hibernate Cheatsheet

丶视觉 2024-10-27 17:21:35
@Entity
public class Competition 
{
   private Map<Date, Collection<Fixture>> fixtureMap;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "competition")
    @MapKey("date")
    public Collection<Fixture> getFixtures(Date day) 
    {
        return fixtureMap.get(day);
    } 
}

@Entity
public class Fixture 
{

    private Competition competition;

    private Date date;

    @Column(name="FIXTURE_DATE")
    public Date getDate() 
    {
        return date;
    }

    @ManyToOne
    public Competition getCompetition() 
    {
        return competition;
    }
}

请记住在保存竞赛对象之前设置双向关系。这意味着您必须在固定装置内设置竞争对象才能保留固定装置对象和关系。

@Entity
public class Competition 
{
   private Map<Date, Collection<Fixture>> fixtureMap;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "competition")
    @MapKey("date")
    public Collection<Fixture> getFixtures(Date day) 
    {
        return fixtureMap.get(day);
    } 
}

@Entity
public class Fixture 
{

    private Competition competition;

    private Date date;

    @Column(name="FIXTURE_DATE")
    public Date getDate() 
    {
        return date;
    }

    @ManyToOne
    public Competition getCompetition() 
    {
        return competition;
    }
}

Remember to setup the bi-directional relationships before saving the Competition object. Meaning you have to set the competition object within fixture to persist the fixture object and the relationship.

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