我正在尝试处理Hibernate异常处理。 我试图坚持到一个表并获得一个运行时异常(预期为故意创建)但我无法从session.save方法中捕获异常。
这是代码
@Override
public void persist(News entity) {
try {
currentSession.save(entity);
}catch(Exception e) {
if(currentTransaction != null) {
currentTransaction.rollback();
}
if(currentSession != null) {
currentSession.close();
}
}
}
这是例外
Hibernate: select next_val as id_val from hibernate_sequence with (updlock, rowlock)
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into News (url, newsid) values (?, ?)
Hibernate: insert into NewsDetail (articleBody, datemodified, datepublished, description, headline, newsid) values (?, ?, ?, ?, ?, ?)
Apr 20, 2018 10:37:17 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 241, SQLState: S0001
Apr 20, 2018 10:37:17 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Conversion failed when converting date and/or time from character string.
Apr 20, 2018 10:37:17 AM org.hibernate.internal.ExceptionMapperStandardImpl mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [org.hibernate.exception.SQLGrammarException: could not execute statement]
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:149)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1442)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:493)
at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3206)
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2412)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:473)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:156)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)
at com. .hibernate.dao.NewsDao.closeCurrentSessionWithTransaction(NewsDao.java:41)
at com.wion.hibernate.service.NewsService.persist(NewsService.java:19)
at com.wion.base.launch.Wion.persist(Wion.java:74)
at com.wion.base.launch.Wion.main(Wion.java:42)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:178)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3018)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3533)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:600)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:474)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1436)
... 12 more
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting date and/or time from character string.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:258)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1535)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:467)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:409)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7151)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2478)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:219)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:199)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:356)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175)
... 20 more
因此,线程被挂起,因为回滚调用不会转到DB。
可能是什么原因?
添加配置
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=Test;integratedSecurity=true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
添加实体
package com.wion.database.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import javax.persistence.SecondaryTables;
import javax.persistence.Table;
//import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Entity
@Table(name="News")
@SecondaryTables({
@SecondaryTable(name="NewsClassification",pkJoinColumns= {@PrimaryKeyJoinColumn(name="newsid")}),
@SecondaryTable(name="NewsDetail",pkJoinColumns= {@PrimaryKeyJoinColumn(name="newsid")})
})
//@JsonIgnoreProperties(ignoreUnknown = true)
public class News {
@Id
@GeneratedValue
@Column(name="newsid")
private int newsid;
@Column(name="type",table="NewsClassification")
private String type;
@Column(name="keywords",table="NewsClassification")
private String keywords;
private String url;
@Column(name="headline",table="NewsDetail")
private String headline;
@Column(name="datepublished",table="NewsDetail")
private String datePublished;
@Column(name="datemodified",table="NewsDetail")
private String dateModified;
@Column(name="description",table="NewsDetail")
private String description;
@Column(name="articleBody",table="NewsDetail")
private String articleBody;
public int getNewsid() {
return newsid;
}
public void setNewsid(int newsid) {
this.newsid = newsid;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getKeywords() {
return keywords;
}
public void setKeywords(String keywords) {
this.keywords = keywords;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getHeadline() {
return headline;
}
public void setHeadline(String headline) {
this.headline = headline;
}
public String getDatePublished() {
return datePublished;
}
public void setDatePublished(String datePublished) {
this.datePublished = datePublished;
}
public String getDateModified() {
return dateModified;
}
public void setDateModified(String dateModified) {
this.dateModified = dateModified;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getArticleBody() {
return articleBody;
}
public void setArticleBody(String articleBody) {
this.articleBody = articleBody;
}
@Override
public String toString() {
return "News [newsid=" + newsid + ", type=" + type + ", keywords=" + keywords + ", url=" + url + ", headline="
+ headline + ", datePublished=" + datePublished + ", dateModified=" + dateModified + ", description="
+ description + ", articleBody=" + articleBody + "]";
}
}