简体   繁体   中英

JPA model relations

I am currently programming a board for me and my friends with the playframework 1.2.4 that uses the JPA. Everything was fine until I came to the view mechanism.

The problem is, that every user can have multiple view entries. Every topic can also have multiple view entries so here is my view model:

@Entity
public class ForumTopicView extends Model
{

    @Required
    @ManyToOne
    public ForumTopic   topic;

    @Required
    @ManyToOne
    public User         user;

    @Required
    public Date         viewDate;

}

On the other hand, there is only one view for a topic AND the connected user.

Here is my topic model:

@Entity
public class ForumTopic extends Model
{

    // ...

    @OneToOne(mappedBy = "topic") // TODO: and the connected user
    public ForumTopicView       view;

    // ...
}

How can I customize this that the user has to be the connected user and for every topic only one view selected is.

Thanks for any help!! MRu

@Yogendra Singh: Yes thats correct. Every user can have a view to one topic.

If one side of an association is a ManyToOne, the other side must be a OneToMany, not a OneToOne.

Every topic has multiple views (and one of these views is for the current user). Each user has multiple views (and one of these views is for a given topic). So you have two OneToMany/ManyToOne associations.

It's not clear what you want to do, but I think you'll need to execute a query to get what you want. If what you want is the list of views associated with the current user and corresponding to a given list of topics, the query simply is

select view from ForumTopicView view
where view.user = :currentUser
and view.topic in (:listOfTopics)

No need for 50 queries if you have 50 topics. The above one retrieves all the views at once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM