1. 程式人生 > >hibernate樂觀鎖 StaleObjectStateException

hibernate樂觀鎖 StaleObjectStateException

你是否出現這個

ERROR SqlExceptionHelper Cannot add or update a child row: a foreign key constraint fails (`car_rental`.`zl_license`, CONSTRAINT `FK_Reference_9` FOREIGN KEY (`UserID`) REFERENCES `zl_user` (`UserID`))
ERROR ExceptionMapperStandardImpl HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
	at org.springframework.orm.hibernate5.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:247)
	at org.springframework.orm.hibernate5.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:785)
	at org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:621)
	at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:746)
	at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:714)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:532)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:304)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)

我出現這個錯誤的原因是我資料庫對映之後

    @Column(name="License_id")
	private int license_id;
	@Column(name="UserID")
	private int userID;
	@Column(name="Front_path")
	private String frontPath;
	@Column(name="Back_path")
	private String backPath;
	@Column(name="License_date")
	private String licenseDate;
	@Column(name="License_status")
	private String licenseStatus;

沒有對license_id進行賦值,因為資料庫中license_id設計的是自增,因此沒有對license_id進行賦值,無找到對應的更新資料因此,大家記得對每個資料都進行賦值,再進行資料庫操作試試