Suppose you have a One-To-Many relation (header-detail) on Pembelian and PembelianDetail. If you are not Indonesian then to make it easier for you Pembelian is Purchase in English.
As usual we use @OneToMany and @ManyToOne annotation in our domain class. When writing those annotation it depends on our strategy, whether we will use 1 DAO+Cascade or 2 DAO to persist the data.

To better understanding my sentence here’s the code

1 DAO+Cascade

In this strategy we use just 1 DAO to persist both the header and the detail data which is Pembelian and PembelianDetail.

1

2

3

4

5

6

7

8

publicclassPembelianDaoImplimplementsPembelianDao{

@Autowired

SessionFactory sessionFactory;

publicvoidsave(Pembelian pembelian){

sessionFactory.getCurrentSession().save(pembelian);

}

}

This DAO is called in PembelianService with declarative transaction using @Transactional annotaion.

1

2

3

4

5

6

7

8

9

@Transactional

publicclassPembelianServiceImplimplementsPembelianService{

@Autowired

PembelianDao pembelianDao;

publicvoidsave(Pembelian pembelian){

pembelianDao.save(pembelian);

}

}

So for you who might wondering how this strategy can save the detail data (PembelianDetail) since we didn’t create PembelianDetailDao here’s the trick. We use cascade property in @OneToMany so that every PenjualanDetail attached to Penjualan will be persisted automatically.

Again, we use PembelianService to call PembelianDao. The service is the same with the one we wrote above, there is no difference

1

2

3

4

5

6

7

8

9

@Transactional

publicclassPembelianServiceImplimplementsPembelianService{

@Autowired

PembelianDao pembelianDao;

publicvoidsave(Pembelian pembelian){

pembelianDao.save(pembelian);

}

}

The SQL is same as well

1

2

3

Hibernate:insert into PENJUALAN(tanggalTransaksi)values(?)

Hibernate:insert into PENJUALAN_DETAIL(ID_PENJUALAN,produk)values(?,?)

Hibernate:insert into PENJUALAN_DETAIL(ID_PENJUALAN,produk)values(?,?)

Conclusion

So both strategy is actually the same, we can infer it from the generated SQL. The important part is both persist strategy is in one transaction. 1 Dao+Cascade is in one transaction. 2 Dao is also in one transaction.