親子関係を持つエンティティが 2 つあります。犬は明らかに親で、子犬は子です。エラーなしで Dog-and-puppies を永続化するにはどうすればよいですか?
@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
}
基本的に、ユーザーは Dog を子犬とともに 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();
}
繰り返しになりますが、子犬に親犬の dog_id を表示させるにはどうすればよいですか?