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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:42:42+00:00 2026-05-31T19:42:42+00:00

I have two entities A and B: A.java: … public class A implements Serializable

  • 0

I have two entities A and B:

A.java:

...
public class A implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "IDA", nullable = false)
private Integer ida;
@Basic(optional = false)
@Column(name = "NAME", nullable = false, length = 30)
private String name;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "a")
private List<B> bList=new ArrayList();

public void addB(B bp){
bp.setA(this);
bList.add(bp);
}
...

B.java:

...
public class B implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected BPK bPK;
@Basic(optional = false)
@Column(name = "NAME", nullable = false, length = 30)
private String name;
@JoinColumn(name = "A_IDA", referencedColumnName = "IDA", nullable = false, insertable = false, updatable = false)
@ManyToOne(optional = false)
private A a;
...

the bPK field is a Composite Primary Key:

@Embeddable
public class BPK implements Serializable {
@Basic(optional = false)
@Column(name = "IDB", nullable = false, length = 20)
private String idb;
@Basic(optional = false)
@Column(name = "A_IDA", nullable = false)
private int aIda;

public BPK() {
}

public BPK(String idb, int aIda) {
    this.idb = idb;
    this.aIda = aIda;
}
...

SQL code:

CREATE TABLE A (
idA INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
PRIMARY KEY(idA)
);

CREATE TABLE B (
idB VARCHAR(20) NOT NULL,
A_idA INTEGER UNSIGNED NOT NULL,
name VARCHAR(30) NOT NULL,
PRIMARY KEY(idB, A_idA),
FOREIGN KEY(A_idA)
REFERENCES A(idA)
  ON DELETE NO ACTION
  ON UPDATE NO ACTION
);

the main code:

   A a=new A(null,"A1");
   BPK bpk=new BPK();
   bpk.setIdb("b1");
   a.addB(new B(bpk,"B1"));

   EntityManager em=getEntityManager();
   em.getTransaction().begin();
   em.persist(a);
   em.getTransaction().commit();

I get this error:

 Exception in thread "main" javax.persistence.RollbackException: Exception  [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504):    org.eclipse.persistence.exceptions.DatabaseException
 [EL Warning]: 2012-03-26 01:29:16.724--UnitOfWork(1902320872)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504):  org.eclipse.persistence.exceptions.DatabaseException
 Internal Exception: org.h2.jdbc.JdbcSQLException: Referential integrity constraint  violation: "CONSTRAINT_42_1: PUBLIC.B FOREIGN KEY(A_IDA) REFERENCES PUBLIC.A(IDA)"; SQL  statement:
 Internal Exception: org.h2.jdbc.JdbcSQLException: Referential integrity constraint   violation: "CONSTRAINT_42_1: PUBLIC.B FOREIGN KEY(A_IDA) REFERENCES PUBLIC.A(IDA)";...

the error indicates a violation of integrity constrains, but why?
a single possibility whether the insertion of the entity B is made before A…
Any help please?
Thank you in advance.

EDIT:
solved just replace this :

 @JoinColumn(name = "A_IDA", referencedColumnName = "IDA", nullable = false, insertable = false, updatable = false)

by this one:

 @MapsId("aIda")

finally B.java:

 @NamedQuery(name = "B.findByName", query = "SELECT b FROM B b WHERE b.name = :name")})
 public class B implements Serializable {
 private static final long serialVersionUID = 1L;
 @EmbeddedId
 protected BPK bPK;
 @Basic(optional = false)
 @Column(name = "NAME", nullable = false, length = 30)
 private String name;
 @MapsId("aIda")
 @ManyToOne(optional = false)
 private A a;
  • 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-31T19:42:43+00:00Added an answer on May 31, 2026 at 7:42 pm

    You have the A_AID field controlled by the aIda attribute in the Embeddable – that means this attribute must be set with the value from A before you can persist B.

    If you are using using JPA 2.0, you can mark the @ManyToOne with the @MapsId(“aIda”) which will allow you to remove the @JoinColumn for it. This will make the JPA provider set the value in b.bPK.aIda with the value from A on persist.

    If you are not using JPA 2,0, you can either set it yourself by first persisting A and then changing your addB method to also set B’s bPK.aIda, or you can change the fields so that the JoinColumn is writable and make the bPK.aIda insertable=false, updatable=false.

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

Sidebar

Related Questions

I have two entities User and Group: @Entity public class User implements Serializable {
A have two entities. For example timing settings and orders. @Entity public class TimingSettings{
I have two entities: public class Parent() { public ICollection<Child> Children { get; set;
Hii, I have two entities with bidirectional association. Project.java class Project{ int project_id; @OneToMany(mappedBy=project)
I have two entities: Event and Comment. @Entity @Table(name = events) public class Event
I have two entities, a Shelf and a Product : public class Shelf {
I have problems creating a custom query. This is what entities: @Entity public class
I have two (or more) Java Threads creating, updating and deleting entities from a
I have two entities which are Student and Class entities. Student and Class are
I have two entities say Customer and Order which exist on their own and

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.