请帮帮我。当我尝试在数据库中创建新行时,我收到一些关于外键FOREIGNKEY_CUSTOMER_ID的内容。如果我只是编辑一些行它就可以了。 这是实体:
package entity;
// Generated 01.11.2011 10:03:53 by Hibernate Tools 3.2.1.GA
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* PurchaseOrder generated by hbm2java
*/
@Entity
@Table(name="PURCHASE_ORDER"
,schema="APP"
)
public class PurchaseOrder implements java.io.Serializable {
private int orderNum;
private int customerId;
private int productId;
private Short quantity;
private BigDecimal shippingCost;
private Date salesDate;
private Date shippingDate;
private String freightCompany;
public PurchaseOrder() {
}
public PurchaseOrder(int orderNum, int customerId, int productId) {
this.orderNum = orderNum;
this.customerId = customerId;
this.productId = productId;
}
public PurchaseOrder(int orderNum, int customerId, int productId, Short quantity, BigDecimal shippingCost, Date salesDate, Date shippingDate, String freightCompany) {
this.orderNum = orderNum;
this.customerId = customerId;
this.productId = productId;
this.quantity = quantity;
this.shippingCost = shippingCost;
this.salesDate = salesDate;
this.shippingDate = shippingDate;
this.freightCompany = freightCompany;
}
@Id
@Column(name="ORDER_NUM", unique=true, nullable=false)
public int getOrderNum() {
return this.orderNum;
}
public void setOrderNum(int orderNum) {
this.orderNum = orderNum;
}
@Column(name="CUSTOMER_ID", nullable=false)
public int getCustomerId() {
return this.customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
@Column(name="PRODUCT_ID", nullable=false)
public int getProductId() {
return this.productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
@Column(name="QUANTITY")
public Short getQuantity() {
return this.quantity;
}
public void setQuantity(Short quantity) {
this.quantity = quantity;
}
@Column(name="SHIPPING_COST", precision=12)
public BigDecimal getShippingCost() {
return this.shippingCost;
}
public void setShippingCost(BigDecimal shippingCost) {
this.shippingCost = shippingCost;
}
@Temporal(TemporalType.DATE)
@Column(name="SALES_DATE", length=10)
public Date getSalesDate() {
return this.salesDate;
}
public void setSalesDate(Date salesDate) {
this.salesDate = salesDate;
}
@Temporal(TemporalType.DATE)
@Column(name="SHIPPING_DATE", length=10)
public Date getShippingDate() {
return this.shippingDate;
}
public void setShippingDate(Date shippingDate) {
this.shippingDate = shippingDate;
}
@Column(name="FREIGHT_COMPANY", length=30)
public String getFreightCompany() {
return this.freightCompany;
}
public void setFreightCompany(String freightCompany) {
this.freightCompany = freightCompany;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 01.11.2011 10:04:03 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="entity.PurchaseOrder" table="PURCHASE_ORDER" schema="APP">
<id name="orderNum" type="int">
<column name="ORDER_NUM" />
<generator class="increment" />
</id>
<property name="customerId" type="int">
<column name="CUSTOMER_ID" not-null="true" />
</property>
<property name="productId" type="int">
<column name="PRODUCT_ID" not-null="true" />
</property>
<property name="quantity" type="java.lang.Short">
<column name="QUANTITY" />
</property>
<property name="shippingCost" type="big_decimal">
<column name="SHIPPING_COST" precision="12" />
</property>
<property name="salesDate" type="date">
<column name="SALES_DATE" length="10" />
</property>
<property name="shippingDate" type="date">
<column name="SHIPPING_DATE" length="10" />
</property>
<property name="freightCompany" type="string">
<column name="FREIGHT_COMPANY" length="30" />
</property>
</class>
</hibernate-mapping>
我的代码:
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tr = session.beginTransaction();
PurchaseOrder o = new PurchaseOrder();
o.setFreightCompany("Companytest");
session.save(o);
tr.commit();
我收到的错误:
Hibernate: select max(ORDER_NUM) from PURCHASE_ORDER
Hibernate: insert into APP.PURCHASE_ORDER (CUSTOMER_ID, PRODUCT_ID, QUANTITY, SHIPPING_COST, SALES_DATE, SHIPPING_DATE, FREIGHT_COMPANY, ORDER_NUM) values (?, ?, ?, ?, ?, ?, ?, ?)
ноя 01, 2011 10:45:21 AM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: -1, SQLState: 23503
ноя 01, 2011 10:45:21 AM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: INSERT в таблице 'PURCHASE_ORDER' вызывает нарушение ограничения чужого ключа 'FOREIGNKEY_CUSTOMER_ID' для ключа (0). Произведен откат оператора.
ноя 01, 2011 10:45:21 AM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not insert: [entity.PurchaseOrder]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2267)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2660)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:56)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at xml.XmlRead.main(XmlRead.java:69)
Caused by: java.sql.SQLIntegrityConstraintViolationException: INSERT в таблице 'PURCHASE_ORDER' вызывает нарушение ограничения чужого ключа 'FOREIGNKEY_CUSTOMER_ID' для ключа (0). Произведен откат оператора.
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2247)
... 11 more
Caused by: org.apache.derby.client.am.SqlException: INSERT в таблице 'PURCHASE_ORDER' вызывает нарушение ограничения чужого ключа 'FOREIGNKEY_CUSTOMER_ID' для ключа (0). Произведен откат оператора.
at org.apache.derby.client.am.Statement.completeExecute(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.parseEXCSQLSTTreply(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.readExecute(Unknown Source)
at org.apache.derby.client.net.StatementReply.readExecute(Unknown Source)
at org.apache.derby.client.net.NetPreparedStatement.readExecute_(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.readExecute(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.flowExecute(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.executeUpdateX(Unknown Source)
... 14 more
org.hibernate.exception.ConstraintViolationException: could not insert: [entity.PurchaseOrder]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2267)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2660)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:56)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at xml.XmlRead.main(XmlRead.java:69)
Caused by: java.sql.SQLIntegrityConstraintViolationException: INSERT в таблице 'PURCHASE_ORDER' вызывает нарушение ограничения чужого ключа 'FOREIGNKEY_CUSTOMER_ID' для ключа (0). Произведен откат оператора.
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2247)
... 11 more
Caused by: org.apache.derby.client.am.SqlException: INSERT в таблице 'PURCHASE_ORDER' вызывает нарушение ограничения чужого ключа 'FOREIGNKEY_CUSTOMER_ID' для ключа (0). Произведен откат оператора.
at org.apache.derby.client.am.Statement.completeExecute(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.parseEXCSQLSTTreply(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.readExecute(Unknown Source)
at org.apache.derby.client.net.StatementReply.readExecute(Unknown Source)
at org.apache.derby.client.net.NetPreparedStatement.readExecute_(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.readExecute(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.flowExecute(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.executeUpdateX(Unknown Source)
... 14 more
答案 0 :(得分:1)
首先,您的语句失败,因为未设置customerId
属性,但您在相应的数据库行上具有非空约束。尝试提供有效的号码作为customerId。
int customerId = 1;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tr = session.beginTransaction();
PurchaseOrder o = new PurchaseOrder();
o.setFreightCompany("Companytest");
o.setCustomerId(customerId);
session.save(o);
tr.commit();
由于没有可能违反的外键关系,你在误导的字符串“FOREIGNKEY”来自哪里,我不是很害羞,你是否在代码的其他地方配置了某种列前缀?