Hi,
There are few places in my application where I need to generate "slugs" or friendly identifiers for my entities. For example, a product named "Hibernate in Action" should get its slug generated as "hibernate-in-action". Of course, if an entity with the same slug already exists in the database, a number is appended to avoid conflicts (e.g. "hibernate-in-action1").
I've been generating all this names in each of my application services, which leads to duplicate code. So I was wondering if I could get this done in a single point, such as a PreInsert Event Listener, but got some trouble as I would need to perform some queries (to find possible duplicates) inside onPreInsert, which (I think) flushes the session and causes failure, so maybe it's not the best place to do this kind of things.
Can anybody give me some advice on how to do this? Thanks in advance.
|