[英]Jpa/Hibernate: avoid join on all join-table in an Entity that is UNIQUE per each relationship
[英]JPA: Modelling m:n relationship & join-table values
在某些實體(例如,用戶<--->組)之間具有真實關系,現在我想對此關系建模並基於該關系存儲其他信息,例如字段“ quality”。
我聽說我將必須創建一個新的聯接表user_group,如下所示:
id | user_ref | group_ref | quality
----------------------------------
1 1 1 0.5
2 1 2 1.3
... ... ... ...
對應的實體具有兩個相關的實體(私有成員)用戶和組,並用@ManyToOne注釋。 另一方面,我的用戶和我的組都有一組相關的user_group-entities,它們都是私有成員,並使用@OneToMany -annotation聲明。
我有三個問題:
...
EntityManager em = ...
...
em.getTransaction().begin();
User u = new User("Pete");
Group g = new Group("Anonymous workaholics")
UserGroup ug = new UserGroup();
ug.addUser(u);
ug.addGroup(g);
em.persist(u); em.persist(g); em.persist(ug);
em.getTransaction().commit();
em.close()
非常感謝!
addUser()
和addGroup()
方法應分別命名為setUser()
和setGroup()
:給定的UserGroup
只有一個用戶和一個組。 我還將為實體本身使用另一個名稱。 例如“會員資格”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.