2017-05-28 111 views
0

我想通过一个可能无意义的问题得到一些建议,或者可能会这样做。让我们有一组利益,像这样一个Many2Many关系的配置文件对象:JPA/Hibernate中的多对多关系Eager

@ManyToMany(fetch=FetchType.EAGER) 
@JoinTable(name="profile_interests", 
joinColumns={ @JoinColumn(name="profile_id") }, 
inverseJoinColumns = { @JoinColumn(name="interest_id") }) 
@OrderColumn(name="display_order") 
private Set<Interest> interests; 

//GETTER AND SETTERS 
public Set<Interest> getInterests() { 
    return interests; 
} 
public void setInterests(Set<Interest> interests) { 
    this.interests = interests; 
} 
public void addInterest(Interest interest) { 
    interests.add(interest); 
} 
public void removeInterest(String interestName) { 
    interests.remove(new Interest(interestName)); 
} 

在我的应用程序控制器我可以通过这种方式添加和删除的利益。

@RequestMapping(value="/save-interest", method=RequestMethod.POST) 
@ResponseBody 
public ResponseEntity<?> saveInterest(@RequestParam("name") String interestName) { 

    SiteUser user = getUser(); 
    Profile profile = profileService.getUserProfile(user); 

    String cleanedInterestName = htmlPolicy.sanitize(interestName); 

    Interest interest = interestService.createIfNotExists(cleanedInterestName); 

    profile.addInterest(interest); 
    profileService.save(profile); 

    return new ResponseEntity<>(null, HttpStatus.OK); 
} 


@RequestMapping(value="/delete-interest", method=RequestMethod.POST) 
@ResponseBody 
public ResponseEntity<?> deleteInterest(@RequestParam("name") String interestName) { 

    SiteUser user = getUser(); 
    Profile profile = profileService.getUserProfile(user); 

    profile.removeInterest(interestName); 

    profileService.save(profile); 

    return new ResponseEntity<>(null, HttpStatus.OK); 
} 

最终,将创建一个配置文件,一个profile_interests和一个兴趣表。 profile_interest表将有一个profile_id和一个interest_id,对吧?

现在想象一下,我还想让其他套装让我们说:活动,激情或讨厌,厌恶,任务,职业。我可以一次又一次地重复这些相同的过程,以涵盖6个新的(活动,激情,仇恨,不喜欢,任务,职业)。

在某些时候,一个人可能对汽车有兴趣,其他人对汽车有兴趣,第三个人讨厌汽车,第四个人说汽车是他的职业。

如果我创建了7个不同的对象集(兴趣,活动,激情,讨厌,不喜欢,任务,职业),我会在所有表中重复其中的很多。

- 有没有办法让7组对象具有共同的(兴趣,活动,激情,讨厌,不喜欢,任务,职业)表,但7个不同的中间表(profile_interests,profile_activities,profile_passions,profile_hates ,profile_dislikes,profile_task,profile_vocation)使用公共表?

谢谢。非常感谢您与非程序员的帮助。可能这是一个有据可查的问题,我已经解决了,我不知道。

PD:该利益实体是在这里:

@Entity 
@Table(name = "interests") 
public class Interest implements Comparable<Interest> { 

@Id 
@Column(name = "id") 
@GeneratedValue(strategy = GenerationType.AUTO) 
private Long id; 

@Column(name = "interest_name", unique = true, length = 25) 
private String name; 

public Interest() { 
} 
+0

将'fetchType'设置为'EAGER'对'Many2Many'关系不好,特别是你有多个。 –

+0

感谢哈迪,如果我多次使用它,我会重新提出你的建议,但这正是我试图避免的(多次使用它)。有关如何做的建议? – Mike

+0

这是可能的,但并不像您想要的那样直截了当:您需要映射7个对象,每个对象一个:profile_interests,profile_activities,profile_passions,profile_hates,profile_dislikes,profile_task,profile_vocation – Boschi

回答

0

在JPA 2个实体可以在多个方面相关 - 这是完全合法的。所以,就像利益,(说)活动将在Profile实体映射为:

@ManyToMany(fetch=FetchType.LAZY) // don't use EAGER unless you really want to :) 
@JoinTable(name="profile_activities", 
    joinColumns={ @JoinColumn(name="profile_id") }, 
    inverseJoinColumns = { @JoinColumn(name="interest_id") }) 
@OrderColumn(name="display_order") 
private Set<Interest> activities; 

//GETTER AND SETTERS AS FOR interests 

你没有表现出Interest实体。 如果这种关系是双向的,那么Interest必须有许多不同的Set<Profile>字段,对于它参与的每个关系(兴趣,活动,...)都有一个字段。在这种情况下,Interest实体中字段的mappedBy属性必须指向Profile的相应字段。

这也假定商业上所有的关系都在同一个实体之间。副作用是用户必须选择活动的列表与用户必须选择“兴趣”的列表相同。如果不是这样,那么你可能需要做更多。

+0

非常感谢,它非常适合标记! – Mike