0

I need to put and get data from MySql with spring boot (Hibernate, Jackson)

Sample JSON:

[
    {
        "name": "Product1",
        "colors": [
            {
                "color": [
                    "#dddd33",
                    "#676767"
                ]
            },
            {
                "color": [
                    "#FFdDFF"
                ]
            }
        ]
    }
]

Same JSON I want to receive but added "id". What should I do to achieve this?

Object:

@Entity
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @JsonProperty("name")
    private String name;

    @JsonProperty("colors")
    @OneToMany(mappedBy = "product_id")
    private Set<Color> colors;
}

@Entity
public class Color {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private Long product_id;

    @JsonProperty("color")
    @CollectionTable(name = "color", joinColumns = @JoinColumn(name = "color_id"))
    @Column(name = "color")
    @ElementCollection
    @OrderBy
    private List<String> color;
}

This not work I get error java.sql.SQLException: Field 'id' doesn't have a default value

3

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.