JPA and Foreign key constraint

Discussion in 'Java' started by carmelo, Oct 27, 2009.

  1. carmelo

    carmelo Guest

    Hi everybody,
    I'm developing a Swing CRUD application with a master/details form,
    using Toplink JPA, MySQL and Netbeans 6.7.1. Master/details tables
    are:

    - master(code_auto_incr, master_code)

    - details(code_auto_incr, description, master_code)

    There is a foreign key constraint which relates details.master_code
    to master.master_code

    The problem is that I get an exception on insert:

    javax.persistence.RollbackException: Exception [TOPLINK-4002] (Oracle
    TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))):
    oracle.toplink.essentials.exceptions.DatabaseException

    Internal Exception:
    com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
    Cannot add or update a child row: a foreign key constraint fails
    (`my_database/details`, CONSTRAINT `FK_details` FOREIGN KEY
    (`master_code `) REFERENCES `master` (`master_code `))

    Error Code: 1452

    Call: INSERT INTO my_database.details(code_auto_incr, description,
    master_code) VALUES (?, ?, ?)
    bind => [1, 1, 0]

    Query: InsertObjectQuery(master.Details
    Code:
    )
    
    
    
    How can I do? Please help me.
    
    Thank you very much in advance for your help
     
    carmelo, Oct 27, 2009
    #1
    1. Advertising

  2. carmelo

    Lew Guest

    carmelo wrote:
    > I'm developing a Swing CRUD application with a master/details form,
    > using Toplink JPA, MySQL and Netbeans 6.7.1. Master/details tables
    > are:
    >
    > - master(code_auto_incr, master_code)


    You don't say but we assume that these tables are InnoDB type and that
    'master'.'master_code' has a UNIQUE constraint. If not, fix that.

    > - details(code_auto_incr, description, master_code)
    >
    > There is a foreign key constraint which relates details.master_code
    > to  master.master_code
    >
    > The problem is that I get an exception on insert:
    >
    > javax.persistence.RollbackException: Exception [TOPLINK-4002] (Oracle
    > TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))):
    > oracle.toplink.essentials.exceptions.DatabaseException
    >
    > Internal Exception:
    > com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
    > Cannot add or update a child row: a foreign key constraint fails
    > (`my_database/details`, CONSTRAINT `FK_details` FOREIGN KEY
    > (`master_code `) REFERENCES `master` (`master_code `))
    >
    > Error Code: 1452
    >
    > Call: INSERT INTO my_database.details(code_auto_incr, description,
    > master_code) VALUES (?, ?,  ?)
    >         bind => [1, 1, 0]
    >
    > Query: InsertObjectQuery(master.Details
    Code:
    )
    >
    > How can I do? Please help me.
    >
    > Thank you very much in advance for your help[/color]
    
    Since you only give some of the relevant information, we can only give
    at best some of the answer.
    
    You are attempting to insert a record into the 'details' table with a
    'master_code'.that does not exist in the 'master' table.
    
    How in the world did you come to attempt that?
    
    --
    Lew
     
    Lew, Oct 27, 2009
    #2
    1. Advertising

  3. carmelo a écrit :
    > Hi everybody,
    > I'm developing a Swing CRUD application with a master/details form,
    > using Toplink JPA, MySQL and Netbeans 6.7.1. Master/details tables
    > are:
    >
    > - master(code_auto_incr, master_code)
    >
    > - details(code_auto_incr, description, master_code)
    >
    > There is a foreign key constraint which relates details.master_code
    > to master.master_code
    >
    > The problem is that I get an exception on insert:
    >
    > javax.persistence.RollbackException: Exception [TOPLINK-4002] (Oracle
    > TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))):
    > oracle.toplink.essentials.exceptions.DatabaseException
    >
    > Internal Exception:
    > com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
    > Cannot add or update a child row: a foreign key constraint fails
    > (`my_database/details`, CONSTRAINT `FK_details` FOREIGN KEY
    > (`master_code `) REFERENCES `master` (`master_code `))
    >
    > Error Code: 1452
    >
    > Call: INSERT INTO my_database.details(code_auto_incr, description,
    > master_code) VALUES (?, ?, ?)
    > bind => [1, 1, 0]
    >
    > Query: InsertObjectQuery(master.Details
    Code:
    )
    > 
    > 
    > 
    > How can I do? Please help me.
    > 
    > Thank you very much in advance for your help[/color]
    
    You don't tell us enough to diagnose the problem. I've had the same kind 
    of problem using Hibernate, if the many-to-one association is not marked 
    optional=false or the join column is not marked nullable=false(I don't 
    remember which).
    In this case Hibernate tried to insert the two records, and then update 
    the details one with the appropriate value for the foreign key. Maybe 
    TopLink does the same.
    
    JB.
     
    Jean-Baptiste Nizet, Oct 27, 2009
    #3
  4. carmelo

    carmelo Guest

    Thank you guys for your answers.

    I'll try to give you more details...

    My MySQL database is of InnoDB type.

    'master'.'master_code' has a UNIQUE constraint.

    - on details:
    @JoinColumn(name = "master_code", referencedColumnName =
    "master_code", nullable = false)
    @ManyToOne(optional = false)

    - on master:
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "master_code")


    I hope you can help me now. This is quite a while I'm trying to solve
    it...

    Thank you very much again
     
    carmelo, Oct 28, 2009
    #4
  5. carmelo

    Lew Guest

    carmelo wrote:
    > Thank you guys for your answers.
    >
    > I'll try to give you more details...
    >
    > My MySQL database is of InnoDB type.
    >
    > 'master'.'master_code' has a UNIQUE constraint.
    >
    > - on details:
    > @JoinColumn(name = "master_code", referencedColumnName =
    > "master_code", nullable = false)
    > @ManyToOne(optional = false)
    >
    > - on master:
    > @OneToMany(cascade = CascadeType.ALL, mappedBy = "master_code")


    Are you able to construct and post an SSCCE
    <http://sscce.org/>
    that demonstrates your difficulty?

    --
    Lew
     
    Lew, Oct 28, 2009
    #5
  6. carmelo

    SergeyT Guest

    As others already told you (but in other words) you are trying to save
    a detail entity without prior saving the master one. For example, you
    created a master entity object, but didn't call manager.persist() on
    it, and then tried to persist the detail entity.
     
    SergeyT, Oct 29, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rajesh Tiwari

    Can Not Access Foreign Key Constraint

    Rajesh Tiwari, Jun 30, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    678
    Rajesh Tiwari
    Jun 30, 2003
  2. Andy
    Replies:
    0
    Views:
    1,488
  3. H5N1
    Replies:
    0
    Views:
    472
  4. puvit82
    Replies:
    4
    Views:
    780
    puvit82
    Feb 1, 2008
  5. Lionel
    Replies:
    9
    Views:
    3,514
Loading...

Share This Page