Java OneToMany列返回空集合
我从一个实体检索oneToMany列集合时遇到问题,该实体是我通过向restful java客户端发出请求而收到的:Java OneToMany列返回空集合,java,rest,jpa,Java,Rest,Jpa,我从一个实体检索oneToMany列集合时遇到问题,该实体是我通过向restful java客户端发出请求而收到的: Itinerary itinerary = port.checkItinerary_XML(Itinerary.class, from, to); Cinerary对象已检索到其所有的“数据”,但当我尝试检索其“OneToMany”列时,只得到一个空集合: Collection<Route> routeCollection = itinerary.g
Itinerary itinerary = port.checkItinerary_XML(Itinerary.class, from, to);
Cinerary对象已检索到其所有的“数据”,但当我尝试检索其“OneToMany”列时,只得到一个空集合:
Collection<Route> routeCollection = itinerary.getRouteCollection();
编辑:处理CheckInvesture\u XML调用的Restful行程客户端:
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);
}
公共类行程客户端{
私有WebTarget WebTarget;
私人客户;
私有静态最终字符串BASE_URI=”http://localhost:8080/id2208_hw3_server/webresources";
public T checkInternation_XML(类响应类型、字符串departureCity、字符串destinationCity)引发ClientErrorException{
WebTarget资源=WebTarget;
resource=resource.path(java.text.MessageFormat.format(“checkinventary/{0}/{1}”,新对象[]{departureCity,destinationCity}));
返回resource.request(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
}
如果在事务方法中检索行程=端口。检查行程\u XML(itinerary.class,from,to);
,然后在事务之外的稍后时间点尝试获取路由集合
,则将获得空集合
解决方案是在用于检索ITeneraly的事务中进行调用。好的,getter用XmlTransient和JsonIgnore进行注释,因此无论它是序列化为XML还是JSON,封送员都会忽略该集合。您能告诉我们您实际在何处检索`行程行程=端口'的代码吗。checkInternerary_XML(Itinerary.class,from,to);`and
Collection routeCollection=Itinerary.getRouteCollection()
?@JB Nizet我试图删除它,但后来发现端口的内部服务器错误。检查行程调用。我应该如何处理它?@idipous我添加了代码段。类似于在我的端口对象getInventureRouteCollection中创建一个新方法,我首先检索行程对象,然后在检索到的对象上进行调用tRouteCollection并返回它?我在主帖子中添加了路线检索代码,以便您查看。好的,以便理解。您可以从web服务中获得一个路线
对象。为什么这个POJO应该知道您在数据库中定义的路线集合
?换句话说,您拥有的ITeneral
没有我没搞定。对,还是我错过了什么?
@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;
}
}
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);
}