通过Rest调用在json中传递fk作为DerivedIdentities的一部分 [英] Passing fk in json via rest call as part of DerivedIdentities

查看:100
本文介绍了通过Rest调用在json中传递fk作为DerivedIdentities的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有嵌入式密钥(id,制造商)的Product实体。我正在通过REST与制造商的fk一起调用产品实体:

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

产品:

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

Id类:

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();
    }
}

我还创建了一个DTO对象,该对象将通过api传递给控制器:

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;

}

制造商:

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


更新


我遵循了以下堆栈溢出信息。我将ProductId更改为:

Update

I followed the following Stack Overflow 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;

现在出现以下错误:

org.hibernate.HibernateException: No part of a composite identifier may be null


Update 2


产品ID似乎没有被填充,这就是为什么它没有被创建的原因。我尝试在以下函数中设置ID,然后成功插入了产品:

Update 2

It looks like the id of the Product isn't populated and that's why it isn't 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;
}

所以现在有一个开放的问题是为什么没有填充ID?

So the open question now is why the id isn't populated ?

推荐答案

我的原始问题是:

java.lang.IllegalArgumentException: Can not set com.dao.Manufacturer field com.dao.ProductId.manufacturer to java.lang.Long

我通过遵循这篇文章。最重要的是,在我的IdClass内部,复合对象的类型应该是他的PK:

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

解决该问题后,我遇到了一个问题new:

Althought after solving it I faced a new :

org.hibernate.HibernateException: No part of a composite identifier may be null

可能是 bug (或与此 bug )在使用@Idclass时与休眠有关

might be a bug(or this bug) related to hibernate when using @Idclass.

无论哪种方式,解决此问题并解决问题的方法都是将id列初始化为一个值:

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;

这将绕过id的休眠验证,并允许它随后将序列值映射到它。

This will bypass hibernate validation of the id and allow it to map the sequence value to it afterwards.

这篇关于通过Rest调用在json中传递fk作为DerivedIdentities的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆