“发现:smallint,预期:整数” Hibernate 对 informix 数据库的验证短时间失败

Posted

技术标签:

【中文标题】“发现:smallint,预期:整数” Hibernate 对 informix 数据库的验证短时间失败【英文标题】:"Found: smallint, expected: integer" Hibernate's validation of informix database fails on short 【发布时间】:2015-10-08 17:05:54 【问题描述】:

我目前正在使用一个预先存在的数据库进行一个项目,该数据库对于我的目的而言是不可变的。当我针对数据库进行验证时,我得到一个错误,据我所知,它是不正确的。

我有三个交互的对象:

@Embeddable
public class AssessQstnChoiceId implements java.io.Serializable 
  private short assessQstnNum;
  ...
  ...
  // NOTE: smallint->short
  /*
   * These are my attempted "Fixes"
   * @Column(name = "assess_qstn_num", columnDefinition = "SMALLINT")
   * @Type(type="java.lang.Short")
   */
  @Column(name = "assess_qstn_num")
  public short getAssessQstnNum() 
    return this.assessQstnNum;
  

  public void setAssessQstnNum(short assessQstnNum) 
    this.assessQstnNum = assessQstnNum;
  
  ...

关键是:

@Entity
@Table(name = "assess_qstn_choice")
public class AssessQstnChoice implements java.io.Serializable 
    private AssessQstnChoiceId id;
    private Set<TstQstnRspns> tstQstnRspnses;

    @EmbeddedId
    public AssessQstnChoiceId getId() 
        return this.id;
    

    public void setId(AssessQstnChoiceId id) 
        this.id = id;
    
    ...
    ...
    @OneToMany
    @JoinColumns(
      @JoinColumn(name = "qstn_choice_num",nullable = false),
      @JoinColumn(name = "assess_qstn_num",nullable = false),
      @JoinColumn(name = "tst_sctn_num",nullable = false),
      @JoinColumn(name = "assess_tst_id",nullable = false)
    )
    public Set<TstQstnRspns> getTstQstnRspnses() 
        return this.tstQstnRspnses;
    

    public void setTstQstnRspnses(Set<TstQstnRspns> tstQstnRspnses) 
        this.tstQstnRspnses = tstQstnRspnses;
    

通过@OneToMany 链接到:

@Entity
@Table(name = "tst_qstn_rspns")
public class TstQstnRspns implements java.io.Serializable 
    private AssessQstnChoice assessQstnChoice;
    ...
    ...
    @ManyToOne
    @JoinColumns(
      @JoinColumn(name = "qstn_choice_num", insertable=false, updatable=false),
      @JoinColumn(name = "assess_qstn_num", insertable=false, updatable=false),
      @JoinColumn(name = "tst_sctn_num", insertable=false, updatable=false),
      @JoinColumn(name = "assess_tst_id", insertable=false, updatable=false),
    )
    public AssessQstnChoice getAssessQstnChoice() 
        return this.assessQstnChoice;
    

    public void setAssessQstnChoice(AssessQstnChoice assessQstnChoice) 
        this.assessQstnChoice = assessQstnChoice;
    

我收到以下错误消息:

org.hibernate.HibernateException: Wrong column type in MYDB.USR.tst_qstn_rspns for column assess_qstn_num. Found: smallint, expected: integer
    at org.hibernate.mapping.Table.validateColumns(Table.java:372)
    at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1336)
    at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:155)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:525)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:453)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:438)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:629)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:677)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:548)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:663)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1642)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

据我所知,验证应该期待 SMALLINT,因为assessQstnNum 很短。有什么我可能会丢失的吗?当我给出 columnDefinition="SMALLINT" 和 type = "SMALLINT"

时,我对 SMALLINT 的期望尤其感到困惑

【问题讨论】:

你使用 mysql 作为你的数据库吗? @Ish 不,我正在使用 Informix。 【参考方案1】:

事实证明,您还必须告诉 joincolumn 列定义。更改 AssessQstnChoice 的定义以包含列定义解决了该问题。

@ManyToOne
    @JoinColumns(
      @JoinColumn(name = "qstn_choice_num", insertable=false, updatable=false, columnDefinition = "INT"),
      @JoinColumn(name = "assess_qstn_num", insertable=false, updatable=false, columnDefinition = "SMALLINT"),
      @JoinColumn(name = "tst_sctn_num", insertable=false, updatable=false, columnDefinition = "SMALLINT"),
      @JoinColumn(name = "assess_tst_id", insertable=false, updatable=false, columnDefinition = "INT"),
    )
    public AssessQstnChoice getAssessQstnChoice() 
        return this.assessQstnChoice;
    

【讨论】:

以上是关于“发现:smallint,预期:整数” Hibernate 对 informix 数据库的验证短时间失败的主要内容,如果未能解决你的问题,请参考以下文章

如何解决“引起:java.lang.IllegalStateException:文件被锁定:nio:/Development/hiber/data/contactmgr.mv.db [1.4.192/

初始 SessionFactory 创建 failed.java.lang.NoClassDefFoundError: org/hiber nate/cfg/Configuration

运行第二天出现hibernate的错误,org.hibernate.TransactionException: JDBC commit failed at org.hiber

一对一映射 Hibernate

hibernate简单注释(一.1)

Hibernate- Criteria 简易