[hibernate-issues] [Hibernate-JIRA] Commented: (HBX-1034) Need a way to have a "native-if-not-assigned" IdentifierGenerator

radhakrishna (JIRA) noreply at atlassian.com
Wed Jul 22 12:55:12 EDT 2009


    [ http://opensource.atlassian.com/projects/hibernate/browse/HBX-1034?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=33654#action_33654 ] 

radhakrishna commented on HBX-1034:
-----------------------------------

-I wanted to override the default ID hibernate generates when saving using session.save() method, my mapping was defined as

 <id name="Id" type="java.lang.Long">
            <column name="ID" precision="11" scale="0" />
            <generator class="increment" />
        </id>

-and saving as

obj.setId(myCustomLongId);
org.hibernate.Session.save(obj)

created a huge problem as save would not even throw an exception that the ID in the object being persisted will be overridden by the PK-generator defined in the entity mapping file. SaveOrUpdate(), however, if the PK is already defined and if there is no persisted record in the database with this ID would surely throw an exception. Shouldn't save() also throw an exception when you change the user's ID while persisting a so called detached instance?

Anyways, I am successfully able to use session.replicate(obj, ReplicationMode.EXCEPTION) because I do know that the object does not exist in the database. What I do not understand is 

"This operation cascades to associated instances if the association is mapped with cascade="replicate"."

Although I did not try saving (using replicate) an entity with an associated instance such as a child collection each with it's own custom ID, I am curious what would happen in such a case, because I am not sure where I should be mentioning the cascade="replicate" as it is defined as cascade="all-delete-orphan" and I would want to use it.

public void replicate(Object object,
                      ReplicationMode replicationMode)
               throws HibernateException

    Persist the state of the given detached instance, reusing the current identifier value. This operation cascades to associated instances if the association is mapped with cascade="replicate".

    Parameters:
        object - a detached instance of a persistent class 
    Throws:
        HibernateException



> Need a way to have a "native-if-not-assigned" IdentifierGenerator
> -----------------------------------------------------------------
>
>                 Key: HBX-1034
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HBX-1034
>             Project: Hibernate Tools
>          Issue Type: New Feature
>            Reporter: Paul Shemansky
>         Attachments: hibernate-native-or-assigned-patch.diff
>
>   Original Estimate: 10 minutes
>  Remaining Estimate: 10 minutes
>
> NOTE: I believe I have explored every documented possibility in achieving the desired functionality.  If I have misused Hibernate, the forum, or Google, I humbly apologize and stand to be corrected.
> With that being said, I believe this may be desired:
> --------------------------------------------------------------------
> By setting the Hibernate configuration's primary key column to use a "native" implementation, Hibernate ALWAYS generates the entity's id when a save() is being called. There is no way to "override" the generated id.
> The attached patch creates an IdentityGenerator which allows a programmer to override the "generated" id, with an "assigned" id at runtime simply by setting the primary key property.
> The patch also changes the IdentifierGeneratorFactory to allow the short-name selection of the generator via a configuration file.
> Furthermore, the English documentation is patched to describe the generator, accordingly.
> Possibly related issues and reference material:
> The documentation does not seem to provide a solution for a native-if-not-assigned generator; you seem to only have one, or the other:
> http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-declaration-id-generator
> A scenario which describes a typical use case that warrants the proposed new feature:
> http://forum.hibernate.org/viewtopic.php?t=982560&highlight=native+id+generator

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list