簡體   English   中英

使用Hibernate通過公共橋表映射3個實體

[英]Mapping 3 entities through a common bridge table using Hibernate

我想模擬一個團隊需要多種技能才能運作的事實。 許多人可以具有一定的技能。 一個人有很多技能。 一個人可以成為許多團隊的一部分。

我使用Hibernate對這種情況進行建模。 我開始使用團隊和技能這兩個實體進行構建,並使用@ManyToMany批注來鏈接這些依賴項。 試圖添加第三個實體(人)是很困難的。 我不知道如何建立該模型,將不勝感激。

我沒有太多使用Hibernate的經驗,所以這是一個挑戰。

我已經搜索了信息,發現的大多數示例都是關於兩個連接的實體,而我無法將這些示例擴展到包括第三個實體。

這些是我的實體:

package com.example.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import java.util.Set;

@Entity
public class Team {
    @Id
    @GeneratedValue
    private Long id;

    private String name;

    @ManyToMany
    private Set<Skill> skills;

    @ManyToMany
    private Set<Person> persons;
}



package com.example.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import java.util.Set;

@Entity
public class Skill {
    @Id
    @GeneratedValue
    private Long id;

    private String knowHow;

    @ManyToMany
    private Set<Team> teams;

    @ManyToMany
    private Set<Person> persons;
}



package com.example.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Set;

@Entity
public class Person {
    @Id
    @GeneratedValue
    private Long id;

    private String name;

    private Set<Team> teams;
    private Set<Skill> skills;
}

這些是我的存儲庫:

package com.example.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.entity.Competence;
import com.example.entity.Team;

import java.util.List;

@Repository
public interface TeamRepository extends CrudRepository<Team, Long> {
    List<Competence> findDistinctByKnowHow(String name);
}



package com.example.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.entity.Competence;
import com.example.entity.Skill;

import java.util.List;

@Repository
public interface SkillRepository extends CrudRepository<Skill, Long> {

    List<Competence> findDistinctByKnowHow(String knowHow);

}



package com.example.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.entity.Competence;
import com.example.entity.Person;

import java.util.List;

@Repository
public interface PersonRepository extends CrudRepository<Person, Long> {

    List<Competence> findDistinctByPerson(String name);

}

恐怕您無法做到這一點。 該模型包含邏輯上的不一致,並可能導致矛盾。 假設您有一個包含2個成員( PersonAPersonB )的TeamA PersonA包含SkillA,PersonB包含SkillB 這意味着TeamA應該包含2個技能( SkillASkillB )。 但是,您的模型應該允許將技能獨立於成員放在團隊級別。 如果僅將SkillA放在TeamA的 技能屬性上會發生什么?

我認為您應該在多對多之間建立聯系

  • 技能
  • 團隊

應該導出團隊級別的技能屬性。

我認為這個問題值得。

假設團隊只能看到他們自己添加的能力,則在三個實體之間使用公用的橋接表可通過設計實現行級安全性。

以一個與VB一起工作的顧問為例。 她確實不喜歡VB,並且不希望其他團隊知道她具有這種技能。

團隊A可以知道人員A具有VB技能。 團隊B不應該知道人員A具有VB技能。

我本來會將此添加為評論,但我缺乏必要的聲譽= /

解決此問題的一種方法是創建一個僅作為橋接實體存在的實體。 這樣,您可以在“真實”實體和橋實體之間添加三個不同的一對多關系。 這樣一來,您應該能夠處理三對多關系。

但是,您可能必須“手動”處理橋實體中的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM