简体   繁体   中英

Many-To-One in hibernate Cascade="none"

If I have an object who is part of many-to-one relationship with other objects and the setting of CASCADE is set to NONE, DO I HAVE TO INDIVIDUALLY SAVE the objects?

For Example

Inside HeroObject there's a SkillObject, inside SkillObject there's CooldownObject. If I want to persist HeroObject in the database, do I have to individually save SkillObject as well and CooldownObject.

HeroObject has its own heroDao.saveMethod(), so does SkillObject - sillDao.saveMethod() and coolDownObject with cooldownDao.saveMethod().

So if the CASCADE is set to "NONE", I have to perform 3 saves() in this example. Am I making any sense here?

if you cascade set to none then you have to save object individually. that's tedious job. its better to use cascade while using mapping. If you save an HeroObject , then all object will also be saved into database. If you delete an HeroObject then all accounts associated with that HeroObject also be deleted. But what if we only want to cascade only save operations but not delete operation. Then we need to clearly specify by Hibernate Cascade Types.. you can visit this link for more info. [ https://howtodoinjava.com/hibernate/hibernate-jpa-cascade-types/][1]

@Noogui Yes you will have to perform operations manually, you have OneToMany mapping between these objects, Hero can have many skills, and a skill can have many type of cool-down settings(as per my understanding) So now if hibernate have to map them in join tables they need to be saved first. But why are you doing it this way unless until there isn't any exceptional case you should benefit from Cascade operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM