-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: AnnotaionConfiguration - @PrePersist is not fired
PostPosted: Thu Sep 06, 2007 4:48 am 
Newbie

Joined: Thu Sep 06, 2007 4:15 am
Posts: 3
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:



@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:

<?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!


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

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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 06, 2007 8:20 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
PrePersist and co are only handled by the EntityManager

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.