Search code examples
grailsgrails-orm

Hibernate - how to generate id manually?


I am working on data migration from one db to a new one in Java. One of my issues are that I must keep the same id values of the entities.

I know it's possible to change the id generator, but I need to set the id manually. Each entity I create using Map from old object to a new one and I just set values. can't I just set manually something like:

Map<String, Object> newEntity = new HashMap<String, Object>()
newEntity.setId(oldEntity.get("id"))

Solution

  • I succeeded in doing it. In my entity I set the following:

    @Id
    private String id
    
    private Date created
    private Date modified
    
    static mapping = {
        id column: 'id', generator: 'assigned'
    }
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    
    public Date getCreated() {
        return created;
    }
    
    public void setCreated(Date created) {
        this.created = created;
    }
    public Date getModified() {
        return modified;
    }
    public void setModified(Date modified) {
        this.modified = modified;
    }
    

    and it worked. Thanks!