Hibernate ManyToOne 语法

Hibernate ManyToOne syntax

我正在尝试使用 MSSQL + Hibernate/JPA 编写代码,我可以在其中输入一些产品并在其中引用一家公司。

我的关系就是这样

    @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,预计会创建新公司而不是仅仅链接它)

经过一些测试和阅读大量资料后,我发现问题与我无关。但是我的 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;
    }
}