将 Spring Boot Jpa 中的三个实体相互连接起来的问题 [英] Problems wiring up three Entities in Spring Boot Jpa with each other

查看:18
本文介绍了将 Spring Boot Jpa 中的三个实体相互连接起来的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的数据库中有一个表 project.每个project有很多users,每个user可能有很多projects.这将是一个经典的 ManyToMany 相关性,因此,如果我想在我的 Spring Boot JPA 中描述它,我可以这样做:

Let's assume I have a table project in my database. Each project has many users and each user might have many projects. This would be a classic ManyToMany-correlation and so, if I would like to depict that in my Spring Boot JPA, I could do as follows:

设置我的实体并将它们连接起来:

Set up my Entities and wire them up:

@Entity
public class User {
  @id
  @Column(name="ID")  
  public int id;

  @ManyToMany
  public Set<Project> projectSet;
}

@Entity
@Table(name="PROJECT")
public class Project {
  @id
  @Column("ID")
  private int id;

  @ManyToMany
  private Set<User> users;
}

实现我的存储库:

@Repository
public interface ProjectRepo implements JpaRepository<Project, Integer> {
  public Set<User> getAllUsers();
}

现在让我们假设我想指定每个用户在这个项目中的角色.所以我会有一个表 role 用以下实体表示指定不同的角色:

Now let's assume I would like to specify what role each user had in this project. So I would have a table role that specifies different roles with the following Entity-representation:

@Entity
@Table(name="ROLES")
public class Role {
  @id
  @Column(name="ID")
  private int id;

  @Column(name="DESCRIPTION")
  private String description;
}

但这与我的用户和项目有何关联?所以我想说明的是,一个用户可以在一个项目中拥有一个角色,但在多个项目中可以有多个角色.此外,一个角色可以分配给一个或多个项目中的多个用户,一个项目可以有多个用户,每个用户只有一个角色,但有多个角色.如果我只是添加一个 @ManyToMany Set;roleSet 到我的 Users,我不能说哪个用户在哪个项目中有哪个角色.那么如何描绘呢?我需要创建第三个类来连接它们吗?

But how is that connected to my Users and Projects? So what I want to show is that one user can have one role in one project, but many roles in many projects. Also, One Role can be assigned to many users in one or many projects and one project can have many users where each of them has only one role but many roles are there. If I simply add a @ManyToMany Set<Roles> roleSet to my Users, I could not say which User would have which Role in which project. So how to depict that? Would I need to create a third class for connecting them?

编辑 1:

为了澄清,我将尝试在marknote的答案中提供建议的代码 -

For me to clarify, I am going to try to provide the code for the suggestion in marknote's answer -

假设我有上面的类(ProjectUserRole),我想描述每个用户都有特定的一个项目中的角色,但可能在另一个项目中扮演另一个角色.此外,一个项目中的多个角色是不可能的(为了简单起见).

Let's assume I have the classes from above (Project, User, Role) and I want to depict that every user has a certain role in one project, but might have another role in another project. Also, multiple roles in one project are not possible (to keep it simple).

所以我会创建一个新类 Assignment(很像我在我的数据库结构中解决这个用例)并将它与我的 User、我的 Project 和我的 Role.

So I'd create a new class Assignment (pretty much like I would solve that use case in my database structure) and connect it with my User, my Project and my Role.

@Entity
@Table(name="USER")
public class User {
  @id
  @Column(name="id")
  private int id;

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

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

  @OneToMany
  private List<Assignment> assignments;
  //getters and setters and stuff
}

@Entity
@Table(name="PROJECT")
public class Project {
  @id
  @Column(name="PNUM")
  private String pnum;

  @Column(name="PNAME")
  private String pname;

  @OneToMany
  private List<Assignment> assignments;
  //getters and setters and stuff
}

@Entity
@Table(name="ROLE")
public class Role {
  @id
  @Column(name="id")
  private int id;

  @Column(name="DESCRIPTION")
  private String description;

  @OneToMany
  private List<Assignment> assignments;
  //getters and setters and stuff
}

到目前为止一切正常,所以我要创建以下 Assignment-class.我需要能够从 User 跟踪到 ProjectRole,所以我需要三个字段.让我们想象一下(通常的)用例:Assignment 表通过以下方式连接 UserProjectRole从 FK 到名称表的组合 PK.现在,Spring Boot JPA 中的组合 PK 通常(据我所知)在使用 @Embeddable 注释的新类中提供.从这个角度来看,我会做以下事情:

That's alright so far, so I'm gonna create the following Assignment-class. I need to be able to trace from User to Project and to Role, so I need the three fields. Let's inmagine the (usual) use-case: The table Assignment is connecting User, Project and Role by having a combined PK out of the FKs to the names tables. Now a combined PK in Spring Boot JPA is usually (as far as I know) provided in a new class that uses the @Embeddable-annotation. From that perspective, I'd do the following:

@Embeddable
public class AssignmentId implements Serializable {
  private User user;
  private Project project;
  private Role role;
}

并将我的 Assignment 类更改为:

And change my Assignment-class to:

@Entity
@Table(name="ASSIGNMENT")
public class Assignment {
  @EmbeddedId
  private AssignmentId assignmentId;
}

现在这样做时,我在尝试运行 spring-boot 时遇到以下错误:

Now when doing it that way, I run into following error when trying to run spring-boot:

org.springframework.beans.factory.BeanCreationException:创建名为entityManagerFactory"的 bean 在类路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class] 中定义时出错:调用 init 方法失败;嵌套异常是 javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory;嵌套异常是 org.hibernate.MappingException:无法确定类型:com.demo.example.entity.Project,在表:赋值,列:[org.hibernate.mapping.Column(project)]

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: com.demo.example.entity.Project, at table: assignment, for columns: [org.hibernate.mapping.Column(project)]

有人可以与此错误有关吗?我认为 @EmbeddedId 可能有问题.

Can someone relate to this error? It might be a problem with the @EmbeddedId I think.

编辑 2

问题出在 @Embedded 类上.将 Entity 连接到元素时出现问题:

The problem is with the @Embedded class. There are problems wiring up the Entity to an element:

private Role role;
private Project project;
private User user;

推荐答案

取决于您的业务需求.假设用户在项目中的角色会有所不同,您可以引入另一个类Assignment,然后它变成3 @OneToMany :)

Depends on your business requirement. Let's say user's role will be different in projects, you can introduce another class Assignment, then it becomes 3 @OneToMany :)

您将在Assignment中使用@ManyToOneUserProjectRolecode>,但您可能需要也可能不需要使用 @OneToMany.关于实现 @OneToMany 的最佳实践有一篇很好的文章:https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate 感谢@vlad-mihalcea

You will refer to User, Project, Role in Assignment with @ManyToOne, but you may or may not need to use @OneToMany. There is a very good article on the best practice of implement @OneToMany: https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate thanks to @vlad-mihalcea

这篇关于将 Spring Boot Jpa 中的三个实体相互连接起来的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆