0

I want to save both child and parent entities whenever a POST call is made. I have an Item entity with a one to one mapping to a parent table Attribute:

@Entity
@Table(name="Item")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Item
{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name="id")
  private Long id;  

  @OneToOne(fetch=FetchType.LAZY)
  @JoinColumn(name="attr_id")
  private Attribute attribute;
  
  @OneToMany(mappedBy = "item", cascade = CascadeType.ALL, orphanRemoval=true)
  private List<ValueSet> valueSets = new ArrayList<>();

  // Other fields, getters, setters, overriding equals and hashcode using Objects.equals() and Objects.hashCode() for all the fields, helper for adding and removing ValueSet
}

The Attribute entity looks like this:

@Entity
@Table(name="Attribute")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Attribute 
{
    @Id
    @Column(name="id")
    private Long id;
    
// Other fields, getters, setters, NOT overriding equals hashCode
}

Whenever an Item gets saved I need the Attribute to get saved as well. I've my postman sending JSON POST data as follows:

{
        "attribute":{
            "id":"6"
        },
        "valueSets":[
            {
                "value":"basic"
            }
        ]
}

My handler looks like this:

@PostMapping("/item")
public void postItems(@RequestBody Item item)
{
    itemRepository.save(item);
}

ItemRepository is just a one liner with @Repository annotation:

public interface ItemRepository extends CrudRepository<Item, Long>

When I try to save the Item I run into - Cannot insert the value NULL into column 'attr_id', table 'Item'; column does not allow nulls. INSERT fails.

I can't figure out why is it unable to take the id value of 6 that I am supplying as part of my POST invocation. The id value 6 already exists on the Attribute table. I have also tried making the relationship bi-directional using mappedBy and CASCADE.ALL but still get the same error.

Any thoughts/suggestions on what I'm messing/missing? Also, is there a better approach to handle nested entities? Should I try to save each of them individually? In that case can the @RequestBody still be the parent entity?

1 Answer 1

1

I have built an example project, and try to replicate your situation, not successful. I am able to insert the "item" without any issue.

I placed the project under this repository https://github.com/hepoiko/user-5483731-1

Hope this help you to troubleshooting further or let me know If I miss anything in there.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that's very helpful! Let me take a look. Also, I was wondering - do I really need bi-directional relationship for saving the child entity when the parent is saved? For example, do I need List of ValueSets in the parent or are there any other ways to save it without having the @OneToMany relation?

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.