开发者

how can i automatically generate a non id value in hibernate?

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!


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

  • Hibernate Core Reference Guide
    • Chapter 12. Interceptors and events


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜