gpt4 book ai didi

java - 尝试使用 Hibernate HQL 返回一组对象时抛出异常

转载 作者:太空宇宙 更新时间:2023-11-04 09:21:42 25 4
gpt4 key购买 nike

我在尝试获取实体 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;
}
}

MonitorFolderUser 之间也存在多对一关系。

现在,我尝试使用以下HQL查询获取由用户ID监控的所有文件夹:

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

这是否与我返回一个 JAXB 无法编码的 MonitorFolder 类型的通用列表有关?

最佳答案

问题是 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

关于java - 尝试使用 Hibernate HQL 返回一组对象时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58252087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com