问题描述
我正在尝试使用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;
}
}