当前位置: 首页 > 知识库问答 >
问题:

无法在Spring数据REST请求中保存OneTomany/ManyToOne关系

堵龙野
2023-03-14
org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING)
public class Contract implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @OneToMany(
            cascade = CascadeType.ALL,
            orphanRemoval = true,
            fetch = FetchType.LAZY,
            mappedBy="contract"
    )
    private List<Participation> participants = new ArrayList<Participation>();

    private String name;

}   
@Entity
public class Participation implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
        
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(nullable = false) //By default the column will be CONTRACT_ID
    private Contract contract;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(nullable = false)
    private Contact contact;

    private String clauses;
}
@Entity
public class Contact implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String emailAddress;
}

我有2个JParepository:

public interface ContractRepository extends JpaRepository<Contract, Long> {
    List<Contract> findByNameContainsIgnoreCase(String name);
}

public interface ContactRepository extends JpaRepository<Contact, Long> {
}

为了保存一份有几个参与者的新合同,我正在邮递员中执行以下步骤:

  1. 创建契约并获取其href:
{
   "name": "Contract1"
}
201 Created
{
  "name": "Contract1",
  "participants": [],
  "_links": {
    "self": {
      "href": "http://localhost:8080/api/contracts/4"
    },
    "contract": {
      "href": "http://localhost:8080/api/contracts/4"
    },
  }
}
{
    "participants": [
        {
            "clauses": "Bla bla bla",
            "contact": {
            "href": "http://localhost:8080/api/contacts/1"
            },
            "contract": {
            "href": "http://localhost:8080/api/contracts/4"
            }
        }
    ]
}
{
    "cause": {
        "cause": null,
        "message": "not-null property references a null or transient value : com.xxx.xxx.model.Participation.contract"
    },
    "message": "not-null property references a null or transient value : com.xxx.xxx.model.Participation.contract; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : com.xxx.xxx.model.Participation.contract"
}
"contract": "http://localhost:8080/api/contracts/4"

没运气。出于某种原因,系统将该字段留空,而不是使用步骤1中创建的实体的foreing键。我做错了什么?

共有1个答案

施季
2023-03-14

问题可以通过以下方式解决:

  1. 添加新的存储库ParticipationRepository(扩展JpaRepository);
  2. 首先创建一个没有参与的合同:

发布http://localhost:8080/api/contracts{“name”:“contract1”}

201 Created
{
  "name": "Contract1",
  "_links": {
    "self": {
      "href": "http://localhost:8080/api/contracts/3"
    },
    "contract": {
      "href": "http://localhost:8080/api/contracts/3"
    },
    "participants": {
      "href": "http://localhost:8080/api/contracts/3/participants"
    }
  }
}
201 Created
{
  "clauses": "bla, bla, bla",
  "_links": {
    "self": {
      "href": "http://localhost:8080/api/participations/5"
    },
    "participation": {
      "href": "http://localhost:8080/api/participations/5"
    },
    "contract": {
      "href": "http://localhost:8080/api/participations/5/contract"
    },
    "contact": {
      "href": "http://localhost:8080/api/contacts/5/contact"
    }
  }
}
 类似资料:
  • 因为我是个新手,我的问题可能很简单。我搜索了一下,但没有找到一个令人信服的答案。在开发REST API时,API的目的是从现有的学生中创建一个玩家团队。POST方法,从现在开始,请求正文将保存请求正文中的学生ID(主键)。 在访问REST API时,请求主体是否可以包含表主键?

  • 我有跟风问题 我有一个Spring数据Rest的基本配置(没有花哨,没有自定义)。 使用spring数据rest webmvc 2.0.0版本和spring数据jpa 1.5.0版本 A类 B类 存储库A 存储库B 当我拯救一个实体时工作正常,但我不知道如何拯救一段关系 e、 g.使用http将“A”保存在“B”中 这是我从这个答案中尝试的最后一件事https://stackoverflow.co

  • 我在将带有@ManyToOne关系的实体(雇员)映射到带有@OneToMany关系的实体(社区)时遇到了问题。 当我将一个实体(社区)分配给一个实体(员工),其中社区的值为空时,它工作正常。 问题出现了,如果雇员已经为社区分配了价值。当我更改该值并保存更改时,该员工为社区获得了新的值,并且这个新社区在集合中获得了该员工。唯一的问题是,老社区仍然有这个员工在收集,但它应该被删除。这只有在数据库重新启

  • 我需要创建一个JPA投影,该投影将从中提取所有记录,并包括一个从中提取的记录计数。 下面是映射的四个实体及其关系: (ManyToOne)(OneToMany)(OneToMany) 在SQL中,这可以通过简单的内联select语句轻松地解决。在JPA投影中,我试图做到以下几点。 在上面的代码中,投影在Entity1上。当我添加Entity4时,它会抛出异常。下面的代码起作用。

  • 我无法使用REST保证执行我们的REST帖子网址。我收到状态 404 错误。我们的REST网址不需要任何身份验证,正文等。相同的网址在POSTMAN,RESTEasy中工作正常。我尝试了所有四种组合以获得响应

  • 我有2个实体(我删除了无用的字段): 而且 这似乎符合我的情况(也是我当前的实现),但它不起作用。 我得到以下错误消息: 来自MySQL: 临时解决方案是从存储库中检索所有相应的权限(按permissionName搜索)。那么保存效果很好。逻辑上是正确的,但我希望有一个更简单的程序...