0

I have two entities with a parent-child relationship. Dog is the parent obviously and the Puppy is the child. How do I persist Dog-and-puppies without error?

@XmlRootElement(name = "dog")
@Entity
@Table(name = "dog", catalog = "zoo")
public class Dog{
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Integer id;

   @Column(name = "dogname")
   private String dogName;

   @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "Dog")
   private Set<Puppy> puppies;

   ...// getters and setters

   @XmlElementWrapper(name = "puppies")
   @XmlElement(name = "puppy")
   public Set<Puppy> getPuppy() {
      return this.puppies;
   }
}

@Entity
@Table(name = "puppy", catalog = "zoo")
public class Puppy{
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Integer id;

   @Column(name = "puppyname")
   private String puppyName;

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "dog_id", nullable = false)
   private Dog dog;

       ...// getters and setters
}

Basically a user passes in a Dog -- with it's puppies -- as a jaxbElement.

@POST
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response createDog(JAXBElement<Dog> jaxbDog) {
    Dog dog = jaxbDog.getValue();
   dogDao.save(dog);
    return Response.status(Response.Status.OK).build();
}

My question, again, is how do I get the puppies to see the dog_id of the parent dog?

1 Answer 1

3

Add a method in Dog entity class as :

    public void addPuppy(Puppy puppy){
           if (this.puppies == null){
               this.puppies = new HashSet<Puppy>();
           }
           puppy.setDog(this); // <--This will set dog as parent in puppy class
           this.puppies.add(puppy);
    }

Add Cascade as ALL for puppies mapping in Dog.

When you want to save dog with puppy/puppies, you can write something like:

       Dog dog = ....getDog object/create new dog object
       Puppy puppy = new Puppy();
       // set puppy attributes.
       dog.addPuppy(puppy);
       //call save method for dog
       dogDao.save(dog);
Sign up to request clarification or add additional context in comments.

3 Comments

by addPuppy(Puppy puppy) do you mean in addition to setPuppy(Puppy puppy). I already have setters and getters.
also what do you mean by Add Cascade as ALL for puppies mapping in Dog. Don't I already have that? Please, clarify. You may copy my code into your response and edit it into the correct version. Thanks.
No. setPuppy is taking Set as parameter. I am talking abot the new method, I have mentioned in the answer. Add the method addPuppy in your Dog class and then use this method to add one puppy at a time to the dog. If you want to use setPuppy then update that method to iterate puppies and set dog i.e. this in each of the puppies.

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.