Struts Problem Report
Struts has detected an unhandled exception: Messages: Cannot add or update a child row: a foreign key constraint fails (`myshop`.`t_product`, CONSTRAINT `FKA91FC0248049AD0` FOREIGN KEY (`productCategory_id`) REFERENCES `t_product_category` (`id`))
Could not execute JDBC batch update
Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
File: com/mysql/jdbc/ServerPreparedStatement.java
Line number: 648
--------------------------------------------------------------------------------Stacktraces
org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:636)
org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:789)
org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:663)
org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:732)
org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:701)
org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:321)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
$Proxy17.save(Unknown Source)
com.zz.service.impl.ProductServiceImpl.save(ProductServiceImpl.java:243)
com.zz.action.admin.ProductAction.save(ProductAction.java:93)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions
java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`myshop`.`t_product`, CONSTRAINT `FKA91FC0248049AD0` FOREIGN KEY (`productCategory_id`) REFERENCES `t_product_category` (`id`))
com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:648)
org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions( 只贴出了主要部分
Struts has detected an unhandled exception: Messages: Cannot add or update a child row: a foreign key constraint fails (`myshop`.`t_product`, CONSTRAINT `FKA91FC0248049AD0` FOREIGN KEY (`productCategory_id`) REFERENCES `t_product_category` (`id`))
Could not execute JDBC batch update
Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
File: com/mysql/jdbc/ServerPreparedStatement.java
Line number: 648
--------------------------------------------------------------------------------Stacktraces
org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:636)
org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:789)
org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:663)
org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:732)
org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:701)
org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:321)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
$Proxy17.save(Unknown Source)
com.zz.service.impl.ProductServiceImpl.save(ProductServiceImpl.java:243)
com.zz.action.admin.ProductAction.save(ProductAction.java:93)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions
java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`myshop`.`t_product`, CONSTRAINT `FKA91FC0248049AD0` FOREIGN KEY (`productCategory_id`) REFERENCES `t_product_category` (`id`))
com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:648)
org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions( 只贴出了主要部分
--------------------------------------------
package com.zz.entity;import java.util.Set;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;import org.hibernate.annotations.GenericGenerator;/**
* 实体类 - 商品分类
* @author superzh
*
*/
@Entity(name="t_product_category")
public class ProductCategory { private static final long serialVersionUID = -5132652107151648662L; public static final String PATH_SEPARATOR = ",";// 树路径分隔符
private String id;//商品分类号
private String name;// 分类名称
private Integer orderList;// 排序
private String path;// 树路径
private ProductCategory parent;// 上级分类
private Set<ProductCategory> children;// 下级分类
private Set<Product> productSet;// 商品 @Column(nullable = false)
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
@Column(nullable = false)
public Integer getOrderList() {
return orderList;
} public void setOrderList(Integer orderList) {
this.orderList = orderList;
} @Column(nullable = true, length = 10000)
public String getPath() {
return path;
} public void setPath(String path) {
this.path = path;
}
@ManyToOne(fetch = FetchType.LAZY)
public ProductCategory getParent() {
return parent;
} public void setParent(ProductCategory parent) {
this.parent = parent;
} @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
@OrderBy("orderList asc")
public Set<ProductCategory> getChildren() {
return children;
} public void setChildren(Set<ProductCategory> children) {
this.children = children;
}
@OneToMany(mappedBy = "productCategory", fetch = FetchType.LAZY)
public Set<Product> getProductSet() {
return productSet;
} public void setProductSet(Set<Product> productSet) {
this.productSet = productSet;
} // 获取分类层级(顶级分类:0)
public Integer getLevel() {
return path.split(PATH_SEPARATOR).length - 1;
}
@Id
@Column(length = 32, nullable = true)
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid")
public String getId() {
return this.id;
} public void setId(String id) {
this.id = id;
}}
------------------------------------------
package com.zz.entity;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;import org.hibernate.annotations.GenericGenerator;/**
* 实体类 - 商品
*
* @author superzh
*
*/@Entity
@Table(name = "t_product")
public class Product { public static final Integer DEFAULT_PRODUCT_LIST_PAGE_SIZE = 12; public static final int MAX_BEST_PRODUCT_LIST_COUNT = 12; public static final int MAX_HOT_PRODUCT_LIST_COUNT = 12; public static final int MAX_NEW_PRODUCT_LIST_COUNT = 12; private static final long serialVersionUID = 4858058186018438872L; private String description;// 描述
private int freezeStore;// 被占用库存数
private Boolean isHot;// 是否为热销商品
private Boolean isMarketable;// 是否上架
private Boolean isNew;// 是否为新品商品
private Long etPrice;// 市场价格
private String name;// 商品名称
private Long price;// 商品价格
private Long cost; //市场价格
private ProductCategory productCategory;// 商品分类
private String productImageSrc;// 商品图片路径存储
private String id;// 商品ID private Integer store;// 商品库存数量 /*
* private Set<CartItem> cartItemSet;// 购物车项 private Set<OrderItem>
* orderItemSet;// 订单项
*/ @Column(length=100)
public Long getCost() {
return cost;
} @Column(length = 10000)
public String getDescription() {
return description;
} @Column(nullable = false)
public Integer getFreezeStore() {
return freezeStore;
} @Id
@Column(length = 32, nullable = true)
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid")
public String getId() {
return id;
} @Column(nullable = false)
public Boolean getIsHot() {
return isHot;
} @Column(nullable = false)
public Boolean getIsMarketable() {
return isMarketable;
} @Column(nullable = false)
public Boolean getIsNew() {
return isNew;
} /**
* 商品是否缺货
*/
@Transient
public boolean getIsOutOfStock() {
if (store != 0 && freezeStore >= store) {
return true;
} else {
return false;
}
}
@Column(precision = 15, scale = 5, nullable = false)
public Long getMarketPrice() {
return etPrice;
} @Column(nullable = false)
public String getName() {
return name;
} @Column(precision = 15, scale = 5, nullable = false)
public Long getPrice() {
return price;
} @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false)
public ProductCategory getProductCategory() {
return productCategory;
} @Column(length = 10000)
public String getProductImageSrc() {
return productImageSrc;
} @Column(length = 100)
public Integer getStore() {
return store;
} public void setCost(Long cost) {
this.cost = cost;
} public void setDescription(String description) {
this.description = description;
} public void setFreezeStore(int freezeStore) {
this.freezeStore = freezeStore;
} public void setFreezeStore(Integer freezeStore) {
this.freezeStore = freezeStore;
} public void setId(String id) {
this.id = id;
} public void setIsHot(Boolean isHot) {
this.isHot = isHot;
} public void setIsMarketable(Boolean isMarketable) {
this.isMarketable = isMarketable;
} public void setIsNew(Boolean isNew) {
this.isNew = isNew;
}
public void setMarketPrice(Long etPrice) {
this.etPrice = etPrice;
} public void setName(String name) {
this.name = name;
} public void setPrice(Long price) {
this.price = price;
}
public void setProductCategory(ProductCategory productCategory) {
this.productCategory = productCategory;
} public void setProductImageSrc(String productImageSrc) {
this.productImageSrc = productImageSrc;
}
public void setStore(Integer store) {
this.store = store;
} /*
* @OneToMany(fetch = FetchType.LAZY, mappedBy = "product")
*
* @Cascade(value = { CascadeType.DELETE }) public Set<CartItem>
* getCartItemSet() { return cartItemSet; }
*
* public void setCartItemSet(Set<CartItem> cartItemSet) { this.cartItemSet
* = cartItemSet; }
*
* @OneToMany(fetch = FetchType.LAZY, mappedBy = "product") public
* Set<OrderItem> getOrderItemSet() { return orderItemSet; }
*
* public void setOrderItemSet(Set<OrderItem> orderItemSet) {
* this.orderItemSet = orderItemSet; }
*/ }
-------------
Hibernate:
insert
into
t_product
(cost, description, freezeStore, isHot, isMarketable, isNew, etPrice, name, price, productCategory_id, productImageSrc, store, id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)