HIbernate不会抛出异常或显示错误 [英] HIbernate doesn't throw exception or show errors

查看:147
本文介绍了HIbernate不会抛出异常或显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Jersey和Hibernate开发一个小型REST Web服务器。一路上,我遇到了代表Hibernate的奇怪行为。也就是说,当从curl POST请求后尝试Session.save()对象时,服务器回复500,但在命令窗口中看不到任何错误。



<但是,当我切换一个断点并在游标的某些jar中运行游标时,我最终会追踪错误消息。它仍然没有显示在任何地方。

如何强制hibernate显示所有这些错误?或者它是否需要设置Jersey?



切换断点的方法:



<$ p $ ($ {code> @POST
@Path({id} / comments / new)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response handlePostNewComment(@PathParam(id)int taskId,ItemComment newComment){
newComment.setItemId(taskId);
Session s = HibernateUtil.getSessionFactory()。openSession();
Transaction tx = s.beginTransaction();
**整数newCommentId =(整数)s.save(newComment); **
s.flush();
tx.commit();
s.close();
HashMap res = new HashMap();
res.put(id,newCommentId);
return Response.status(201).entity(res).build();

服务器对curl的回应:


$ curl -i -X POST -HContent-type:application / json--data @ comm.json localhost:8000 / task / 19 / comments / new



HTTP / 1.1 500请求失败。
Content-Type:text / html; charset = ISO-8859-1
日期:2015年10月27日星期二20:34:16 GMT
连接:关闭
Content-Length :1033


消息在IntelliJ控制台中:

 连接到目标VM,地址:'127.0.0.1:57501',transport:'socket'
任务跟踪器企业版运行
2015年10月27日下午9:34:12 org .glassfish.grizzly.http.server.NetworkListener start
INFO:已启动的监听器绑定到[localhost:8000]
应用程序已启动。
使用CTRL + C
停止应用程序org.glassfish.grizzly.http.server.HttpServer start
INFO:[HttpServer] Started。
2015年10月27日下午9:34:14 org.hibernate.Version logVersion
INFO:HHH000412:Hibernate Core {5.0.2.Final}
2015年10月27日上午9:34:14 PM org.hibernate.cfg.Environment< clinit>
INFO:HHH000206:找不到hibernate.properties
2015年10月27日下午9:34:14 org.hibernate.cfg.Environment buildBytecodeProvider
INFO:HHH000021:字节码提供程序名称:javassist
Oct 27,2015 9:34:14 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager< clinit>
INFO:HCANN000001:Hibernate Commons Annotations {5.0.0.Final}
2015年10月27日下午9:34:14 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
警告:HHH000402:使用Hibernate内置连接池(不适用于生产!)
2015年10月27日下午9:34:14 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO:HHH000401:在URL [jdbc:postgresql:// localhost:5432 / tracker]使用驱动程序[org.postgresql.Driver]
2015年10月27日下午9:34:14 org.hibernate.engine.jdbc。 connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO:HHH000046:连接属性:{user = cristian,password = ****}
Oct 27,2015 9:34:14 PM org.hibernate.engine.jdbc .connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO:HHH000006:Autocommit模式:false
2015年10月27日下午9:34:14 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl conf igure
INFO:HHH000115:Hibernate连接池大小:1(min = 1)
org.hibernate.dialect.Dialect< init>
INFO:HHH000400:使用方言:org.hibernate.dialect.PostgreSQL94Dialect
2015年10月27日下午9:34:15 org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO:HHH000424:禁用上下文LOB创建为createClob()方法抛出错误:java.lang.reflect.InvocationTargetException
2015年10月27日下午9:34:15 org.hibernate.type.BasicTypeRegistry注册
INFO :HHH000270:类型注册[java.util.UUID]覆盖以前:org.hibernate.type.UUIDBinaryType@43aa6671
2015年10月27日下午9:34:15 org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO:HHH000397:使用ASTQueryTranslatorFactory

成功的请求看起来是一样的,除了一些SQL



在调试时,我终于遇到了org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher类中的异常。







我能做些什么来强制这些组件向我显示stdout或stderr上的错误消息?或将它们记录在某处?

解决方案

这就是这样的情况我只注册了一个 ExceptionMapper< Throwable> 。这些用于将异常映射到 Response s。有时在有未处理的异常情况下,Jersey会吞下它,向客户端发送一个有时无用的500 Server Error。在这些情况下, ExceptionMapper 将允许我们自己处理它,至少在发送响应之前打印堆栈跟踪

  @Provider 
public class DebugExceptionMapper实现ExceptionMapper< Throwable> {

@Overrride
public ResponseResponse(Throwable e){
e.printStackTrace();
return Response.serverError()
.entity(e.getMessage())
.build();
}
}

另请参阅:




I'm developing a small REST web server with Jersey and Hibernate. Along the way I've encountered some weird behaviour on behalf of Hibernate. Namely, when trying to Session.save() an object after POST request from curl, the server replies with 500, but I don't see any error in the command window.

But when I toggle a breakpoint and do a step over the cursor runs inside some of the jars in the dependencies, where I eventually track down the error message. Still it doesn't show it anywhere.

How can I force hibernate to show all these errors? Or is it Jersey that needs to be set up?

The method where I toggled a breakpoint:

@POST
@Path("{id}/comments/new")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response handlePostNewComment(@PathParam("id") int taskId, ItemComment newComment) {
    newComment.setItemId(taskId);
    Session s = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = s.beginTransaction();
    **Integer newCommentId = (Integer)s.save(newComment);**
    s.flush();
    tx.commit();
    s.close();
    HashMap res = new HashMap();
    res.put("id", newCommentId);
    return Response.status(201).entity(res).build();
}

Server response to curl:

$ curl -i -X POST -H "Content-type: application/json" --data @comm.json localhost:8000/task/19/comments/new

HTTP/1.1 500 Request failed. Content-Type: text/html;charset=ISO-8859-1 Date: Tue, 27 Oct 2015 20:34:16 GMT Connection: close Content-Length: 1033

Message in console in IntelliJ:

Connected to the target VM, address: '127.0.0.1:57501', transport: 'socket'
task tracker enterprise Edition running
Oct 27, 2015 9:34:12 PM org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [localhost:8000]
Application started.
Stop the application using CTRL+C
Oct 27, 2015 9:34:12 PM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
Oct 27, 2015 9:34:14 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.2.Final}
Oct 27, 2015 9:34:14 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Oct 27, 2015 9:34:14 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Oct 27, 2015 9:34:14 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.0.Final}
Oct 27, 2015 9:34:14 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Oct 27, 2015 9:34:14 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/tracker]
Oct 27, 2015 9:34:14 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=cristian, password=****}
Oct 27, 2015 9:34:14 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Oct 27, 2015 9:34:14 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Oct 27, 2015 9:34:14 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL94Dialect
Oct 27, 2015 9:34:15 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
Oct 27, 2015 9:34:15 PM org.hibernate.type.BasicTypeRegistry register
INFO: HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@43aa6671
Oct 27, 2015 9:34:15 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory

It looks the same on a successful request, except with some SQL echoed from hibernate.

When debugging I finally come across the exception somewhere in the org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher class

java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer

This has happened several times, with different error messages.

What can I do to force these components to show me the error messages, either on stdout or stderr? Or log them somewhere?

解决方案

It's situations like this I just register an ExceptionMapper<Throwable>. These are used to map exceptions to Responses. Sometimes where there is an unhandled exception, Jersey will swallow it, sending the client a sometimes useless 500 Server Error. In these cases the ExceptionMapper will allow use to handle it ourselves, at leasting printing the stacktrace before sending the response

@Provider
public class DebugExceptionMapper implements ExceptionMapper<Throwable> {

    @Overrride
    public Response toResponse(Throwable e) {
        e.printStackTrace();
        return Response.serverError()
           .entity(e.getMessage())
           .build();
    }
}

Also See:

这篇关于HIbernate不会抛出异常或显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆