大侠们帮忙看看这个是什么问题关于外键约束的Cannot add or update a child row: a foreign key constraint

java522123428 2011-10-27 01:38:16
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(

只贴出了主要部分
...全文
1663 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
AoYuanSE 2011-10-27
  • 打赏
  • 举报
回复
项目里尽量不要使用外键 很容易引起不必要的麻烦 用id关联就可以
java522123428 2011-10-27
  • 打赏
  • 举报
回复
谢谢各位了。确实是,我外键引用没建好引起的。
java522123428 2011-10-27
  • 打赏
  • 举报
回复
主要操作就是添加一个商品信息。。这个商品信息里面涉及到了一个productcategory(商品分类)代码都贴出来了啊
xiaoqiuwyh 2011-10-27
  • 打赏
  • 举报
回复
应该是在主键表里没有这个数据造成的吧。
java522123428 2011-10-27
  • 打赏
  • 举报
回复
这句出错了。hibernate生成的sql
-------------
Hibernate:
insert
into
t_product
(cost, description, freezeStore, isHot, isMarketable, isNew, marketPrice, name, price, productCategory_id, productImageSrc, store, id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
java522123428 2011-10-27
  • 打赏
  • 举报
回复
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`))
java522123428 2011-10-27
  • 打赏
  • 举报
回复
我用的SSH2框架弄的。表都没数据啊。怎么会这样啊
qq827010202 2011-10-27
  • 打赏
  • 举报
回复
有一种可能就是,你删除一个对象,这个对象刚好被别的对象关联啦。删除不了。
qq827010202 2011-10-27
  • 打赏
  • 举报
回复
具体的操作
qq827010202 2011-10-27
  • 打赏
  • 举报
回复
你贴的代码也忒多了吧。你是做什么操作?
leilei0932_java 2011-10-27
  • 打赏
  • 举报
回复
很明显是主外键关系出了问题,有可能你在外键表的外键列中插入了一个值,但这个值却在主键表的主键列中不存在,这样的话由于主外键关系你的操作会被拒绝的
java522123428 2011-10-27
  • 打赏
  • 举报
回复
。。。在线等锕。。。
yxy353245805 2011-10-27
  • 打赏
  • 举报
回复
天啊,,看的好头痛。
java522123428 2011-10-27
  • 打赏
  • 举报
回复
product.java


------------------------------------------
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 marketPrice;// 市场价格
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 marketPrice;
}

@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 marketPrice) {
this.marketPrice = marketPrice;
}

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; }
*/



}
java522123428 2011-10-27
  • 打赏
  • 举报
回复
productcategory.java


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

}

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧