-->
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: IdentifierGenerator and EmptyInterceptor prevent storing?
PostPosted: Mon May 21, 2012 8:30 am 
Newbie

Joined: Mon May 21, 2012 5:57 am
Posts: 2
Hi,
I'm new with Hibernate and I'm facing some issue using a custom IdentifierGenerator and an Interceptor for populate a "CreationDate" field; debugging the code I'm able to see the fields populated, but then the entity is not stored in the database...
Separately they works like a charm...
Is normal or not?! I've searched a lot with no success...

Entity:
Code:
@Entity(name = "project")
@Table(name = "projects", uniqueConstraints = {
      @UniqueConstraint(columnNames = "id") })
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@ProjectEntityType
public class Project {      
   @Id
   @GenericGenerator(name="seq_id", strategy="it.afbnet.timesheet.projects.bean.id.ProjectIdentifierGenerator")
   @GeneratedValue(generator="seq_id", strategy=GenerationType.SEQUENCE)
   @Column(name = "id", unique = true, nullable = false)
   private String id;
.....
   @Column(name = "creationDate", unique = false, nullable = false)
   @DateTimeFormat(pattern = "dd/MM/yyyy HH:mm:ss")
   private Date creationDate;
.....
}


Dao:
Code:
public void save(Project project) {
      Session session = sessionFactory.getCurrentSession();
      session = session.sessionWithOptions().interceptor(new ProjectInterceptor()).flushBeforeCompletion(false).openSession();
      if(project.getId()==null)
         session.save(project);
      else
         session.merge(project);
   }


Interceptor:
Code:
private class ProjectInterceptor extends EmptyInterceptor
   {
      private static final long serialVersionUID = -1430713218109883245L;

      @Override
      public boolean onSave(Object entity, Serializable id, Object[] state,
            String[] propertyNames, Type[] types) {
         
         
         for (int i = 0; i < propertyNames.length; i++) {
               if ("creationDate".equals(propertyNames[i])) state[i] = new Date();
             }
         return true;
      }   
   }


Thank's in advance!!


Top
 Profile  
 
 Post subject: Re: IdentifierGenerator and EmptyInterceptor prevent storing?
PostPosted: Tue May 29, 2012 4:34 am 
Newbie

Joined: Mon May 21, 2012 5:57 am
Posts: 2
no one?


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.