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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:36:48+00:00 2026-06-03T19:36:48+00:00

While developing my Java EE application I’ve encountered two situations in which my entities

  • 0

While developing my Java EE application I’ve encountered two situations in which my entities were prematurely created in the DB; They were inserted before their referenced rows were inserted, resulting in a violation of a foreign key.

At first I thought I got it wrong, creating the rows in the DB in the wrong order.
But no, this was not the case. Then I started thinking there are bugs in my JPA provider*, or maybe they just got it wrong.
But then I realised that the EntityManager wasn’t aware of the connection between the tables.

Let me explain:
In both situtation table A references table B using a foreign key.

Situtation #1:

My code:
 1. Creates an entity of table B.
 2. Creates a row of table A using JDBC (there's no entity for table A).  
JPA provider:
 1. Runs the JDBC first (inserts a row into table A).
 2. SQLException is thrown, because B isn't created yet.

Why use JDBC?
Because it’s not my code.

Situtation #2:

My code:
 1. Creates an entity of table B.
 2. Creates an entity of table A. The thing is, A only holds B's ID.
JPA provider:
 1. Creates entity A.
 2. SQLException is thrown, because B isn't created yet.

Why B doesn’t have A as a member?
Because B has a big blob in it, and it takes a lot of memory, and I want to minimize references to B objects.

I need a solution. Maybe there’s someway to tell the EntityManager to run the SQL exactly in the same order I invoked it.
While in the toilets I also thought about using ‘flush’ after creating entity B, but I can’t test it now because our developement DB is down for maintenance (DBA working their magic).

* I’m using TopLink which comes with Oracle WebLogic 12.1.1.0.

  • 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-03T19:36:49+00:00Added an answer on June 3, 2026 at 7:36 pm

    Good way to solve is to have relationship between A and B. What can be done to make that more efficient is:

    • Make relationship from A to B lazy (FetchType.LAZY)
    • Make blob field in B lazy
    • When only reference to B is needed, use EntityManager.getReference.

    If because of some reason that cannot be done, then you should flush entitymanager after each operation that is required to be done before later operations.

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

Sidebar

Related Questions

I'm developing a Java Swing application which contains a JTable . By default, while
Am Beginner to java.I have a small doubt while i am developing an application
While developing a web application in JSF, I came across an expression which was
Hai I am developing a standalone application using Java in which I am using
While developing a web based java application I am a bit confused about the
I was working on a project which includes developing an application using java sockets.
I mean to say while developing application in eclipse. If i write java program
I am developing a stand-alone java application which uses eclipselink. It is all fine
Which is the best base java IDE (eclipse, Netbean) for developing spring based application
I am developing an application for WebSphere 6.1 which uses a Java servlet. Within

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.