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

  • Home
  • SEARCH
  • 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 6600549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:38:14+00:00 2026-05-25T18:38:14+00:00

In my java web app (using playframework ),I have a Customer having an Address

  • 0

In my java web app(using playframework),I have a Customer having an Address mapped like this

@Entity
class Customer extends Model{
    @ManyToOne
    Address address;
...
}

@Entity
class Address extends Model{
    String addressline1;
   ...
}

I have a setCustomerAddress(..) method which takes user input,r
etrieves matched address from db or creates a new address,and then if customer address is null or different ,sets the input address.

public static void setCustomerAddress(...){
   Customer = Customer.findById(custId);
   Address address = findOrCreateAddress(addressline1,...);
   if ((customer.getAddress()==null) || (!customer.getAddress().equals(address))) {
    customer.setAddress(address);
    customer.save();
}
...
}

customer1 creates an Address1.
customer2 inputs same Address1.

now both have Address1;

id | addressline1 | addressline2 | country 
----+--------------+--------------+---------
 25 |apple st.     |              | US


id  |name          |address_id
----+--------------+------------
1   |jim           |25
2   |roy           |25

if customer2 adds an addressline2,the address is considered different,so a new Address is created

address table

 id | addressline1 | addressline2 | country 
----+--------------+--------------+---------
 25 |apple st.     |              | US
 26 |apple st.     |richman's end | US

customer table

id  |name          |address_id
----+--------------+------------
1   |jim           |25
2   |roy           |26

Suppose,customer1 updates his address by adding the same addressline2 as customer2,Now his address_id points to the address of customer2
customer table

id  |name          |address_id
----+--------------+------------
1   |jim           |26
2   |roy           |26

This leaves an address record(25) which doesn’t belong to any customer.

Should this be left in db ?or should this be removed?Which is the proper practice?ManyToOne doesnot allow for any orphanRemoval I guess.Do I have to clean this up in code ,may be doing some checking for such lone entities?

Any pointers would be welcome

  • 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-25T18:38:15+00:00Added an answer on May 25, 2026 at 6:38 pm

    Orphan removal can be done only for @OneToOne and @OneToMany annotated relationships in JPA 2.0.

    In the case of @ManyToOne, orphan removal is not a valid term, as you are attempting to supposedly deleted a “parent”, when no child entities exist. This is quite a different scenario compared to the child entity being deleted, when it has no parent (i.e. it has been orphaned).

    If you believe that Address records must be deleted in the database, when no Customer records exist, you must write code to delete the Address entity, when no Customer entities refer to it. I’m unaware of the rest of your code, but this looks like a job for a SQL (native) or a JPQL query to fetch all such Addresses.

    Note that, it is impossible to use the orphan deletion feature within JPA 2.0 in this case, due to the reason stated previously. Therefore, creating a bidirectional relationship with a @OneToMany annotated property in your Address entity (having the orphanRemoval attribute set to true) will not result in deletion of unreferenced Address records. You may however, create a bidirectional relationship to avoid using a SQL or JPQL query (as you can then count the number of Customers in the collection property of the Address).

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

Sidebar

Related Questions

I inherited this gigantic legacy Java web app using Struts 1.2.4. I have a
I inherited this gigantic legacy Java web app using Struts 1.2.4. I have a
I have created some simple app in Java, and 'deployed' it using Java Web
I have a Java web-app using standard web.xml and servlets. At the moment, I'm
I have a Java web app using JDBC created in NetBeans deployed to Tomcat
I'm using Maven 2 and Hibernate for a Java web app. I have a
This Java web-app I am using that other developers wrote is structured in the
I have a Java web app that I'm developing, using JBoss Seam as the
I am using Apache Axis to connect my Java app to a web server.
Hello I am a Java web app developer I have a problem, I would

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.