我目前正在开发一个项目,其中包含一个预先存在的数据库,该数据库对我来说是不可变的。当我对数据库进行验证时,我得到一个错误,据我所知,这是错误的。
我有三个相互作用的对象:
@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,因为assessmentQstnNum是一个简短的。有什么我可能会失踪?当我给出columnDefinition =&#34; SMALLINT&#34;时,我对SMALLINT的期望特别困惑。和type =&#34; SMALLINT&#34;
答案 0 :(得分:1)
事实证明,您还必须告诉joincolumn列的deffinition。更改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;
}