开发者

Hibernate: Multiple associations to the same class

I need to associate an entity with two lists of other entities – both lists containing entities of the same type. It looks something like this:

@Entity
public class Course {
    private List<Test> preTests;
    @OneToMany(cascade= javax.persistence.CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = “course”)
    @OrderBy("testNumber")
    public List< Test > getPreTests() {
        return preTests;
    }

    public void setPreTests(List< Test > preTests) {
        this. preTests = preTests;
    }

    private List<Test> postTests;
    @OneToMany(cascade= javax.persistence.CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = “course”)
    @OrderBy("testNumber")
    public List< Test > getPostTests() {
        return postTests;
    }

    public void setPostTests(List< Test > postTests) {
        this. postTests = postTests;
    }
}

Now, I haven’t even bothered to try this per se because it seems obvious that Hibernate would have no way to distinguish which Tests go into preTests and which into postTests. Correct me if I’m wrong, but the only information it has to work with are the foreign keys in the Test table pointing to the Course record, and both the pre- and post-Tests will point at the same Course. My next thought was to create explicit PreTest and PostTest subclasses of Test, and have one List<PreTest> and one List<PostTest>, but that leads to the infamous “mappedBy reference an unknown target entity property: course” problem, which the folks at Hibernate claim – for reasons that strike me as fussy –开发者_如何学Go is by-design and therefore not likely to go away.

My current thought is to use separate join tables to hold the two associations. I see no reason why that shouldn’t work. It also suits my desire to keep all explicit SQL (or even HQL) out of the entity definitions, and to resist the coercion to create weird composite keys or otherwise write code that reflects only the quirks of my persistence framework rather than my object design.

Still, before I commit myself to that course, I’m asking whether any of you has a cleaner solution or knows of flaws in my reasoning here.

Thanks in advance.

Michael


I don't know if this is a really good idea or not but you could include a

@org.hibernate.annotations.Where

annotation on your association mapping and use a discriminator column on your Test table so that you can tell a Pre from a Post test when loading the Test instances.


One option is the Table per class hierarchy method of inheritance mapping, which it sounds like you started doing, but didn't go all the way with.

Another option is, as otherwise mentioned, using the @Where annotation.

A third option, which I often use for similar cases, is to map a single collection for all Tests related to the Course, and then add helper methods to return just the desired subsets when you want to work with pre or post tests. Example code:

    @Entity
    public class Course {

    private List<Text> tests;

    @OneToMany(cascade= javax.persistence.CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = “course”)
    @OrderBy("testNumber")
    public List< Test > getTests() {
        return tests;
    }

    public List< Test > getPreTests() {
        List<Test> preTests = new ArrayList<Test>();
        for (Test test : preTests) {
           if (test.isPreTest()) {
               preTests.add(test);
           }
        }
        return preTests;
    }


    public List< Test > getPostTests() {
        List<Test> postTests = new ArrayList<Test>();
        for (Test test : postTests) {
           if (test.isPostTest()) {
               postTests.add(test);
           }
        }
        return postTests;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜