-->
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.  [ 3 posts ] 
Author Message
 Post subject: Prevent Hibernate from generating identity sometimes
PostPosted: Mon May 28, 2007 4:38 am 
Newbie

Joined: Mon May 28, 2007 4:20 am
Posts: 6
I am usingHibernate version: 3.3.1 (Annotations 3.2.2)

I have classes that are stored on a server database (MySQL). These classes are serialized and sent to a client, where they are to be put into a local database (Derby 10.2.2.0).

The classes contain annotations for generating an identity column. However on the client, the identity value has to be the same as on the server. When I add the received objects, Hibernate generates a new ID value, and (of course) throws an exception if I try to change it later.

Code:
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   @Column(name = "ID", nullable = false)
   private Integer id = null;


What I need is a way of overriding the annotation on the client, or forcing Hibernate to accept the incoming ID. I would like to use JPA standard things only because sometimes I have to use these classes with other frameworks (like Toplink).

There is a way out - if I can't find anything else - and that is to generate the ID's myself when new records are inserted on the server (and remove the @GeneratedValue). However I prefer not to do this if possible.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 28, 2007 5:22 am 
Newbie

Joined: Mon May 28, 2007 4:20 am
Posts: 6
I may just have solved this problem, though I'd still appreciate any feedback.

On the client (only) I added an orm.xml file that overrides the definition of the ID field only. (I have removed identifiers, and replaced them with '....')

This seems to do what's necessary - preventing Hibernate from generating an ID for this field - only on the client. I didn't add definitions for the other fields in the class.

Code:
<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
    version="1.0">
    <package>......</package>
    <entity class="....." name="....">
        <attributes>
            <id name="id">
               <column name="ID" />
            </id>
        </attributes>
    </entity>
</entity-mappings>


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 2:57 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
session.replicate might be interesting as well

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.