如何在JPA和REST中获取实体内部的属性列表?

时间:2013-08-29 22:07:05

标签: jpa collections entity eclipselink jersey-2.0

环境

泽西 Eclipselink(JPA)

实体

国家---城市

@OneToMany(cascade = CascadeType.ALL, mappedBy = "countryCountryId")
private Collection<City> cityCollection;

@XmlTransient
public Collection<City> getCityCollection() {
        return cityCollection;
    }

REST

@GET
@Override
@Produces({"application/xml", "application/json"})
public List<Country> findAll() {
    return super.findAll();
}

RESULT

<countries>
  <country>
   <country>Finland</country>
   <countryId>1</countryId>
   <lastUpdate>2013-08-30T00:43:35+03:00</lastUpdate>
  </country>
 <country>
  <country>Sweden</country>
  <countryId>2</countryId>
  <lastUpdate>2013-08-30T00:43:35+03:00</lastUpdate>
 </country>
</countries>

问题

为什么即使有字段也没有城市呢?

如何在同一个@GET中获得城市?

甚至可能,我想是这样吗?

由于 萨米

1 个答案:

答案 0 :(得分:1)

@XmlTransient---> **THIS OFF!**
public Collection<City> getCityCollection() {
        return cityCollection;
    }

@XmlTransient off我把它移到了:

@XmlTransient
public Country getCountryCountryId() {
    return countryCountryId;
}

它正在发挥作用:)