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 8464899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:53:20+00:00 2026-06-10T14:53:20+00:00

For almost one entire day I am facing a problem involving JPA/ Eclipselink and

  • 0

For almost one entire day I am facing a problem involving JPA/ Eclipselink and MySql. In my MySQL database I configured two tables in the following way:

Table: Member

memberID int(11) PK AI
desiresID int(11)
email
firstName
lastName
password

Table: MemberDesire

desireID int(11) PK AI

with ForeignKey constraint between Member and MemberDesire
using Member.desireID and MemberDesire.desireID

In Eclipse I let those tables be imported automatically and it recognizes the linking perfectly. Those two classes look as follows:

    @Entity
    public class Member implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int memberID;

private String email;

private String firstName;

private String lastName;

private String password;

//bi-directional many-to-one association to MemberDesire
@ManyToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="desiresID")
private MemberDesire memberDesire;

and:

    @Entity
    @Table(name="MemberDesires")
    public class MemberDesire implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private int desireID;

private int maximumAge;

private int minimumAge;

//bi-directional many-to-one association to Member
@OneToMany(mappedBy="memberDesire")
private List<Member> members;

Now when I’m trying to persist a new Member with an included MemberDesire to the database the following error appears:

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`test`.`member`, CONSTRAINT `desireKey` FOREIGN KEY (`desiresID`) REFERENCES `MemberDesires` (`desireID`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Error Code: 1452
Call: INSERT INTO MEMBER (EMAIL, FIRSTNAME, LASTNAME, PASSWORD, desiresID) VALUES (?, ?, ?, ?, ?,)
    bind => [5 parameters bound]

The persisting itself is done in this way:

EntityManager em = Persistence

                .createEntityManagerFactory("TestProject")
                .createEntityManager();
        EntityTransaction entityTransaction = em.getTransaction();

        entityTransaction.begin();

        member = new Member();
        member.setFirstName(firstName);
        member.setLastName(lastName);
        member.setEmail(email);
        member.setPassword(password);

        MemberDesire desire = new MemberDesire();
        member.setMemberDesire(desire);

        List<Member> members = new ArrayList<Member>();
        members.add(member);
        desire.setMembers(members);

        em.persist(member);

        entityTransaction.commit();

        em.close();

I would very much appreciate any help you could give me!!
Thank you 🙂

Sebastian

  • 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-06-10T14:53:21+00:00Added an answer on June 10, 2026 at 2:53 pm

    MemberDesire is not set to have a primary key generated, so when you call

     MemberDesire desire = new MemberDesire();
     member.setMemberDesire(desire); 
    

    it should result in an invalid MemberDesire entity with a null pk being associated to the member. You can turn EclipseLink logging level to Finest to see the SQL and the parameters that are issued which might help prove the problem. If you are trying to associate an existing MemberDesire to a new Member, you should use the em.find() api or query for the entity so that you can associate the managed instance instead of creating a new object.

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

Sidebar

Related Questions

I'm almost finished with my program, but I have one problem that I can't
We have one of our tables in our database that is starting to be
I am trying to take tables from one Access Database File, add them to
I have a database which has almost 300 tables, I've just come across with
I have been doing PHP stuff for almost one year and I have never
This one is almost a philosophical question: is it bad to access and/or set
I am making an installer in java swing it almost completed only one thing
I'm trying to create a drop down button and its almost working except one
Inserting rows one by one from a 600 record data-set is taking almost an
Hi I'm trying to copy one row from DataTable to another, I looked almost

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.