These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: DB Export/Import: Cannot setId() with "native" id generator
PostPosted: Tue May 19, 2009 2:37 pm 
Newbie

Joined: Sun Dec 04, 2005 2:24 pm
Posts: 9
An important facility in my application is to be able to backup/restore the database via a XML file.

Obviously when restoring entities, the primary key values must be preserved. However I cannot figure how to override the native ID generator specified in the mapping file for the purpose of restoring the database.

If I remove <generator class="native"/> from the id element in the entity mapping I can do it. But that is not an acceptable solution as automatic ID generation is required at all other times.

Any suggestions on how I can have automatic ID generation and still be able to explicitly set the primary key value when creating an entity?

I am using Hibernate 3.3 and MySQL 5.0.

Thanks!


Top
 Profile  
 
 Post subject: Re: DB Export/Import: Cannot setId() with "native" id generator
PostPosted: Wed May 20, 2009 1:44 pm 
Newbie

Joined: Sun Dec 04, 2005 2:24 pm
Posts: 9
I thought of a few solutions:

1. Dynamically change the generator class while restoring the database (I've seen people ask about this, but little in the way of replies)
2. Use native SQL to create stub records, but this involves that the application have knowledge of the db schema: very ugly.
3. In addition to the normal mapping for the entity class, have a dummy mapping using entity-name to distinguish it from the main mapping. This dummy mapping will use assigned ID generation, but will omit all properties other than the ID to reduce code duplication.

Option 3 looks to be the most promising. The mapping file looks like this:

Code:
<!-- proper mapping -->
<class name="com.acme.project.SubjectNode" table="subject_nodes">
   <id name="id" column="id">
      <generator class="native"/>
   </id>
        <property name="name" column="name" />
       <!-- etc ... -->
</class>

<!-- dummy mapping, with just id and (implied) assigned ID generator -->   
<class name="com.acme.project.SubjectNode" table="subject_nodes" entity-name="DummySubjectNode">
   <id name="id" column="id" />
</class>


To restore an entity with a particular ID I do

Code:
Long nodeId = new Long(nodeEl.valueOf("@id"));
      
// First save dummy version of SubjectNode which has "assigned" ID generator
SubjectNode node = new SubjectNode();
node.setId(nodeId);
hsession.save("DummySubjectNode", node);
   
// Make sure dummy entity is actually in the database
hsession.flush();
      
// Now reload this entity using the normal mapping and populate the properties
node = (SubjectNode)hsession.load(SubjectNode.class, nodeId);
node.setName(nodeEl.valueOf("@name"));
// set other properties etc...
hsession.save(node);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.