0

I have two entities, one is Meal and one is Category. Each category (essentially types of meals, breakfast, dinner etc).

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;

@Data
@Entity
@Table(name = "meal_table")
@EqualsAndHashCode(exclude = {"category"})
public class Meal implements Serializable {
    @Serial
    private static final long serialVersionUID = 888L;

    @Id
    @SequenceGenerator(
            name = "id_sequence",
            sequenceName = "id_sequence",
            allocationSize = 1
    )
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "id_sequence")
    private Long id;

    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "categoryId", referencedColumnName = "id")
    @JsonIgnoreProperties("meals")
    private Category category;

    private BigDecimal price;
    //item code
    private String code;

    private String imgUrl;
    private String description;

    // 0 unavailable 1 available
    private Integer status;

//    private LocalDateTime createTime;
//    private LocalDateTime updateTime;

}


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

@Data
@Entity
@Table(name = "category_table")
@EqualsAndHashCode(exclude = "meals")
public class Category implements Serializable {
    @Serial
    private static final long serialVersionUID = 888L;
    @Id
    @SequenceGenerator(
            name = "id_sequence",
            sequenceName = "id_sequence",
            allocationSize = 1
    )
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "id_sequence")
    private Long id;

    @Column(unique = true)
    private String name;


    @OneToMany(mappedBy = "category",cascade = CascadeType.ALL)
    @ToString.Exclude
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler", "category"})
    Set<Meal> meals = new HashSet<>();

    public void addMeal(Meal meal){
        meals.add(meal);
    }
}

I also have a simple DTO class

@Data
public class MealDto extends Meal {
    private String categoryName;
    private Integer copies;
}

This is the method I used to save the newly added meal

@PostMapping("/")
    public CommonResult saveMeal(@RequestBody MealDto mealDto){
        log.info(mealDto.toString());
        Category c = categoryService.findByName(mealDto.getCategoryName());
// it alwyas prints the desired Category instance.
        log.info(c.toString());

        Meal meal = new Meal();
        BeanUtils.copyProperties(mealDto, meal);
        log.info(meal.toString());
        c.addMeal(meal);
        //https://stackoverflow.com/a/44263632/13062745
        meal.setCategory(c);
        mealService.save(meal);
        return CommonResult.success();
    }


However, when I ran the post method in debug mode, I can see that there is an infinite nested dependency of 'Category' and 'Meal' after calling ```meal.setCategory(c)```. I've tried several ways but still not working.

enter image description here

zxcisnoias
  • 494
  • 3
  • 19

1 Answers1

0

You're going to have to either implement your own copy method, or convince BeanUtils to skip the copy on one side or the other. Something like this, possibly:

BeanUtils copyProperties API to ignore null and specific propertie

EDIT: Also, without seeing the stack trace, I assume the problem is in the copy, not on the save, but I could be wrong.