无法初始化代理 - 在jax-ws + hibernate中没有Session

时间:2013-05-28 12:10:40

标签: hibernate jaxb jax-ws

我坚持使用JAX-WS公开Web服务。由于JAXB在休眠关闭会话后尝试序列化对象,因此会出现问题。我谷歌在过去的两天里,但无法得到正确的答案。以下是其他人给出的某种解决方案。

  • 渴望加载对象
  • 使用@XmlTransient
  • SOAPHandler但未完成答案

以下是详细情景。我写了一个模型类,其中包含@OneTOMany关系,@ ManyToOne关系和@OneToOne关系。 Web服务返回该类的对象。当我通过SOAP UI调用服务时,它给出了以下错误消息。

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
         <faultcode>S:Server</faultcode>
         <faultstring>could not initialize proxy - no Session</faultstring>
      </S:Fault>
   </S:Body>
</S:Envelope> 

模型类(没有getter和setter)

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
@Entity
@AttributeOverrides({ @AttributeOverride(name = "id", column = @Column(name = "mas_form_id")),
        @AttributeOverride(name = "tenantId", column = @Column(name = "mas_form_tenant_id")) })
@FilterDef(name = "tenantFilter", parameters = @ParamDef(name = "tenantIdParam", type = "string"))
@Filters(@Filter(name = "tenantFilter", condition = "mas_form_tenant_id = :tenantIdParam"))
@Table(name = "mas_form")
public class Form extends BaseRevolution {

    @Column(name = "mas_form_action")
    private String formAction;
    @Column(name = "mas_form_is_active")
    private boolean isActive;
    @Column(name = "mas_form_is_lock")
    private boolean isLock;
    @ManyToOne
    @JoinColumn(name = "mas_ft_id")
    private FormType formType;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
    private List<FormQuestion> formQuestions = new ArrayList<FormQuestion>(0);
    @OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
    @LazyCollection(LazyCollectionOption.FALSE)
    private Set<DocumentRequired> documentRequiredSet = new HashSet<DocumentRequired>(0);
    @OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
    private List<Application> applications = new ArrayList<Application>(0);
    @OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<FormField> formFields = new ArrayList<FormField>(0);
    @OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
    private List<FormCharge> formCharges = new ArrayList<FormCharge>(0);
    @Column(name = "mas_form_method")
    private String formMethod;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
    private List<FormFieldGroup> formFieldGroup;
    @Column(name = "mas_form_name_en")
    private String formNameEn;
    @Column(name = "mas_form_name_si")
    private String formNameSi;
    @Column(name = "mas_form_name_ta")
    private String formNameTa;
    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "activity")
    private ElgActivity elgActivity;
    @Column(name = "description", nullable = true, length = 1000)
    private String description;

    //getters and setters

}

Web服务接口

@WebService(name = "formWebServicePort", targetNamespace = "http://lk.gov.elg/core/ws/form/")
public interface FormWebService {

    @WebMethod(operationName = "getAllFormsWithDocumentReferences", action = "urn:GetAllFormsWithDocumentReferences")
    @SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
    @WebResult(name = "formsWithDocumentReferences")
    List<Form> getAllFormsWithDocumentReferences(
            @WebParam(name = "tenantId", targetNamespace = "http://lk.gov.elg/core/ws/form/") String tenantId,
            @WebParam(name = "categoryName", targetNamespace = "http://lk.gov.elg/core/ws/form/") String categoryName);
}

Web服务实现

@SchemaValidation
@WebService(targetNamespace = "http://lk.gov.elg/core/ws/form/", endpointInterface = "lk.gov.elg.core.ws.citizen.FormWebService")
public class FormWebServiceImpl extends SpringBeanAutowiringSupport implements FormWebService {

    private static final Logger logger = LoggerFactory.getLogger(FormWebServiceImpl.class);
    @Autowired
    private FormService formService;

    @Override
    public List<Form> getAllFormsWithDocumentReferences(
            @WebParam(name = "tenantId", targetNamespace = "http://lk.gov.elg/core/ws/form/") String tenantId,
            @WebParam(name = "categoryName", targetNamespace = "http://lk.gov.elg/core/ws/form/") String categoryName) {

        List<Form> formList = formService.getAllFormsWithDocRefs(tenantId, categoryName);
        return formList;
    }
}

帮我解决这个问题。提前谢谢。

2 个答案:

答案 0 :(得分:0)

您是否尝试在退出会话之前初始化Form实体的集合。这会导致加载集合。所以基本上它应该做同样的急切加载,但不知何故,急切加载并不总是有帮助。如果需要,我可以在以后或明天挖掘更多细节。

答案 1 :(得分:0)

只是一个想法:我们最近通过向sessionbean添加(EJB3)拦截器方法来克服这个问题(我们使用的是JBoss7):

@javax.interceptor.AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
        Object result = ctx.proceed();

        // Add code here to inspect the result and initialize all collections.

        return result;
}