5

I have two entities like this :

public class Client {
  ...
@OneToMany(mappedBy = "client", cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.REMOVE },orphanRemoval = true, fetch=FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
@Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
private List<CompanyContract> companyContracts;
  ...
}

CompanyContract :

public class CompanyContract {

...

@ManyToOne
@JoinColumn(name = "IDCLIENT")
private Client client;

...
}

And I want to delete a companyContract like this :

client.getCompanyContracts().remove(companyContract)
client = clientService.updateClient(client);

But the delete process is not happening

What am I doing wrong ?

EDIT

This is my updateClient method in my DAO :

@Override
@Transactional
public Client updateClient(Client client) {

    return entityManager.merge(client);
}
Moatez Bouhdid
  • 1,311
  • 7
  • 19
  • 43

3 Answers3

8

First of all, you don't need set cascadeTypes and @Cascade. I think it might be a source of your problems.

Did you override equals and hashCode in ContractCompany? because maybe you are not removing the desired object from List<ContactCompany>

Look at this

@OneToMany(mappedBy = "client", ...)

mappedBy informs hibernate that client is the owner of this relation, so you need to get client, get a list of clientContracts, remove one (make sure it's removed from the list), update client and object is removed.

Example source code below

CompanyContract toBeRemoved = //companyContract which you want to delete
client.getCompanyContracts().remove(toBeRemoved);
toBeRemoved.client = null;

save both objects, toBeRemoved and client

Jakub Pomykała
  • 1,955
  • 3
  • 25
  • 55
1

Cascade delete-orphan example If you just want to delete two referenced ‘stockDailyRecords’ records? This is called orphan delete, see example…

1. No delete-orphan cascade
You need to delete the ‘stockDailyRecords’ one by one.

StockDailyRecord sdr1 = (StockDailyRecord)session.get(StockDailyRecord.class,
                                            new Integer(56));
StockDailyRecord sdr2 = (StockDailyRecord)session.get(StockDailyRecord.class,
                                            new Integer(57));

session.delete(sdr1);
session.delete(sdr2);
Output

Hibernate:
    delete from mkyong.stock_daily_record
    where DAILY_RECORD_ID=?
Hibernate:
    delete from mkyong.stock_daily_record
    where DAILY_RECORD_ID=?
  1. With delete-orphan cascade The cascade=”delete-orphan” is declared in ‘stockDailyRecords’ to enable the delete orphan cascade effect. When you save or update the Stock, it will remove those ‘stockDailyRecords’ which already mark as removed.

    Stock stock = (Stock)session.get(Stock.class, new Integer(2));
    stock.getStockDailyRecords().remove(sdr1);
    stock.getStockDailyRecords().remove(sdr2);
    
    session.saveOrUpdate(stock);
    

    Output

    Hibernate: delete from mkyong.stock_daily_record where DAILY_RECORD_ID=? Hibernate: delete from mkyong.stock_daily_record where DAILY_RECORD_ID=?

In short, delete-orphan allow parent table to delete few records (delete orphan) in its child table.

You should be using cascade=”delete-orphan” in order to perform such a task.

Reference

Sadiq Ali
  • 1,222
  • 2
  • 14
  • 22
0

Try it with these annotations:

@OneToMany(mappedBy = "client", cascade = CascadeType.ALL,orphanRemoval = true, fetch=FetchType.EAGER)
private List<CompanyContract> companyContracts;
Asarudeen A
  • 49
  • 1
  • 11