-->
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.  [ 1 post ] 
Author Message
 Post subject: @PrePersist is not fired when using AnnotationConfiguration
PostPosted: Thu Sep 06, 2007 4:30 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:


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.


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

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.