0

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.

vitoriac
  • 79
  • 1
  • 1
  • 7
  • There is a difference between both the insert pet requests. – Arun Sudhakaran Feb 09 '23 at 13:35
  • @Arun Sudhakaran I'm sorry! Its because I copied from the json that was already working (owners/1) I just took the pet from inside it, but the request I had done to insert the pet was exaclty the same as the second one, I just changed the name and the breed. But I'll edit my question so it looks the same – vitoriac Feb 09 '23 at 13:54

2 Answers2

0

Try using this sample.

In Owners class:

@OneToMany(mappedBy = "owners", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Pets> pets = new HashSet<>();

In Pets class:

@ManyToOne
@JoinColumn(name = "owners_id", referencedColumnName = "id")
private Owners owners;

It is also better not to use plural nouns for classes.

0

Every time you send a list this way, you rewrite it. You don't add an item, you rewrite the list. If you want to add, you request already exisiting items of the list, add a new one, and send this altered list.

dimirsen Z
  • 873
  • 13
  • 16