Hello,
I have a question about AnnotationConfiguration.
I want to configure hiberante through annotations and use @PrePersist and @PreMerge callbacks for entities.
I declares class MyEntity:
Code:
@Entity
@Table(name="OBJECTS")
public class MyEntity {
private Integer objectID;
@Id
@Column(name="OBJECT_ID")
@SequenceGenerator(name="OBJECT_SEQ", sequenceName="OBJECT_SEQ", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="OBJECT_SEQ")
public Integer getObjectID() {
return ObjectID;
}
public void setObjectID(Integer objectID) {
this.objectID = objectID;
}
@PrePersist
public void prePersist()
{
System.out.println("In Object");
}
@PreRemove
public void preRemove()
{
System.out.println("here");
}
}
My hibernate.cfg.xml file:
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">....</property>
<property name="connection.username">...</property>
<property name="connection.password">...</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<mapping class="MyEntity"/>
</session-factory>
</hibernate-configuration>
But the following code doesn't fire PrePersist procedure!
Code:
AnnotationConfiguration conf = new AnnotationConfiguration();
SessionFactory sessionFactory = conf.configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.persist(new MyEntity());
session.flush();
session.close();
and this code works fine
Code:
Ejb3Configuration conf = new Ejb3Configuration();
EntityManagerFactory emf = conf.configure("hibernate.cfg.xml").buildEntityManagerFactory();
EntityManager em = emf.createEntityManager();
em.persist(new MyEntity());
em.close();
Is there any way to use callbacks through annotaions and sessionFactory?
Thank you.