A表
@Entity
public class a implements Serializable {
private int id;
private String name; private b bb; @JoinColumn(name = "outid", referencedColumnName = "id")
@OneToOne(cascade = CascadeType.ALL)
public b getBb() {
return bb;
} public void setBb(b bb) {
this.bb = bb;
} @Id
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} @Column(name = "Name", length = 50)
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}}B表
@Entity
public class b  implements Serializable{
private int id;
private String name;

private a aa;

@OneToOne(cascade = CascadeType.ALL)   
    @JoinColumn(name = "id",referencedColumnName = "outid")   
public a getA() {
return aa;
}
public void setA(a aa) {
this.aa = aa;
}

@Id
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} @Column(name = "Name", length = 50)
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}测试类 
public class test {
private static aService aservice;
private static bService bservice; @BeforeClass
public static void setUpBeforeClass() throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"beans.xml");
aservice = (aService) applicationContext
.getBean("aservice");
bservice = (bService) applicationContext
.getBean("bservice");
}

@Test
public void testbsvae(){
b x = new b();
x.setId(1);
x.setName("b1");
bservice.saveB(x);

}

@Test
public void testasvae(){
a x = new a();
x.setId(1);
x.setName("a");
b bb = bservice.getB(1);
x.setBb(bb);
aservice.saveA(x);
}
我表格  已经在数据库里生成了 
但@Test
public void testasvae(){
a x = new a();
x.setId(1);
x.setName("a");
b bb = bservice.getB(1);
x.setBb(bb);
aservice.saveA(x);
}
报错
org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:636)
at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:789)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:663)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:732)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:701)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:321)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
at service.aService$$EnhancerByCGLIB$$f6dd86c.saveA(<generated>)
at test.testasvae(test.java:44)
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.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2252)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2688)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:655)
... 28 more
Caused by: java.sql.BatchUpdateException: Duplicate entry '1' for key 1
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:648)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 43 more

解决方案 »

  1.   

    1.因为Hibernate Tools(或者Eclipse本身的Database Explorer)生成*.hbn.xml工具中包含有catalog="***"(*表示数据库名称)这样的属性,将该属性删除就可以了 
    2.估计是你的列名里面有关键字的原因吧,命名列的时候不要单独使用date,ID...这种关键字 Hibernate查询时候的问题。 
    莫名其妙地报如下的错误, 
    org.hibernate.exception.GenericJDBCException: could not execute query 最后把mysql的驱动程序 从 mysql-connector-java-3.2.0-alpha改成mysql-connector-java-5.0.4就可以了。
      

  2.   

    还有可能的原因是:没有把自增的主键设为"increment",你检查一下。