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
Orphan removal can be done only for
@OneToOneand@OneToManyannotated 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
Addressrecords must be deleted in the database, when noCustomerrecords exist, you must write code to delete theAddressentity, when noCustomerentities 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 suchAddresses.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
@OneToManyannotated property in yourAddressentity (having theorphanRemovalattribute set to true) will not result in deletion of unreferencedAddressrecords. You may however, create a bidirectional relationship to avoid using a SQL or JPQL query (as you can then count the number ofCustomers in the collection property of theAddress).