问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/12685653/hibernate-custom-code-on-insert-update