I have the class book:
@Entity
@Table(name = "books")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType
public class Book {
@Id
@XmlAttribute
private String isbn;
private String title;
private String authors;
private int year;
@OneToMany(mappedBy="reservedBook")
private List<Reservation> bookReservations;
//Getters, setters, addReservation, remove Reservation
......................
}
Then, I have class Reservation
@Entity
@Table(name = "reservations")
@XmlRootElement
public class Reservation {
private String username;
private String isbn;
@Temporal(TemporalType.DATE)
private Date date;
@ManyToOne
@JoinColumn(name = "isbn")
private Book reservedBook;
@ManyToOne
@JoinColumn(name = "username")
private User userWhoReserved;
//Getters and setters
...........
}
In the resource class I try to get a specific book like this:
@GET
@Path("/{isbn}")
@Produces(MediaType.TEXT_XML)
public Book getBookByIsbn(@PathParam("isbn") String isbn) {
Book book = entityManager.find(Book.class, isbn);
if (book != null) {
return book;
}
}
Now, it doesn’t serialize field List bookReservations. I’ve tried a lot of ideas found on the internet, like annotating the getter for the list with @XmlElement or using other annotations in other places (I don’t remember them now), but nothing worked.
EDIT: This is how a response looks like:
<book isbn="3333333333">
<title>Mere Christianity</title>
<authors>C. S. Lewis</authors>
<year>2000</year>
</book>
but I also want to show the reservations.
What seems to be the solution?
Thanks!
Sorin
I think the problem may be with the list
bookReservationsbeingnull.I use this code everyday and works for me (Ignore other annotations that I have not provided).
I have faced problem in these cases but only found that due to the list being null for some reason it is not serialized. Try checking whether the list is being set by JAXB in the setter method by debugging at that method.