2013-02-28 26 views
0

我有两个对象,Customer和Store。我希望用户(来自用户表)能够将客户或商店指定为“首选”。然后,我可以看到喜欢不同商店或顾客的用户列表。这可能与一个休眠映射?在休眠中如何设置各种表格对象的状态

如果可以在每个用户的商店客户上设置首选状态,那么表结构将会显示什么内容?

+0

我不明白你想要什么。状态只需要一个额外的字段。什么是商店的首选意味着什么?对于客户? – 2013-02-28 21:38:10

+0

我想看看哪些客户和商店是用户的首选。我认为这意味着一个新的表与商店或客户ID和用户ID。然后,我可以看到所有准备好的商店/客户。 – Atma 2013-02-28 22:08:36

回答

1

因此,用户有许多首选商店,而商店是许多用户的首选商店。这是用户和商店之间的ManyToMany关联。

就映射它作为解释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); 
} 
+0

客户呢?我会使用某种多态来获得一组客户和商店吗? – Atma 2013-02-28 22:45:48

+0

你为什么要混合这两个实体。他们是不同的,没有共同之处,至少我猜是这样。因此,只需在User和Customer之间添加另一个ManyToMany关联即可。 – 2013-02-28 22:48:58