【问题标题】:Not able to map fields of different entities with many to many relationship无法映射具有多对多关系的不同实体的字段
【发布时间】:2020-10-06 20:43:05
【问题描述】:

DB Schema - 2 个表(用户和角色)具有多对多关系,并由 postgres 中的中间表(user_role)桥接。我想获取所有角色和创建它的人的姓名。名称在用户表中可用,但所有其他详细信息都在角色表中。 Roles 表有一个字段 created_by(创建角色的人的 User_id)。

我正在尝试构建一个 GET 请求,以查看给定 id 的所有角色以及创建它的人的姓名

实体类Users1.java

@Data
@Entity
@Table(name = "user")
public class Users1 {

@Id
@Column(name = "user_id")
private Long user_id;

@Column(name = "org_id")
private Long org_id;

@Column(name = "boss")
private Boolean boss;

@Column(name = "firstname")
private String firstname;

@Column(name = "created_by")
private Long created_by;


@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_on")
private Date created_on;

@ManyToMany(fetch = FetchType.LAZY,
        cascade = {CascadeType.ALL})
@JoinTable(name = "user_role",
    joinColumns = {@JoinColumn(name = "user_id")},
    inverseJoinColumns = {@JoinColumn(name = "created_by")})

private List<Role> roles = new ArrayList<Role>();

// Getters and Setters

实体类 Role.java

@Data
@Entity
@Table(name = "role")
public class Role implements Serializable {
private static final long serialVersionUID = -2343243243242432341L;

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

@Column(name = "org_id")
private Long org_id;

@Column(name = "role_name")
private String role_name;

@Column(name = "created_by")
private Long created_by;

@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_on")
private Date created_on;

@ManyToMany(fetch = FetchType.LAZY,
        cascade = {CascadeType.ALL},
        mappedBy = "roles")

private List<Users1> users = new ArrayList<>();

// Getters and Setters

roles.repository 类

@Repository
@Transactional
public interface RolesRepository extends CrudRepository<Role,Long> {
    @Query(value ="select r.*,u.firstname as firstname 
    from user u 
    join user_role ur  on u.user_id = ur.user_id 
    join role r on ur.role_id = r.role_id 
    where(true = (select u.boss from user u  where u.user_id = ?2) and r.org_id = ?1)
    or 
    (false = (select u.boss from user u  where u.user_id = ?2) and r.created_by =?2)", 
    nativeQuery=true)

    public Iterable<Role> findAllById(Long org_id,Long created_by );


    @Query("from Users1 u where u.user_id=?2 and u.org_id = ?1")
    public Users1 findUserbyOrgId(Long org_id, Long created_by);

}

roles.Controller 类:

public ResponseEntity<Iterable<Role>> getAllRoles(@RequestParam("org_id") Long org_id, 
@RequestParam("created_by") Long created_by ) throws Exception {

        if( null != rolesRepository.findUserbyOrg(org_id, created_by)) {
            Iterable<Role> Roles = rolesRepository.findAllById(org_id, created_by); }

GET Response from postman:
[
    {
        "roleId": 3,
        "org_id": 2,
        "roleName": "manager",
        "createdBy": 5,
        "createdOn": 1591716178419,
    }
]

我得到了除了名字之外的所有内容。我不确定如何在我的 GET API 中获取它。任何帮助将不胜感激。

【问题讨论】:

    标签: java spring-boot jpa


    【解决方案1】:

    不是直接回答您的问题,但由于以下原因,@ManyToMany 在实际领域通常不被鼓励。

    • 连接表可能需要更多信息(但不能)
    • 由于连接表被隐藏,查询结果难以预料。

    推荐的方法是

    • 将两个@ManyToMany 类分解为两个@OneToMany 类+ 一个@ManyToOne 类(加入表)
    • 将连接表提升为实体类。

    stackoverflow 和 youtube 中有很多此类案例的实践。我认为它最终会为您节省更多时间来切换到这种方法。

    【讨论】:

      猜你喜欢
      • 2018-01-13
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 2013-06-14
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多