我有一个运行EclipseLink的Web服务。我已经使用基于数据库模式的NetBeans生成了一些Java类(“数据库中的实体类...”)。

我的问题是有关NetBeans根据数据库模式生成的两个类,称为Person.java和Group.java。 Person.java和Group.java是JAR文件“ MyLib.jar”的一部分

在Group.java中,我有以下代码(以及其他代码):

@Column(name = "groupName")
private String groupName;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "group")
private Collection<Person> personCollection;

public String setGroupName(String groupName) {
    this.groupName = groupName;
}

public String getGroupName(){
    return groupName;
}

@XmlTransient
public Collection<Person> getPersonCollection() {
    return personCollection;
}

public void setPersonCollection(Collection<Person> personCollection) {
    this.personCollection = personCollection;
}


我有两个不同的应用程序,其中包括“ MyLib.jar”。一个是Web服务本身,另一个是连接到Web服务的客户端。客户端当然也包括Web服务接口JAR。

在客户端中,我创建了一个这样的人员集合,然后将组实例发送到Web服务:

Collection<Person> persons = new ArrayList<Person>();
Person person = new Person();
person.setName("Peter");
persons.add(person);
group.setName("GroupName");
group.setPersonCollection(persons);
System.out.print(persons); // prints the instance and its content
webserviceInstance.setGroup(group);


真正奇怪的是,在Web服务方法setGroup中,集合为null!

@Override
public void setGroup(Group group) {
    System.out.print(group.getGroupName()); // prints "GroupName"
    System.out.print(group.getPersonCollection()); // prints null meaning the instance is null
    ...
}


我根本无法理解为什么Web服务中的集合为null,而客户端中的集合为null。
我做错什么了吗?

最佳答案

问题是,WebService不直接支持集合,您需要实现一个包含对象集合的类,类似于:

public class ListExamenConductor implements List<Examenconductor>{

    private ArrayList<Examenconductor> al = new ArrayList<Examenconductor>();
.... //Need to implements all methods


然后,在webService中,将所有集合发送到Object,如下所示:

@WebMethod(operationName = "consultarExamenConductor")
    public ListExamenConductor consultarExamenConductor(@WebParam(name = "identificacion") Identificacion identificacion) {
        System.out.println("Consultar examenes, consultado en "+new Date());
        EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("scrc_proyectoPU");
        EntityManager em = emFactory.createEntityManager();
        ExamenconductorJpaController jpaController  = new ExamenconductorJpaController(em.getTransaction(), emFactory);
        ListExamenConductor  examenes = new ListExamenConductor();
        examenes.addAll(jpaController.consultarExamen(identificacion));
        System.out.println("Consultar antecedente, resultado "+examenes.size() + " en "+new Date());
        return examenes;
    }


有用

07-28 03:37
查看更多