0
votes

I am new to Hibernate. I had read that when using cascade.ALL to persist, changes are propogated from the owning side. I have below example where I am saving the non owning entity and the owning entity is also saved.

class Account {

@OneToMany(mappedBy="account" , cascade=CascadeType.ALL)
List<Transaction> list= new ArrayList<Transaction>();

}

class Transaction {

@ManyToOne
@JoinColumn(name="account_id")
Account account;

}

Account a = new Account("savings");

    Transaction t1 = new Transaction("shoe purchase", 45);

    t1.setAccount(a);

    a.getList().add(t1);


    accountRepository.save(a); //If cascaded is on account

Does it matter which side cascade is on. In this case I could have used cascade.PERSIST

Another question I had was do we write unidirectional mapping for OneToMany/ManyToOne relationships ?

Thank you

1

1 Answers

0
votes

Yes, it does matter. If cascade is defined only on parent, when you make any changes on parent and persist it, then the essential changes on children will also be performed, but changing children won't affect parent entity. Same, when only child entity is cascading changes.