使用 hsqldb 的 Hibernate 查询在 Hudson / Jenkins 上失败,在本地工作
我有一个使用 Spring 3 + Hibernate 3.3.2.GA 的相当大的 java 项目。我正在使用 Maven 2 和 JUnit 4,并且 Jenkins 已经成为我们的 CI 服务器几天了(尽管之前是 Hudson,我们也遇到了同样的问题)。
大约一周以来,我遇到了一个奇怪的问题,Maven 构建工作正常并且所有测试都在本地构建中通过,但 CI 服务器报告它不稳定。该项目在 CI 服务器上配置为 Maven 2 项目,因此执行的命令完全相同(全新安装站点)。当向现有实体添加新字段时会出现此问题。所有需要持久保存此类实体实例的测试都会在 Jenkins 上失败,并显示以下消息:
could not insert:> [com.monits.fcoach.model.Challenge];
SQL [insert into challenge (id, name, sortingStrategy, year) values (null,
?, ?, ?)];
nested exception is org.hibernate.exception.DataException:could not insert:[com.monits.fcoach.model.Challenge]
然而,堆栈跟踪才是真正有趣的:
org.springframework.dao.DataIntegrityViolationException: could not insert: [com.monits.fcoach.model.Challenge]; SQL [insert into challenge (id, name, sortingStrategy, year) values (null, ?, ?, ?)]; nested exception is org.hibernate.exception.DataException: could not insert: [com.monits.fcoach.model.Challenge]
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:642)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)
at com.monits.fcoach.dao.GenericHibernateDao.create(GenericHibernateDao.java:101)
at com.monits.fcoach.dao.GenericDAOHibernateTest.setUp(GenericDAOHibernateTest.java:113)
at com.monits.fcoach.dao.ChallengeDaoHibernateTest.setUp(ChallengeDaoHibernateTest.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
Caused by: org.hibernate.exception.DataException: could not insert: [com.monits.fcoach.model.Challenge]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:100)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:64)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2176)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2656)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:563)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:551)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:686)
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:1)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
... 38 more
Caused by: java.sql.SQLException: Column count does not match in statement [insert into challenge (id, name, sortingStrategy, year) values (null, ?, ?, ?)]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:116)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:54)
... 56 more
如上所述,根本原因是:
java.sql.SQLException:列计数 与语句 [插入 进入挑战(id、姓名、 排序策略,年份)值(null, ?,?,?)]
4 个列名,4 个值,但它不匹配......
该 sortingStrategy 是我们在构建开始失败时添加的字段。
该项目在应用程序容器上运行顺利,在 IDE 中一切正常,并在所有开发计算机中使用 Maven,但在 Jenkins / Hudson 上不断失败并显示此消息。对此实体执行插入的所有测试都会以完全相同的方式失败。
课程是这样的:
package com.monits.fcoach.model;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import com.monits.fcoach.sorting.StandardStandingsSorting;
@Entity
@Table(name = "challenge", uniqueConstraints={
@UniqueConstraint(columnNames={"name", "year"})
})
public class Challenge {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private int year;
// This is the field we added when the build started failing
@Column(nullable = true)
private String sortingStrategy;
@ManyToMany
private Set<League> leagues;
@OneToMany
@JoinColumn(name="challenge_id", insertable=false, updatable=false)
private Set<Match> matches;
...
}
欢迎任何想法,我几乎迷失了......
I have a quite big project in java using Spring 3 + Hibernate 3.3.2.GA. I'm using Maven 2 and JUnit 4, and Jenkins has been our CI server for a couple days now (though it was Hudson before and we had the same issue).
Since a week or so, I have a weird issue where a Maven build works and all tests pass in local builds, but the CI server reports it's unstable. The project is configured as a Maven 2 project on the CI server, so the command being executed is the exact same (clean install site). The issue arose when a new field was added to an existing entity. All tests requiring to persist an instance of such entity fail on Jenkins with the following message:
could not insert:> [com.monits.fcoach.model.Challenge];
SQL [insert into challenge (id, name, sortingStrategy, year) values (null,
?, ?, ?)];
nested exception is org.hibernate.exception.DataException:could not insert:[com.monits.fcoach.model.Challenge]
The stacktrace however, is what's really interesting:
org.springframework.dao.DataIntegrityViolationException: could not insert: [com.monits.fcoach.model.Challenge]; SQL [insert into challenge (id, name, sortingStrategy, year) values (null, ?, ?, ?)]; nested exception is org.hibernate.exception.DataException: could not insert: [com.monits.fcoach.model.Challenge]
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:642)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)
at com.monits.fcoach.dao.GenericHibernateDao.create(GenericHibernateDao.java:101)
at com.monits.fcoach.dao.GenericDAOHibernateTest.setUp(GenericDAOHibernateTest.java:113)
at com.monits.fcoach.dao.ChallengeDaoHibernateTest.setUp(ChallengeDaoHibernateTest.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
Caused by: org.hibernate.exception.DataException: could not insert: [com.monits.fcoach.model.Challenge]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:100)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:64)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2176)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2656)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:563)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:551)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:686)
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:1)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
... 38 more
Caused by: java.sql.SQLException: Column count does not match in statement [insert into challenge (id, name, sortingStrategy, year) values (null, ?, ?, ?)]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:116)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:54)
... 56 more
As stated above, the root cause is:
java.sql.SQLException: Column count
does not match in statement [insert
into challenge (id, name,
sortingStrategy, year) values (null,
?, ?, ?)]
4 columns names, 4 values, yet it doesn't match....
That sortingStrategy is the field we added when the build started failing.
The project runs smoothly on the application container, everything works fine from the IDE and using Maven in all dev machines, but it fails constantly on Jenkins / Hudson with this message. All tests performing an insert for this entity fail in the exact same way.
The class is this:
package com.monits.fcoach.model;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import com.monits.fcoach.sorting.StandardStandingsSorting;
@Entity
@Table(name = "challenge", uniqueConstraints={
@UniqueConstraint(columnNames={"name", "year"})
})
public class Challenge {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private int year;
// This is the field we added when the build started failing
@Column(nullable = true)
private String sortingStrategy;
@ManyToMany
private Set<League> leagues;
@OneToMany
@JoinColumn(name="challenge_id", insertable=false, updatable=false)
private Set<Match> matches;
...
}
Any ideas are welcome, I'm pretty much lost...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会尝试将它与在命令行上调用 Maven 的自由风格项目一起使用。有时,M2 插件中的嵌入式 Maven 版本会出现奇怪的问题。无论如何,这通常是构建 Maven 项目的首选方法。
I would try using it with a free style project that call Maven on the command line. Sometimes the embedded Maven version in the M2 plugin has weird issues. That is generally the preferred way to build a Maven project anyway.
正如 TheStijn 在评论中指出的那样(hw 不会回复,所以我可以正确地将他的答案标记为正确),解决方案是删除 hsql 数据库文件并让 jenkins 从头开始创建它。显然,HSQL 上的更新有时会失败,自从这篇原始文章以来,这种情况已经在我身上发生过两次,但将其切换为在我的测试脚本上创建或使用内存中的 HSQL 数据库工作正常。
As pointed out by TheStijn in the comments (hw wouldn't reply so I can properly mark his answer as correct), the solution was to delete the hsql database file and let jenkins create it from scratch. Apparently the update sometimes fails on HSQL, this has happened to me twice already since this original post, but switching it to create on my test script or using in-memory HSQL databases works properly.
我们有类似的詹金斯测试失败,而我们的本地开发测试成功。
我们发现 jenkins box 已更新,帐户/名称已更改。
我们的应用程序在持久性(插入和更新)中使用系统 user.name,该系统在本地工作,但 jenkins 的用户名较长,而我们的表长度较短,因此我们在 jenkins 中进行的 Maven 测试失败了。已经整理好了...
We had a similar jenkins' tests fail while our local dev tests were successful.
We discovered that the jenkins box was updated and the account/ name was changed.
Our app was using the system user.name in our persistence (insert and update) which worked locally but jenkins' username was longer and our table had a shorter length, hence the maven tests we had in jenkins were failing. Got that sorted...