【问题标题】:Spring Boot JPA: Many to Many relation as its own class?Spring Boot JPA:多对多关系作为自己的类?
【发布时间】:2020-03-20 18:44:28
【问题描述】:

我正在使用 Spring Boot 和 JPA,并且两个表之间存在多对多关系。这个关系也需要有自己的属性,所以我不得不让这个关系成为自己的类。我也有所有类的存储库。另外,使用非原始字段时如何正确映射外键?

我的问题:我如何实现这一点,以便 Spring boot 不会给我一个错误?我是 Spring Boot 和 JPA 的新手。这是下面的代码sn-p:

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

@Entity //this is a relationship type. Does an annotation for relationship types exist?
@Embeddable
@Getter
@Setter
@NoArgsConstructor
public class RelationshipType implements Serializable {

    @Id
    private Long id;

    @NotNull
    @JoinColumn(name="id")
    private Class1 class1;

    @NotNull
    @JoinColumn(name="code")
    private Class2 class2;


    private Integer anotherNumber;
}

【问题讨论】:

  • 你写的代码有什么问题? "所以 Spring boot 不会给我报错" 你还没有描述任何错误
  • 事情是,由于我现在必须进行更改,我得到一个“class2 没有定义@Idclass”错误,显然需要先修复它。解决该问题后,我将编辑帖子并说明解决方案是否有效。

标签: java spring spring-boot jpa


【解决方案1】:

它应该类似于下面的示例。您需要将与其他表的关系设置为 @ManyToOne 以获得所需的 Employee 和 Number 之间的 @ManyToMany 关系。

@Entity
@Getter
@Setter
@Table(name = "employees_numbers")
public class EmployeeNumber {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "employee_id", nullable = false)
    private Employee employee;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "number_id", nullable = false)
    private Number number;

    @Column(name = "extra_column", nullable = false)
    private String extraColumn;
}

旁注:不要在整数上使用@NotEmpty。它是专门为收藏而设计的。

旁注 2:我会在这里跛行,说你想要的是将 nullable = false 添加到列中,而不是将它们标记为 @NotNull。两者有很大区别。

【讨论】:

  • 我已经采纳了您的更改,并在 id 中添加了以下注释:@GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="id", insertable = false, updatable = false) 现在是说:column: id (should be mapped with insert="false" update="false")
  • 如果您想向多对多关系添加奖励字段,您必须创建一个单独的实体并插入和/或更新它.它不适合您的原因可能是因为您留下了@Embeddable 或其他一些注释,这些注释可能会向 Hibernate 发出信号,表明这是一个不被实例化、插入和更新的实体。
  • 我实际上删除了@Embeddable 注释,我的注释与你的匹配(我还有@NoArgsConstructor')这是我收到的完整错误消息:
  • 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: Repeated column in mapping for entity: de.models.RelationshipType column: id (should be mapped with insert="false" update="false")
  • 谢谢,现在我不再遇到这个问题了。
猜你喜欢
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
  • 2021-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多