Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6753839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:12:35+00:00 2026-05-26T13:12:35+00:00

Please help me. When i try to create new row in database i receive

  • 0

Please help me. When i try to create new row in database i receive something about foreign key FOREIGNKEY_CUSTOMER_ID. if i just edit some row it works just fine.
Here’s entity:

    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>

my code:

 Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tr = session.beginTransaction();
            PurchaseOrder o = new PurchaseOrder();
o.setFreightCompany("Companytest");
                    session.save(o);
                    tr.commit();

errors i receive:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:12:36+00:00Added an answer on May 26, 2026 at 1:12 pm

    First, your statement fails because the customerId property is not set, but you have a not-null constraint on the corresponding database row. Try to provide a valid number as 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();
    

    I am not quite shure where the misleading String “FOREIGNKEY” comes from since there is no foreign key relationship present that could be violated, did you configure some sort of column prefix at some other place in the code?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

if you could please help me out... i am try to create o forgot
i am new to perl so need you help alot i try to create
Please help me with using the DrScheme built-in function filter. create a function hello
When I try to create a new project($ rails new first_app) it gives following
I have a problem when i try to create new model field type -
If my question isn't clear, please help me improve it with comments. I'm new
Please help! I'm really at my wits' end. My program is a little personal
Please help! Background info I have a WPF application which accesses a SQL Server
Please help me with a sanity check. Assuming a many-to-many relationship: Post, PostTagAssoc, Tag
Please help, I am stuck here --- irb> a = line of text\n line

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.