I am using hibernate annotations, spring, a sessionFactory and defining everything in a context.xml (like so..)
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>mypackage.model.Attributes</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
</bean>
On my entity I have several properties, one being the id and another a string value "externalId" that I need to be automatically generated. (as an example it might be "dev_" followed by a 5 digit numerical value representing the id. so if the id is 4 then the externalId would be 'dev_00004')
@Entity
@Table(name="ATTRIBUTES")
public class Attributes {
private Long id;
private String externalId;
...
...
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column( name = "ID" )
public Long getId() {
return id;
}
public void setId( Long p_id ) {
id = p_id;
}
@Column(name = "EXTERNALID")
public String getExternalId() {
return externalId;
}
public void setExternalId(String p_externalId) {
externalId = p_externalId;
}
...
...
Does anyone know how I can achieve this? I had a look into the @PrePersist but all the dao's etc use saveOrUpdate and the two don't seem to go hand in hand. I thought perhaps @preUpdate might work, but again that doesn't appear to get called. Can anyone give me any ideas on how I might achieve this?
Thanks!