Java com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:重复条目';2';对于键';初级';测试期间
嗨,我有这个测试类,它将3个实体类保存到数据库中。 第一次保存时也会将强制转换实体存储两次。Java com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:重复条目';2';对于键';初级';测试期间,java,mysql,spring,hibernate,jdbc,Java,Mysql,Spring,Hibernate,Jdbc,嗨,我有这个测试类,它将3个实体类保存到数据库中。 第一次保存时也会将强制转换实体存储两次。 @Test // @Transactional public void test() throws ParseException { //Creating a movie Movie movie1 = new Movie(); movie1.setMovieName("Movie One"); movie1.setPlot("Sample Plot1");
@Test
// @Transactional
public void test() throws ParseException {
//Creating a movie
Movie movie1 = new Movie();
movie1.setMovieName("Movie One");
movie1.setPlot("Sample Plot1");
movie1.setReleaseDate(new Date(DateUtils.dateToMilliSecondsConverter(20160101L)));
//Creating cast
Cast cast1 = new Cast("male", "lead", Type.MALE_LEAD);
Cast cast2 = new Cast("female", "lead", Type.FEMALE_LEAD);
List<Cast> cast = new ArrayList<>();
cast.add(cast1);
cast.add(cast2);
//adding cast to movie
movie1.setCast(cast);
movie1.setRuntime(120);
movieDAO.saveOrUpdate(
//Creating a movie
Movie movie2 = new Movie();
movie2.setMovieName("Movie Two");
movie2.setPlot("Sample Plot2");
movie2.setReleaseDate(new Date(DateUtils.dateToMilliSecondsConverter(20160101L)));
movie2.setRuntime(120);
movieDAO.saveOrUpdate(movie2);
CinemaHall ch = new CinemaHall();
ch.setName("Odeon");
ch.setMovie(new ArrayList<Movie>() {
{
add(movie1);
add(movie2);
}
});
cinemaHallDAO.saveOrUpdate(ch);
当我运行上面提到的测试时,我得到以下错误
org.springframework.dao.DataIntegrityViolationException:无法
执行语句;SQL[n/a];约束[null];嵌套异常是
org.hibernate.exception.ConstraintViolationException:无法
在处执行语句
org.springframework.orm.hibernate5.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:164)
在
org.springframework.orm.hibernate5.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:741)
在
org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:589)
在
org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
在
org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
在
org.springframework.transaction.interceptor.TransactionSpectSupport.commitTransactionAfterReturning(TransactionSpectSupport.java:485)
在
org.springframework.transaction.interceptor.TransactionSpectSupport.invokeWithinTransaction(TransactionSpectSupport.java:291)
在
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
在
org.springframework.aop.framework.ReflectiveMethodInvocation.procement(ReflectiveMethodInvocation.java:179)
在
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
在
com.movie.ticketing.dao.MovieDAO$$EnhancerBySpringCGLIB$$d0001d8.saveOrUpdate()
在com.movie.ticketing.dao.TestApp.test(TestApp.java:52)上
sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于
位于的sun.reflect.NativeMethodAccessorImpl.invoke(未知源)
sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)位于
java.lang.reflect.Method.invoke(未知源代码)位于
org.junit.runners.model.FrameworkMethod$1.runReflectVeCall(FrameworkMethod.java:47)
在
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
在
org.junit.runners.model.FrameworkMethod.invokeeexplosive(FrameworkMethod.java:44)
在
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
在
org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
在
org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
在
org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
位于org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
在
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
位于org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)位于
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)位于
org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)位于
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)位于
org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
在
org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
位于org.junit.runners.ParentRunner.run(ParentRunner.java:309)
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
在
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
在
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
在
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
在
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:670)
在
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
在
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
原因:org.hibernate.exception.ConstraintViolationException:可能
不在执行语句
org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59)
在
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
在
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
在
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
在
org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
在
org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)
在
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2921)
在
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3421)
在
org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)
在
org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:560)
在
org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:434)
在
org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlu
public class CinemaHall{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
@OneToMany(targetEntity = Movie.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Movie> movie = new ArrayList<>();
}
public class Movie implements Common {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String movieName;
private Date releaseDate;
@OneToMany(targetEntity = Cast.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Cast> Cast = new ArrayList<>();
private String plot;
private Integer runtime;
@ElementCollection(targetClass = String.class)
private List<String> showTimings = new ArrayList<>();
}
public class Cast{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
@Enumerated(value = EnumType.STRING)
private Type type;
}