com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:重复条目' 2'关键' PRIMARY'在测试过程中

时间:2016-08-10 20:16:45

标签: 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");
    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);

}

实体类如下

CinemaHall.java

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<>();
}

Movie.Java

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<>();
}

Cast.Java

public class Cast{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String firstName;
    private String lastName;
    @Enumerated(value = EnumType.STRING)
    private Type type;
}

当我运行上述测试时,我收到以下错误。

  

org.springframework.dao.DataIntegrityViolationException:无法   执行声明; SQL [不适用];约束[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.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:485)     在   org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)     在   org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)     在   org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)     在   com.movi​​e.ticketing.dao.MovieDAO $$ EnhancerBySpringCGLIB $$ d00001d8.saveOrUpdate()     在com.movi​​e.ticketing.dao.TestApp.test(TestApp.java:52)at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47)     在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在   org.junit.runners.model.FrameworkMethod.invokeExplosively(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)at   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)at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)at at   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)at   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(AbstractFlushingEventListener.java:337)     在   org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)     在org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1295)     在   org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:468)     在   org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3135)     在   org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2352)     在   org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)     在   org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147)     在   org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access $ 100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)     在   org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl $ TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)     在   org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)     在   org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:581)     ... 38更多引起:   com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:   重复输入&#39; 2&#39;关键&#39; PRIMARY&#39;在   sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)     at sun.reflect.NativeConstructorAccessorImpl.newInstance(未知   来源)at   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知   来自java.lang.reflect.Constructor.newInstance(未知来源)     在com.mysql.jdbc.Util.handleNewInstance(Util.java:404)at   com.mysql.jdbc.Util.getInstance(Util.java:387)at   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:932)at at   com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)at at   com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)at at   com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)at at   com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)at at   com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)at   com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)     在   com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)     在   com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)     在   com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5094)     在   com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)     在   org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)     在   org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)     在   org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)     ......还有56个

0 个答案:

没有答案