I'm having trouble with a relationship between two attributes (owners and pets: one owner should be able to have many pets) in spring, this is my code:
@Entity
@Table(name="owners_table")
public class Owners {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(nullable=false)
private String name;
@Column(nullable=false)
private String phoneNumber;
@Column(nullable=true)
private String email;
@Column(nullable=false)
private String address;
@OneToMany(fetch=FetchType.LAZY,mappedBy="id")
@JsonManagedReference(value="pet-owners")
@JsonIgnore
private List<Pets> pets;
public Owners() {
}
@JsonCreator
public Owners(Integer id, String name, String phoneNumber, String email, String address) {
this.id = id;
this.name = name;
this.phoneNumber = phoneNumber;
this.email = email;
this.address = address;
}
(plus the getters and setters)
@Entity
@Table(name="pets_table")
public class Pets {
private String name;
private String breed;
private Date birthDate;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonBackReference(value="pet-owners")
@JoinColumn(name = "owner_id")
private Owners owner;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name="walk_id")
@JsonBackReference(value="pet-walks")
private DogWalking walk;
public Pets() {
}
@JsonCreator
public Pets(String name, String breed, Date birthDate, Owners owner , Integer id) {
this.name = name;
this.breed = breed;
this.birthDate = birthDate;
this.id = id;
}
(plus getters and setters)
I believe this is the only part of the code that matters for this question, but if you need anything else you can tell me and I'll put it here.
I'm gonna explain the problem in action now: I had this owner in my database: (my request: localhost:8080/owners/6)
{
"id": 6,
"name": "Lucas Calado",
"phoneNumber": "(83) 98326-9273",
"email": "lucasthecalado@gmail.com",
"address": "Rua Bancário João Luiz , 92",
"pets": []
}
and then I added one pet to the pet list: (my request: localhost:8080/pets/insert)
{
"name": "Fenrir",
"breed": "Husky siberiano",
"birthDate": "2017-03-08T03:00:00.000+00:00",
"owner":{
"id":6
}
"walks": null
}
and, until now, everything is fine
now, if I make the same request to get the owner by id, this is whats shown to me:
{
"id": 6,
"name": "Lucas Calado",
"phoneNumber": "(83) 98326-9273",
"email": "lucasthecalado@gmail.com",
"address": "Rua Bancário João Luiz , 92",
"pets": [
{
"name": "Fenrir",
"breed": "Husky siberiano",
"birthDate": "2017-03-08T03:00:00.000+00:00",
"id": 6,
"walks": null
}
]
}
BUT, when I try to add another pet to the same owner id, it goes to the next owner id (in this case, id=7, and nothing happens to the owner that has the id 6. (my request: localhost:8080/pets/insert)
{
"name": "Roabson",
"breed": "Pug",
"birthDate": "2018-12-02T03:00:00.000+00:00",
"owner":{
"id":6
}
}
my owner (id=6) stays the absolute same, with only one pet, and my next owner (id=7) recieves this pet.