微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Hibernate ManyToOne语法

如何解决Hibernate ManyToOne语法

我正在尝试使用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,期望创建新的公司而不是仅仅链接它)

解决方法

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

我所做的修复实际上很简单。 首先,我删除了@ManyToOne关系,因为它迫使我在我的产品中创建一个Company变量。 在公司中仅保留@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;
    }

}

ProductForm:

@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;
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。