简体   繁体   English

在Spring Boot Jpa中相互连接三个实体时出现问题

[英]Problems wiring up three Entities in Spring Boot Jpa with each other

Let's assume I have a table project in my database. 假设我的数据库中有一个表project Each project has many users and each user might have many projects . 每个project有许多users ,每个user可能有许多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: 这将是一个经典的ManyToMany -correlation关系,因此,如果我想在Spring Boot JPA中对此进行描述,则可以执行以下操作:

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;
}

Implement my Repository: 实施我的存储库:

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

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: 因此,我将拥有一个表role ,该角色使用以下实体表示指定不同的角色:

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

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

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. 如果我只是将@ManyToMany Set<Roles> roleSet到我的Users ,则无法确定哪个User在哪个项目中将具有哪个Role。 So how to depict that? 那么如何描绘呢? Would I need to create a third class for connecting them? 我需要创建第三类来连接它们吗?

Edit 1: 编辑1:

For me to clarify, I am going to try to provide the code for the suggestion in marknote's answer - 为了让我澄清,我将尝试在marknote的答案中提供建议的代码-

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. 假设我有上面的类( ProjectUserRole ),并且我想描述每个用户在一个项目中都具有特定角色,但是在另一个项目中可能具有另一个角色。 Also, multiple roles in one project are not possible (to keep it simple). 同样,在一个项目中不可能有多个角色(为了简单起见)。

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 . 因此,我将创建一个新的类Assignment (非常类似于我将在数据库结构中解决该用例的情况),并将其与UserProjectRole

@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
}

That's alright so far, so I'm gonna create the following Assignment -class. 到目前为止还可以,所以我将创建以下Assignment -class。 I need to be able to trace from User to Project and to Role , so I need the three fields. 我需要能够从User跟踪到Project以及到Role ,所以我需要三个字段。 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. 让我们想象一下(通常)用例:表Assignment通过将FK中的组合PK组合到名称表来连接UserProjectRole 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. 现在,据我所知,通常在Spring Boot JPA中使用@Embeddable -annotation的新类中提供了组合PK。 From that perspective, I'd do the following: 从这个角度来看,我将执行以下操作:

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

And change my Assignment -class to: 并将我的Assignment -class更改为:

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

Now when doing it that way, I run into following error when trying to run spring-boot: 现在,当这样做时,在尝试运行spring-boot时遇到以下错误:

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; org.springframework.beans.factory.BeanCreationException:创建在类路径资源[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaConfiguration.class]中定义的名称为'entityManagerFactory'的bean时出错:调用init方法失败; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; 嵌套的异常是javax.persistence.PersistenceException:[PersistenceUnit:默认]无法构建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)] 嵌套的异常是org.hibernate.MappingException:无法确定com.demo.example.entity.Project的类型,在表:分配处,对于列:[org.hibernate.mapping.Column(project)]

Can someone relate to this error? 有人可以跟这个错误有关吗? It might be a problem with the @EmbeddedId I think. 我认为@EmbeddedId可能有问题。

Edit 2 编辑2

The problem is with the @Embedded class. 问题在于@Embedded类。 There are problems wiring up the Entity to an element: Entity到元素时存在问题:

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

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 ,然后它变成3 @OneToMany :) 在此处输入图片说明

You will refer to User , Project , Role in Assignment with @ManyToOne , but you may or may not need to use @OneToMany . 你会参考UserProjectRoleAssignment@ManyToOne ,但你可能会或可能不会需要使用@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 有一篇关于实现@OneToMany的最佳实践的很好的文章: https ://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate感谢@vlad -mihalcea

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM