当前位置: 动力学知识库 > 问答 > 编程问答 >

mysql - In hibernate how to set status for various table objects

问题描述:

I have two objects, Customer and Store. I would like a user (from a user table) to be able to specify a customer or store as "preferred". I would then be able to see a list of users who prefer different stores or customers. Is this possible with a hibernate mapping?

What would the table structure look if a status of preferred could be set on either customer of store per user?

网友答案:

So, a User has many preferred Stores, and a Store is the preferred store of many users. This is thus a ManyToMany association between User and Store.

Just map it as explained in the documentation:

public class User {
    @ManyToMany
    private Set<Store> preferredStores  = new HashSet<Store>(0);
}

public class Store {
    // necessary only if you want the association to be bidirectional:
    @ManyToMany(mappedBy = "preferredStores")
    private Set<User> preferringUsers = new HashSet<User>(0);
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: