简体   繁体   English

如何使用 REST API - Spring 引导插入带有伪造密钥的数据

[英]How to insert data with forgein key using REST API - Spring boot

I've created my REST API that connect with my MYSQL database.我已经创建了我的 REST API 连接到我的 MYSQL 数据库。 It supports GET, PUT, DELETE and POST methods.它支持 GET、PUT、DELETE 和 POST 方法。 Everything works fine, but when I'm trying to POST data to a table that contain forgein key I get:一切正常,但是当我尝试将数据发布到包含 forgein 键的表时,我得到:

Caused by: java.sql.SQLIntegrityConstraintViolationException: Column 'id_companies' cannot be null引起:java.sql.SQLIntegrityConstraintViolationException:列'id_companies'不能是null

I'm trying to POST data using Postman(through body or/and JSON raw)我正在尝试使用 Postman 发布数据(通过 body 或/和 JSON raw)

I've seen similar problems on the Inte.net but I didn't find a solution to my specific problem.我在 Inte.net 上看到过类似的问题,但没有找到解决我的具体问题的方法。
I'm Using maven and lombok.我正在使用 maven 和龙目岛。

Entity User:实体用户:

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "users")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_users")
    private Integer idUser;
    @Column(name = "first_name")
    private String firstName;
    @Column(name = "last_name")
    private String lastName;
    @Column(name = "phone_number")
    private String phoneNumber;

    @ManyToOne(targetEntity = Company.class)
    @JsonBackReference(value = "companyUsers")
    @JoinColumn(name = "id_companies")
    private Company companies;
}

UserRepository:用户资料库:

@RepositoryRestResource(collectionResourceRel = "users", path = "users")
    public interface UserRepository extends PagingAndSortingRepository<User, Integer> {
}

Entity Company:实体公司:

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "companies")
public class Company implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_companies")
    private Integer idCompany;
    @Column(name = "name")
    private String name;
    @Column(name = "nip")
    private String nip;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "companies")
    @JsonManagedReference(value = "companyUsers")
    private Set<User> users;
}

CompanyRepository:公司资料库:

@RepositoryRestResource(collectionResourceRel = "companies", path = "companies")
    public interface CompanyRepository extends PagingAndSortingRepository<Company, Integer> {
}

---EDITED--- ---编辑---

sample data I am trying to add via Postman POST我试图通过 Postman POST 添加示例数据

Trying to create user with id_companies = 2(I'm sure it exists in my batabase)尝试使用 id_companies = 2 创建用户(我确定它存在于我的数据库中)

{
    "id_users": 7,
    "firstName": "Joe",
    "lastName": "Doe",
    "phoneNumber": "565345765",
    "id_companies": 2,
}

I've tried POSTing it without "id_users" because its AUTO_INCREMENT, but the same problem keeps happening.我试过在没有“id_users”的情况下发布它,因为它是 AUTO_INCREMENT,但同样的问题不断发生。

As far I understand You may have done wrong in your controller class.据我了解,您的 controller class 可能做错了。

If you want to create a user when posting your entity.如果您想在发布实体时创建用户。 At first, you have to save your user in your controller class.首先,您必须将您的用户保存在您的 controller class 中。

company.setUsers(userRepository.save(company.getUser));
return companyRepository.save(company);

If you want to use existing users then you have to load user using id in your controller.如果您想使用现有用户,则必须使用 controller 中的 id 加载用户。

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

相关问题 如何使用使用 SPRING BOOT DATA REST 创建的 rest API 插入 - How to insert using rest API created using SPRING BOOT DATA REST 上传一个 excel 文件从文件中读取数据并使用 spring 引导将其插入数据库 rest api - Upload an excel file read the data from file and insert it into database using spring boot rest api 如何在rest控制器spring boot中为邮递员生成api-key - how to generate api-key for postman in rest controller spring boot 如何在 Spring Boot 中使用 RestTemplate 从来自外部 REST API 的 JSON 响应中提取数据? - How to extract data from JSON response coming from external REST API using RestTemplate in Spring Boot? 如何将整数列表作为 json 数据传递给 spring 引导 rest ZDB974238714CA8DE634A7ACE14 - How to pass the list of integers as json data to spring boot rest API? 如何在 spring 中使用 Rest API 从多个表中获取数据 - how to fetch data using Rest API in spring boot from multiple tables 在Spring Boot Rest API中获取JSON数据 - fetch json data in spring boot rest api 使用 LDAP 身份验证的 Spring Boot REST API - Spring Boot REST API using LDAP authentication 使用 Spring Boot 的带有 websocket 的 REST API - REST API with websocket using Spring boot REST api 使用 java 和 spring 启动 - REST api using java and spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM