我无法从通过请求收到宁静的Java客户端的实体中检索oneToMany列集合时遇到问题:
Itinerary itinerary = port.checkItinerary_XML(Itinerary.class, from, to);
行程对象的所有内容都是'检索到的数据,但当我尝试检索其数据时OneToMany列我只获得一个空集合:
Collection<Route> routeCollection = itinerary.getRouteCollection();
我尝试了懒惰和渴望的fetchtype,但都没有奏效。
行程实体(相关部分):
@Entity
@Table(name = "ITINERARY")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Itinerary.findAll", query = "SELECT i FROM Itinerary i"),
@NamedQuery(name = "Itinerary.findById", query = "SELECT i FROM Itinerary i WHERE i.id = :id"),
@NamedQuery(name = "Itinerary.findByStartCity", query = "SELECT i FROM Itinerary i WHERE i.startCity = :startCity"),
@NamedQuery(name = "Itinerary.findByEndCity", query = "SELECT i FROM Itinerary i WHERE i.endCity = :endCity")})
public class Itinerary implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "ID")
private Integer id;
@OneToMany(fetch=FetchType.LAZY,cascade = CascadeType.ALL, mappedBy = "itineraryId")
private Collection<Route> routeCollection= new ArrayList();
public Itinerary() {
}
public Itinerary(Integer id) {
this.id = id;
}
@XmlTransient
@JsonIgnore
public Collection<Route> getRouteCollection() {
return routeCollection;
}
public void setRouteCollection(Collection<Route> routeCollection) {
this.routeCollection = routeCollection;
}
路线实体(仅限相关部分):
@Entity
@Table(name = "ROUTE")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Route.findAll", query = "SELECT r FROM Route r"),
@NamedQuery(name = "Route.findById", query = "SELECT r FROM Route r WHERE r.id = :id"),
@NamedQuery(name = "Route.findByFromCity", query = "SELECT r FROM Route r WHERE r.fromCity = :fromCity"),
@NamedQuery(name = "Route.findByToCity", query = "SELECT r FROM Route r WHERE r.toCity = :toCity")})
public class Route implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "ID")
private Integer id;
@JoinColumn(name = "ITINERARY_ID", referencedColumnName = "ID")
@ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.ALL,optional = false)
private Itinerary itineraryId;
public Route() {
}
public Route(Integer id) {
this.id = id;
}
public Itinerary getItineraryId() {
return itineraryId;
}
public void setItineraryId(Itinerary itineraryId) {
this.itineraryId = itineraryId;
}
}
编辑:处理checkItinerary_XML调用的Restful Itinerary客户端:
public class ItineraryClient {
private WebTarget webTarget;
private Client client;
private static final String BASE_URI = "http://localhost:8080/id2208_hw3_server/webresources";
public <T> T checkItinerary_XML(Class<T> responseType, String departureCity, String destinationCity) throws ClientErrorException {
WebTarget resource = webTarget;
resource = resource.path(java.text.MessageFormat.format("checkItinerary/{0}/{1}", new Object[]{departureCity, destinationCity}));
return resource.request(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
}
答案 0 :(得分:0)
如果您在事务方法中检索itinerary = port.checkItinerary_XML(Itinerary.class, from, to);
,然后尝试在事务之外的某个时间点获取routeCollection
,那么您将获得一个空集合。
解决方案是在用于检索iteneray的事务中进行调用。