Возникает исключение при попытке вернуть набор объектов с использованием Hibernate HQL - PullRequest
1 голос
/ 05 октября 2019

Я получаю сообщение об ошибке при попытке получить список сущностей MonitorFolder .

Вот как я построил класс MonitorFolder

package dataObjectLayer;

import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import javax.persistence.*;

@Entity
@Table(name = "monitor_folder",
        uniqueConstraints = {@UniqueConstraint(columnNames = "monitor_folder_id", name = "uc_monitorFolderId_monitor_folder"),
                @UniqueConstraint(columnNames = "path", name = "uc_pathFolder_monitor_folder")
        })
public class MonitorFolder {
    private int monitorFolderId;
    private String dateMonitored;
    private String timeMonitored;
    private String pathFolder;
    private User owner;

    public MonitorFolder() {
    }

    public MonitorFolder(int monitorFolderId, String dateMonitored, String timeMonitored, String pathFolder) {
        this.monitorFolderId = monitorFolderId;
        this.dateMonitored = dateMonitored;
        this.timeMonitored = timeMonitored;
        this.pathFolder = pathFolder;
    }

    public MonitorFolder(User owner) {
        this.owner = owner;
    }

    @Column(name = "monitor_folder_id", length = 10)
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getMonitorFolderId() {
        return monitorFolderId;
    }

    public void setMonitorFolderId(int monitorFolderId) {
        this.monitorFolderId = monitorFolderId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    @OnDelete(action = OnDeleteAction.CASCADE)
    public User getOwner() {
        return owner;
    }

    public void setOwner(User owner) {
        this.owner = owner;
    }

    @Column(name = "date_monitored")
    public String getDateMonitored() {
        return dateMonitored;
    }

    public void setDateMonitored(String dateMonitored) {
        this.dateMonitored = dateMonitored;
    }

    @Column(name = "time_monitored")
    public String getTimeMonitored() {
        return timeMonitored;
    }

    public void setTimeMonitored(String timeMonitored) {
        this.timeMonitored = timeMonitored;
    }

    @Column(name = "path")
    public String getPathFolder() {
        return pathFolder;
    }

    public void setPathFolder(String pathFolder) {
        this.pathFolder = pathFolder;
    }
}

Существует также отношение ManyToOne между MonitorFolder и Пользователь .

Теперь я пытаюсь отслеживать все папки с помощью идентификатора пользователя, используя следующую команду HQL-запрос :

public List<MonitorFolder> selectMonitoredFoldersByUser(User owner, SessionFactory sf) throws PersistenceException {
    List<MonitorFolder> monitorFolders = null;
    try (Session s = sf.openSession()) {
        String hql = " from MonitorFolder mf join fetch User u on u.userId = :insertedUserId";
        Query <MonitorFolder> q  = s.createQuery(hql);
        q.setParameter("insertedUserId", owner.getUserId());
        monitorFolders = q.getResultList();
    }
    return monitorFolders;
}

Но я получаю следующую ошибку:

 javax.xml.ws.WebServiceException: javax.xml.bind.MarshalException


- with linked exception:
[com.sun.istack.SAXException2: javax.xml.bind.JAXBException: class [Ljava.lang.Object; nor any of its super class is known to this context.
javax.xml.bind.JAXBException: class [Ljava.lang.Object; nor any of its super class is known to this context.]
    at com.sun.xml.ws.message.jaxb.JAXBMessage.writePayloadTo(JAXBMessage.java:403)
    at com.sun.xml.ws.message.AbstractMessageImpl.writeTo(AbstractMessageImpl.java:154)
    at com.sun.xml.ws.api.message.MessageWrapper.writeTo(MessageWrapper.java:199)
    at com.sun.xml.ws.encoding.StreamSOAPCodec.encode(StreamSOAPCodec.java:134)
    at com.sun.xml.ws.encoding.SOAPBindingCodec.encode(SOAPBindingCodec.java:242)
    at com.sun.xml.ws.transport.http.HttpAdapter.encodePacket(HttpAdapter.java:432)
    at com.sun.xml.ws.transport.http.HttpAdapter.access$100(HttpAdapter.java:100)
    at com.sun.xml.ws.transport.http.HttpAdapter$1.onCompletion(HttpAdapter.java:534)
    at com.sun.xml.ws.server.WSEndpointImpl$1.onCompletion(WSEndpointImpl.java:326)
    at com.sun.xml.ws.api.pipe.Fiber.completionCheck(Fiber.java:867)
    at com.sun.xml.ws.api.pipe.Fiber.run(Fiber.java:774)
    at com.sun.xml.ws.api.pipe.Fiber.start(Fiber.java:425)
    at com.sun.xml.ws.server.WSEndpointImpl.processAsync(WSEndpointImpl.java:349)
    at com.sun.xml.ws.server.WSEndpointImpl.process(WSEndpointImpl.java:359)
    at com.sun.xml.ws.transport.http.HttpAdapter.invokeAsync(HttpAdapter.java:530)
    at com.sun.xml.ws.transport.http.servlet.ServletAdapter.invokeAsync(ServletAdapter.java:206)
    at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doGet(WSServletDelegate.java:159)
    at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doPost(WSServletDelegate.java:194)
    at com.sun.xml.ws.transport.http.servlet.WSServlet.doPost(WSServlet.java:80)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1457)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.SAXException2: javax.xml.bind.JAXBException: class [Ljava.lang.Object; nor any of its super class is known to this context.
javax.xml.bind.JAXBException: class [Ljava.lang.Object; nor any of its super class is known to this context.]
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:276)
    at com.sun.xml.bind.v2.runtime.BridgeImpl.marshal(BridgeImpl.java:104)
    at com.sun.xml.bind.api.Bridge.marshal(Bridge.java:145)
    at com.sun.xml.ws.db.glassfish.BridgeWrapper.marshal(BridgeWrapper.java:170)
    at com.sun.xml.ws.message.jaxb.JAXBMessage.writePayloadTo(JAXBMessage.java:394)
    ... 42 more
Caused by: com.sun.istack.SAXException2: javax.xml.bind.JAXBException: class [Ljava.lang.Object; nor any of its super class is known to this context.
javax.xml.bind.JAXBException: class [Ljava.lang.Object; nor any of its super class is known to this context.
    at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:247)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:262)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:653)
    at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:169)
    at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:159)
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:361)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696)
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:271)
    ... 46 more
Caused by: javax.xml.bind.JAXBException: class [Ljava.lang.Object; nor any of its super class is known to this context.
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:593)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:648)
    ... 51 more

Это связано с тем, что я возвращаюуниверсальный список с типом MonitorFolder, который JAXB не может маршалировать?

1 Ответ

0 голосов
/ 12 октября 2019

Проблема заключалась в том, что hql явно не сообщает, какой тип объекта он будет возвращать. Я изменил:

from MonitorFolder mf join fetch User u on u.userId = :insertedUserId

на:

select mf from MonitorFolder mf join fetch User u on u.userId = :insertedUserId
...