1

我正在使用带有 hilo 密钥生成器和 Postgres 9.3 的休眠 3。表列是 BIGINT,模型类变量是 long(java)。

从后端没有问题,但是当我尝试从前端控制台保存时显示 Bad Integer '2147483649' 错误。表架构是:

CREATE TABLE user_log
(
  id bigint NOT NULL,
  appuser_id bigint,
  user_role_id bigint,
  login_time timestamp without time zone,
  logout_time timestamp without time zone,
  logged_in_ip character varying(32),
  CONSTRAINT user_log_pkey PRIMARY KEY (id),
  CONSTRAINT fk_app_user FOREIGN KEY (appuser_id)
      REFERENCES app_user (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_user_role FOREIGN KEY (user_role_id)
      REFERENCES user_role (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

型号类:

public class UserLog extends ModelBase {

/** identifier field */
private Long id;

/** persistent field */
private AppUser user;

/** nullable persistent field */
private Date loginTime;

/** nullable persistent field */
private Date logoutTime;

/** nullable persistent field */
private UserRole loggedInRole;

/** nullable persistent field */
private String loggedInIP;

/**
 * @hibernate.id generator-class="hilo"  column="id"
 * 
 */
public Long getId() {
    return this.id;
}

public void setId(Long id) {
    this.id = id;
}

/**
 * @hibernate.many-to-one
 * @hibernate.column name="user_role_id"
 * 
 */
public UserRole getLoggedInRole() {
    return loggedInRole;
}

public void setLoggedInRole(UserRole loggedInRole) {
    this.loggedInRole = loggedInRole;
}

/**
 * @hibernate.property column="login_time" type="java.util.Date"
 * 
 */
public Date getLoginTime() {
    return loginTime;
}

public void setLoginTime(Date loginTime) {
    this.loginTime = loginTime;
}

/**
 * @hibernate.property column="logout_time" type="java.util.Date"
 * 
 */
public Date getLogoutTime() {
    return logoutTime;
}

public void setLogoutTime(Date logoutTime) {
    this.logoutTime = logoutTime;
}

/**
 * @hibernate.many-to-one
 * @hibernate.column name="appuser_id"
 * 
 */
public AppUser getUser() {
    return user;
}

public void setUser(AppUser user) {
    this.user = user;
}

/**
 * @hibernate.property column="logged_in_IP" length="32"
 * 
 */
public String getLoggedInIP() {
    return loggedInIP;
}

public void setLoggedInIP(String loggedInIP) {
    this.loggedInIP = loggedInIP;
}

}

错误堆栈跟踪:

 org.postgresql.util.PSQLException: Bad Integer 2147483649
    at org.postgresql.jdbc1.AbstractJdbc1ResultSet.toInt(AbstractJdbc1ResultSet.java:857)
    at org.postgresql.jdbc1.AbstractJdbc1ResultSet.getInt(AbstractJdbc1ResultSet.java:282)
    at org.apache.commons.dbcp.DelegatingResultSet.getInt(DelegatingResultSet.java:186)
    at org.hibernate.id.TableGenerator.doWorkInCurrentTransaction(TableGenerator.java:140)
    at org.hibernate.engine.TransactionHelper$1Work.doWork(TransactionHelper.java:38)
    at org.hibernate.engine.transaction.Isolater$JdbcDelegate.delegateWork(Isolater.java:187)
    at org.hibernate.engine.transaction.Isolater.doIsolatedWork(Isolater.java:43)
    at org.hibernate.engine.TransactionHelper.doWorkInNewTransaction(TransactionHelper.java:51)
    at org.hibernate.id.TableGenerator.generate(TableGenerator.java:94)
    at org.hibernate.id.TableHiLoGenerator.generate(TableHiLoGenerator.java:62)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:99)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
    at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:541)
    at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:312)
    at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:538)
    at com.dimensionsgroup.campus.core.dao.impl.CampusDaoSupport.create(CampusDaoSupport.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:288)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:155)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
    at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:51)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
    at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:51)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
    at $Proxy9.create(Unknown Source)
    at com.dimensionsgroup.campus.common.action.LoginAction.go(LoginAction.java:137)
    at com.dimensionsgroup.campus.common.action.AbstractAction.execute(AbstractAction.java:113)
    at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:168)
    at com.opensymphony.xwork.interceptor.DefaultWorkflowInterceptor.intercept(DefaultWorkflowInterceptor.java:55)
    at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:165)
    at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:35)
    at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:165)
    at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:35)
    at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:165)
    at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:35)
    at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:165)
    at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:35)
    at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:165)
    at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:35)
    at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:165)
    at com.opensymphony.xwork.DefaultActionProxy.execute(DefaultActionProxy.java:115)
    at com.opensymphony.webwork.dispatcher.ServletDispatcher.serviceAction(ServletDispatcher.java:272)
    at com.opensymphony.webwork.dispatcher.ServletDispatcher.service(ServletDispatcher.java:237)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:879)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
    at java.lang.Thread.run(Unknown Source)
4

2 回答 2

0

将休眠版本 3.0 更改为 3.2 后问题解决。

于 2017-09-28T08:22:41.137 回答
0

当 Hibernate 尝试为user_log表创建新 id 时,会发生错误。

在您的应用程序中,Hibernateorg.hibernate.id.TableGenerator用来生成 Id,而 Ids 又想要返回一个整数。这会导致异常,因为下一个可用 id 大于Integer.MAX_VALUE.

似乎您使用的是 < 3.5 的 Hibernate 版本,因为Javadoc说:

同样,此处支持的返回类型是 IntegralDataTypeHolder 支持的任何类型。这是 3.5 的新功能。在此之前,此生成器仅返回整数值。

因此,即使您在表和 POJO 中使用了正确的数据类型,TableGenerator 也只能返回整数,除非您更新到更新的 Hibernate 版本。

于 2015-09-11T07:31:39.087 回答