0

I have Building entity:

@Entity
@Data
public class Building {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "building_id", nullable = false, unique = true)
private int buildingId;

@Column(name = "name", nullable = false, length = 255)
private String name;

@Column(name = "address", nullable = false, length = 255)
private String address;

@Column(name = "deleted", nullable = false)
private Boolean isDeleted = Boolean.FALSE;

@OneToMany(mappedBy = "building", cascade = CascadeType.MERGE)
private List<Floor> floorList;
}

And Floor entity:

@Entity
@Data
public class Floor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "floor_id", nullable = false, unique = true)
private int floorId;

@Column(name = "name", nullable = false, length = 255)
private String name;

@Column(name = "deleted")
private Boolean isDeleted = Boolean.FALSE;

@ManyToOne
@JoinColumn(name = "building_id")
private Building building;
}

So one Building has many Floors and i have trouble when inserting data in Building to update building_id in Floor entity (floor is populated table). This is example of my Building payload:

{
"name": "Some name",
"address": "Some address",
"floorList": [
    {
        "floorId": 1
    }
]   
}

I guess the problem can be cascading, but maybe i am completly wrong. So what can i do to solve this problem?

N J
  • 33
  • 4
  • 3
    have you tried various cascade types? I don't know much but I would try with `Cascade.ALL` and figure out if cascade is the problem or if there's anything else. – Arun Gowda Dec 27 '21 at 13:56
  • @ArunGowda Yes i did try `Cascade.ALL` but then i get `detached entity passed to persist` error – N J Dec 27 '21 at 14:35
  • Did you check this one out? Setters on both the entities could be the issue https://stackoverflow.com/a/13500921/8283737 – Arun Gowda Dec 28 '21 at 04:14

0 Answers0