You are viewing...

Because Hibernate 4.1 docs are pretty annoying

Updated on February 13, 2013 at the 09th hour
Posted under:

DISCLAIMER: All views are considered my own and you should not draw any conclusions on associates.

Annotation Weirdness

http://stackoverflow.com/questions/305880/hibernate-annotation-placement-question

For a weird reason, it doesn't like @Id annotation on a field. It fails to get the type when I place it on the field. Even if it gets the type, the error cascades to joins and something will fail down there. :/ Still learning to use JPA Annotations for Hibernate.

Interceptors

http://stackoverflow.com/questions/11428410/hibernate-4-0-0final-where-is-the-sessionfactory-opensessioninterceptor-interce

If you wanted a per-session interceptor you need to do
Session session = sesssionfactory.withOptions()
.interceptor(new Interceptor())
.openSession();

Event Listeners


To create and use a listener you must first create a listener:
public class EntityListener
{

@PrePersist
@PostPersist
@PreUpdate
@PostUpdate
@PreRemove
@PostLoad
public void onEvent(Object thing)
{
//do thing
}
}

then attach the listener to the entity with this annotation: @EntityListeners({EntityListener.class}). Furthermore, you must use an EntityManager either generated from from E3jbConfiguration (deprecated) or Persistence.createEntityManagerFactory(). The former will use a hibernate.cfg.xml (go figure....) and the latter will need persistence.xml (in src/META-INF/ dir... just create it) because it is the JPA one.

You can also use those annotations in your entity class, but they do not require an argument.
You just read "Because Hibernate 4.1 docs are pretty annoying". Please share if you liked it!
You can read more recent posts here.