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