首页 > 解决方案 > 休眠多对一语法

问题描述

我正在尝试编写使用 MSSQL + Hibernate/JPA 的代码,我可以在其中输入一些产品并在其上创建一个 FK,指的是一家公司。

我的关系就是这样

    @OneToMany(mappedBy = "companyId")
    private Set<Product> product;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "companyId")
    private Company companyId;

但是,然后采用我在 JSON 中发送的 FK,它创建了一家新公司

这是 JSON:

{
    "name":"Test",
    "price":"35.63",
    "productCompanyId":"10293", (this is the ID on company receipt, to make )
    "companyId":"2"
}

这里是我的实体

@Data
@Entity
@Table
@NoArgsConstructor
@AllArgsConstructor
public class Company {

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

    @NotNull
    String name;

    @OneToMany(mappedBy = "companyId")
    private Set<Product> product;

    public Company(String name){
        this.name = name;
    }
}
@Data
@Entity
@Table
@NoArgsConstructor
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @NotNull
    int productCompanyId; //product code in company receipt, making easier later updates

    @NotNull
    String name;

    @NotNull
    java.math.BigDecimal price;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "companyId")
    private Company companyId;// identification of where it was purchased

    public Product(String name, java.math.BigDecimal price, int productCompanyId, Company companyId) {

        this.price = price;
        this.name = name;
        this.productCompanyId = productCompanyId;
        this.companyId = companyId;
    }

}

数据库:[1]:https ://i.stack.imgur.com/WtRWR.jpg

id  name
1   TestCompany1
2   TestCompany2
3   2
id  name    price   product_company_id  company_id
1   Test    35.63   10293   3

我认为问题出在这里:

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductRepository productRepository;

    @PostMapping
    public ResponseEntity<Product> insertProduct (@RequestBody ProductForm form){
        Product product = form.creationConverter();
        productRepository.save(product);
        return ResponseEntity.ok().build();
    }
}

@Data
public class ProductForm {

    @NotNull
    String name;

    @NotNull
    java.math.BigDecimal price;

    @NotNull
    int productCompanyId;

    @NotNull
    Company companyId;

    public Product creationConverter() {
        return new Product(name, price, productCompanyId, companyId);
    }

    public Product updateConverter(Integer id, ProductRepository productRepository) {
        Product product = productRepository.getOne(id);
        product.setName(this.name);
        product.setPrice(this.price);
        product.setProductCompanyId(this.productCompanyId);
        product.setCompanyId(companyId);
        return product;
    }
}

但我找不到将这个 companyId 设置为 int 的方法(我正在向控制器发送一个新的 objetc,预计会创建新公司,而不仅仅是链接它)

标签: hibernatespring-data-jpahibernate-mapping

解决方案


经过一些测试并阅读了很多东西后,我发现问题不是我的关系。但是我的 ProductForm.java

事实上,我为修复它所做的事情非常简单。首先我删除了我的@ManyToOne 关系,因为它迫使我在我的产品中创建一个公司变量,只在公司中留下@OneToMany。这样我就可以在我的产品中使用一个int变量“companyId”,并且数据库中的记录停止重复=)

这是如何工作的:

产品:

@Data
@Entity
@Table
@NoArgsConstructor
public class Product {


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

    @NotNull
    int productCompanyId; //product code in company receipt, making easier later updates

    @NotNull
    String name;

    @NotNull
    java.math.BigDecimal price;

    @NotNull
    int companyId;// identification of where it was purchased

    public Product(String name, java.math.BigDecimal price, int productCompanyId, int companyId) {

        this.price = price;
        this.name = name;
        this.productCompanyId = productCompanyId;
        this.companyId = companyId;
    }

}

产品形式:

@Data
public class ProductForm {

    @NotNull
    String name;

    @NotNull
    java.math.BigDecimal price;

    @NotNull
    int productCompanyId;

    @NotNull
    int companyId;

    public Product creationConverter() {
        return new Product(name, price, productCompanyId, companyId);
    }

    public Product updateConverter(Integer id, ProductRepository productRepository) {
        Product product = productRepository.getOne(id);
        product.setName(this.name);
        product.setPrice(this.price);
        product.setProductCompanyId(this.productCompanyId);
        product.setCompanyId(companyId);
        return product;
    }
}

然后是有关系的公司:

@Data
@Entity
@Table
@NoArgsConstructor
@AllArgsConstructor
public class Company {


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

    @NotNull
    String name;

    @OneToMany(mappedBy = "companyId")
    private Set<Product> product;

    public Company(String name){
        this.name = name;
    }
}

推荐阅读