马歇尔实体没有任何关系

时间:2018-08-24 12:39:38

标签: java xml jpa

当前,我有一个使用JPA连接到看起来像这样的数据库的类。

public class Person {
    private String name;
    private int age;

    @OneToMany
    private List<Pet> pets;

    @OneToOne
    private House house;
}

我正在使用javax.ws.rs批注来创建像这样的xml Web服务

@GET
@Path("/load")
@Produces("application/xml")
public Response loadByPetId(@QueryParam("petId") int petId) {
    return Response.ok(personBean.loadByPetId(petId)).build();
}

(我正在使用eclipselink)

@Stateless
public class PersonBean {
    @PersistenceContext(unitName = "...")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public PersonBean() {
        super(Person.class);
    }

    public List<Person> loadByPetId(int petId) {
        return em.createNamedQuery("Person.findByPetId", Person.class).setParameter("petId", petId).getSingleResult();
    }
}

返回一个XML,其中包含给定Person之类的每个属性和关系,例如

<Person>
    <name>toto</name>
    <age>18</age>
    <Pets>
        <Pet>
            ...
        </Pet>
        <Pet>
            ...
        </Pet>
        ...
    </Pets>
    <House>
        ...
    </House>
</Person>

我希望能够创建另一个Web服务,该Web服务仅加载给定Person的属性而没有关系,例如

<Person>
    <name>toto</name>
    <age>18</age>
</Person>

但是,当我使用Pets加载正确的Person时,Pets列表已加载,我不知道如何“卸载” 防止它出现在我的xml中。

0 个答案:

没有答案
相关问题