Manually assign a value for sleep mode UUID

As you know, in sleep mode, set the id generator to "uuid", then sleep mode will automatically generate the UUID value in the id field when a new object is saved. If the generator setting is “assigned”, the identifier must be assigned a value before saving the object.

And I found that if I set the generator to uuid and assign the id value manually, hibernation will change the value to the new UUID.

My question is: when the generator is configured as uuid, how do I manually assign a value to it?

PS: I use spring HibernateDaoSupport to save.

org.springframework.orm.hibernate3.support.HibernateDaoSupport.save(Ojbect obj)

Thank!

+3
source share
1 answer

, - INSERT native SQL save().

:

public class FallbackUUIDHexGenerator extends UUIDHexGenerator {
    private String entityName;

    @Override
    public void configure(Type type, Properties params, Dialect d)
            throws MappingException {
        entityName = params.getProperty(ENTITY_NAME);
        super.configure(type, params, d);
    }

    @Override
    public Serializable generate(SessionImplementor session, Object object)
            throws HibernateException {            
        Serializable id = session
            .getEntityPersister(entityName, object)
            .getIdentifier(object, session);       

        if (id == null)
            return super.generate(session, object);
        else
            return id;
    }
}

Hibernate, , strategy.

+9

All Articles