简体   繁体   中英

hibernate: Custom code on insert / update

I'm new to hibernate and trying to evaluate if I can actually use it for my project. One type of entity needs to do some special checks on insert or update. I'm going to try to explain as easy as possible. Lets say we have colors. A user can create a Mixture of n colors and store that mixture in the database. Later if a user searches for color "blue" all Mixtures containing blue should be displayed.

Business rule is that any color should only be in the database once. So if a new mixture is inserted, it must first be checked if all the colors are already in the database and if yes that color should be reused (referenced) and if not a new one should be created.

If a mixture is changed say "blue" is replaced with "red", the behavior must be that the initial "blue" remains untouched and the system checks if "red" exists and either reuses it or creates it and then adds it to the mixture.

How can i do this with hibernate?

You can do it, but that does not have a lot with Hibernate.

The stuff which you describe is something what should be done by your service classes.

This should be logic of your application, not something what ORM tool should do.

Hear is link where you can read about Hibernate events

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