问题
A short summary : I got a Product entity that has an embedded key (id,manufacturer). I'm passing through rest call the product entity with fk of the manufacturer :
{
"name":"Chocolate",
"register_date":"19/03/2020",
"manufacturer_id": 52,
"rating":"Amazing"
}
When I try to save the entity in the controller I'm getting the following error
java.lang.IllegalArgumentException: Can not set com.dao.Manufacturer field com.dao.ProductId.manufacturer to java.lang.Long
Product :
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@IdClass(ProductId.class)
@Entity
public class Product {
@Id
@SequenceGenerator(name = "product_id_seq", sequenceName = "product_id_seq", initialValue = 1)
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator = "product_id_seq")
private Long id;
@ManyToOne(fetch=FetchType.LAZY)
@Id
private Manufacturer manufacturer;
private String name;
@JsonFormat(pattern = "dd/MM/YYYY")
private Date register_date;
@Enumerated(EnumType.ORDINAL)
private Rating rating;
public enum Rating {
Amazing,Good_Value_For_Money,Bad
}
The Id class :
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@AllArgsConstructor
@NoArgsConstructor
public class ProductId implements Serializable {
private Long id;
private Manufacturer manufacturer;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ProductId pId1 = (ProductId) o;
if (id != pId1.id) return false;
return manufacturer.getId() == pId1.manufacturer.getId();
}
@Override
public int hashCode() {
return id.hashCode()+manufacturer.getId().hashCode();
}
}
I also created a DTO object that will be passed through the api to the controller :
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class ProductCreationDTO {
private String name;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/mm/yyyy")
private Date register_date;
private Long manufacturer_id;
private Product.Rating rating;
}
The manufacturer :
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Manufacturer {
@Id
@SequenceGenerator(name = "manufacturer_id_seq", sequenceName = "manufacturer_id_seq", initialValue = 1)
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator = "manufacturer_id_seq")
private Long id;
private String name;
private String country;
@OneToMany(mappedBy = "manufacturer",fetch = FetchType.LAZY)
private List<Product> products;
In my controller I have the following 2 functions :
RestController
@RequestMapping("/api")
public class ProductController {
@Autowired
ProductService productService;
@Autowired
ManufacturerService manufacturerService;
@RequestMapping(value = "/product/", method = RequestMethod.POST)
public HttpStatus insertProduct(@RequestBody ProductCreationDTO pcd) {
Product p = mapProductCreationDTOtoProduct(pcd);
if(p.getManufacturer() == null)
return HttpStatus.BAD_REQUEST;
return productService.addProduct(p) ? HttpStatus.CREATED : HttpStatus.BAD_REQUEST;
}
private Product mapProductCreationDTOtoProduct(ProductCreationDTO pcd)
{
Product p = new Product();
p.setName(pcd.getName());
p.setRating(pcd.getRating());
p.setRegister_date(pcd.getRegister_date());
Optional<Manufacturer> m = manufacturerService.getManufacturer(pcd.getManufacturer_id());
if (m.isPresent())
p.setManufacturer(m.get());
return p;
}
the add method under the productService :
@Transactional
public boolean addProduct(Product p)
{
return productRepository.save(p)!=null;
}
Update : I followed the following stackoverflow post I changed the ProductId to :
public class ProductId implements Serializable {
private Long id;
private Long manufacturer;
....
and in the Product class I added the following annotation above the Manufacturer :
@Id
@ManyToOne(fetch=FetchType.LAZY)
@MapsId("manufacturer")
private Manufacturer manufacturer;
and now I'm getting the following error :
org.hibernate.HibernateException: No part of a composite identifier may be null
Update 2 : It looks like the id of the Product isnt populated and thats why it isnt created. I tried setting the id in the following function and the product was inserted successfully :
private Product mapProductCreationDTOtoProduct(ProductCreationDTO pcd)
{
Product p = new Product();
p.setName(pcd.getName());
p.setRating(pcd.getRating());
p.setRegister_date(pcd.getRegister_date());
Optional<Manufacturer> m = manufacturerService.getManufacturer(pcd.getManufacturer());
if (m.isPresent())
p.setManufacturer(m.get());
p.setId((long) 1); <-------------------------------------------
return p;
}
So the open question now is why the id isnt populated ?
回答1:
The obvious mistake is a @id
annotation over the name
property in the Product
entity whereas it should be over the manufacturer
property
回答2:
My original problem was :
java.lang.IllegalArgumentException: Can not set com.dao.Manufacturer field com.dao.ProductId.manufacturer to java.lang.Long
I solved it by following this post . The bottom line was that inside my IdClass, the type of the composite object should be his PK :
public class ProductId implements Serializable {
private Long id; // matches name of @Id attribute
private Long manufacturer; // name should match to @Id attribute and type of Manufacturer PK
Althought after solving it I faced a new :
org.hibernate.HibernateException: No part of a composite identifier may be null
might be a bug(or this bug) related to hibernate when using @Idclass.
Either way, the way to handle this problem and solve it is initiate the id column to a value :
public class Product {
@Id
@SequenceGenerator(name = "product_id_seq", sequenceName = "product_id_seq")
@GeneratedValue(strategy= GenerationType.SEQUENCE,generator = "product_id_seq")
private Long id=-1L;
This will bypass hibernate validation of the id and allow it to map the sequence value to it afterwards.
来源:https://stackoverflow.com/questions/60765095/passing-fk-in-json-via-rest-call-as-part-of-derivedidentities