1

I have a web service running EclipseLink. I have generated some Java classes with NetBeans that are based on the database schema ("Entity classes from database...").

My problem is about two classes called Person.java and Group.java that have been generated by NetBeans based on the database schema. Person.java and Group.java is part of the JAR file "MyLib.jar"

In Group.java, I have this code (among other code):

@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;
}

I have two different applications that include the "MyLib.jar". One is the web-service itself and the other is a client that connects to the web-service. The client also, of course, includes the web service interface JAR.

In the client I create a collection of Persons like this and then send the group instance to the web-service:

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);

The really strange thing is that in the web-service method setGroup, the collection is 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
    ...
}

I simply cannot understand why the collection is null in the web service but not in the client. Am I doing anything wrong?

4

2 回答 2

0

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

public class ListExamenConductor implements List<Examenconductor>{

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

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

@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;
    }

有用

于 2013-11-21T04:35:07.837 回答
-1

我有同样的问题,解决方案是创建其他不使用 JPA 的类,并且使用这个类工作 WebService,我认为 jpa 条目的标签与

于 2013-11-13T23:51:20.520 回答