views:

55

answers:

3

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!

A: 

You can implement the PreInsertEventListener and hook it into your SessionFactory. Then implement the listener to watch for this particular object type and allocate your external ID as necessary.

You initialise the event listeners using a map property of the spring factory bean.

  <property name="eventListeners" ref="hibernateEventListenerMap">

  <util:map id="hibernateEventListenerMap">
    <entry key="pre-insert">
      <list>
        <ref bean="customEventListenerBeanNameHere"/>
      </list>
    </entry>
  </util:map>
Mike Q
Thanks, that's very helpful - just looking into it now :-)
Rainyday
A: 

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.

PrePersist/PreUpdate are JPA annotations so for them to work, you will have to use an EntityManager.

Can anyone give me any ideas on how I might achieve this?

Use the "equivalent" from Hibernate Core i.e. an interceptor or the event system.

Reference

Pascal Thivent
A: 

Did you try

public void setId( Long p_id ) {
        id = p_id;
        this.externalId = "dev_" + StringUtils.leftPad(p_id.toString(), 5, '0') );
    }

?

String utils from http://commons.apache.org/lang/api-2.5/index.html?org/apache/commons/lang/StringUtils.html

CelinHC