最糟糕的是,我手動添加了10條記錄到我的數據庫中。然後我運行該項目,它出現這樣的錯誤:重複鍵'1'爲'PRIMARY'鍵。然後我刷新頁面九次,它顯示從「複製條目'2'鍵'PRIMARY'」到「複製條目'10'鍵'PRIMARY」。最後,該項目變得正常了,這意味着我可以從11條記錄中將項目插入到數據庫中。怎麼了?com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:重複條目'5'鍵'PRIMARY'
我在verison 5.10 final中使用hibernate。我將在下面顯示我的代碼塊:
我的困惑是項目創建的表沒有爲auto_increatement設置。如下: pictures this is a picture
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="packagesToScan" value="com"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.implicit_naming_strategy">legacy-jpa</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
</props>
</property>
<property name="sharedCacheMode" value="ENABLE_SELECTIVE"/>
</bean>
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import java.util.Date;
@Table(name = "sssp_empoyee")
@Entity
public class Employee {
private Integer id;
private String lastName;
private String email;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birth;
private Date crateTime;
private Department department;
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "increment")
@Id
public Integer getId() {
return id;
}
@Transactional
public void saveEmployee(Employee employee) {
employee.setCrateTime(new Date());
respository.saveAndFlush(employee);
}
錯誤信息:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException : Duplicate entry '5' for key 'PRIMARY'
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
sun.reflect.DelegatingConstructorAccessorImpl.__newInstance(DelegatingConstructorAccessorImpl.java:45)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java)
java.lang.reflect.Constructor.newInstance(Constructor.java:423)
com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
com.mysql.jdbc.Util.getInstance(Util.java:381)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2046)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1964)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1949)
com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:147)
org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
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.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1300)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.__invoke(DelegatingMethodAccessorImpl.java:43)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344)
com.sun.proxy.$Proxy35.flush(Unknown Source)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.__invoke(DelegatingMethodAccessorImpl.java:43)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java)
java.lang.reflect.Method.invoke(Method.java:498)
我看你的身份證生成器總是從0開始重新啓動你的項目,可以嗎? –
我手動添加到數據庫中的原始數據從1開始。似乎該項目有其自己的ID,但不是根據數據庫的ID。我已經設置了ID自動治療。我不知道如何解決這個問題。 – JSO