-->
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.  [ 7 posts ] 
Author Message
 Post subject: Unexpected update to Parent when insert Child
PostPosted: Tue Sep 13, 2005 1:06 am 
Newbie

Joined: Mon Aug 30, 2004 1:37 am
Posts: 16
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0.5

Mapping documents:
Parent.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">   
<hibernate-mapping>

<class
    name="hib3jdk14Test.biDirOneToManyTest01.Parent"
    table="parent"
    lazy="true"
>
    <id
        name="parentId"
        type="long"
        column="parent_id"
    >
        <generator class="assigned"/>
    </id>

    <version
        name="version"
        type="integer"
        column="version"
        unsaved-value="negative"
    />
    <property
        name="parentName"
        type="string"
        column="parent_name"
        length="50"
        not-null="true"
    >
    </property>
   
    <!-- Associations -->
 
    <!-- bi-directional one-to-many association to Child01 -->
    <set
        name="children"
        lazy="true"
        inverse="true"
      cascade="none"
    >
        <key>
            <column name="parent_id" />
        </key>
        <one-to-many
            class="hib3jdk14Test.biDirOneToManyTest01.Child01"
        />
    </set>
</class>
</hibernate-mapping>


Child01.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
       
<hibernate-mapping>

<class
    name="hib3jdk14Test.biDirOneToManyTest01.Child01"
    table="child01"
    lazy="true"
>
    <id
        name="childId"
        type="long"
        column="child_id"
    >
        <generator class="assigned"/>
    </id>

    <version
        name="version"
        type="integer"
        column="version"
        unsaved-value="negative"
    />
   
    <property
        name="childName"
        type="string"
        column="child_name"
        length="50"
        not-null="true"
    >
    </property>
   
    <!-- Associations -->
 
    <!-- bi-directional many-to-one association to Parent -->
    <many-to-one
        name="parent"
        class="hib3jdk14Test.biDirOneToManyTest01.Parent"
        not-null="true"
    >
        <column name="parent_id" />
    </many-to-one>
   
</class>
</hibernate-mapping>


POJOs:
Parent.java:
Code:
package hib3jdk14Test.biDirOneToManyTest01;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Parent implements Serializable {
    /** identifier field */
    private Long parentId;
    private String parentName;
    private int version = -1;
    private Set children = new HashSet();
   
    /** no-arg constructor for JavaBean tools */
    public Parent() {
    }

    /** main constructor */
    public Parent(Long parentId, String parentName) {
        this.parentId = parentId;
        this.parentName = parentName;
    }

    public Long getParentId() {
        return this.parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public String getParentName() {
        return this.parentName;
    }

    public void setParentName(String parentName) {
        this.parentName = parentName;
    }

    public int getVersion() {
        return this.version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public Set getChildren() {
        return this.children;
    }

    private void setChildren(Set children) {
        this.children = children;
    }

    //scaffolding code for 1:n
    public void addChild(Child01 child){
        if (child == null){
            throw new IllegalArgumentException("Can't add a null child");
        }
        child.setParent(this);
        this.children.add(child);//in mapping inverse=true so should not trigger an update but it does..
                          //it also triggers a sql select of all the children
    }
   
    public String toString() {
        return ("[parentId]" + getParentId() + "[parentName]" + getParentName());
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Parent)) return false;                                                                       
        final Parent parent = (Parent) o;                                                                       
        if (!getParentName().equals(parent.getParentName())) return false;                                                                       
        return true;
    }
                                                                       
    public int hashCode() {
        int result;
        result = getParentName().hashCode();
        result = 29 * result;
        return result;
    }
}


Child01.java
Code:
package hib3jdk14Test.biDirOneToManyTest01;

import java.io.Serializable;

public class Child01 implements Serializable {
   
    /** identifier field */
    private Long childId;
    private String childName;
    private int version = -1;
    private Parent parent;

    /** no-arg constructor for JavaBean tools */
    public Child01() {
    }

    /** main constructor
     */
    public Child01(Long childId, String childName, Parent parent) {
        this.childId = childId;
        this.childName = childName;
        parent.addChild(this);
    }

    public Long getChildId() {
        return this.childId;
    }

    private void setChildId(Long childId) {
        this.childId = childId;
    }

    public String getChildName() {
        return this.childName;
    }

    public void setChildName(String childName) {
        this.childName = childName;
    }

    public int getVersion() {
        return this.version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public Parent getParent() {
        return this.parent;
    }

    public void setParent(Parent parent) {
        this.parent = parent;
    }

    public String toString() {
        return  ("[childId]" + getChildId() + "[childName]" + getChildName());
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Child01)) return false;                                                                       
        final Child01 child = (Child01) o;
        if (!getChildName().equals(child.getChildName())) return false;
        return true;
    }
                                                                       
    public int hashCode() {
        int result;
        result = getChildName().hashCode();
        result = 29 * result;
        return result;
    }
                                                                       
   
}


Code between sessionFactory.openSession() and session.close():
Code:
           session = sf.openSession();
            tx = session.beginTransaction();
            System.out.println("**** Getting parent ****");
            parent = (Parent)session.get(Parent.class, parentId);
            System.out.println("**** Creating new Child ****");
            Child01 child = new Child01(childId, childName, parent);
            System.out.println("**** Saving Child ****");
            session.save(child);           
            tx.commit();       
            session.close();


Full stack trace of any exception that occurs:
none

Name and version of the database you are using:
hsql

The generated SQL (show_sql=true):
[java] Hibernate: select parent0_.parent_id as parent1_0_, parent0_.version as version0_0_, parent0_.parent_name as parent3_0_0_ from parent parent0_ where parent0_.parent_id=?
[java] Hibernate: select children0_.parent_id as parent4_1_, children0_.child_id as child1_1_, children0_.child_id as child1_0_, children0_.version as version1_0_, children0_.child_name as child3_1_0_, children0_.parent_id as parent4_1_0_ from child01 children0_ where children0_.parent_id=?
[java] Hibernate: insert into child01 (version, child_name, parent_id, child_id) values (?, ?, ?, ?)
[java] Hibernate: update parent set version=?, parent_name=? where parent_id=? and version=?


Debug level Hibernate log excerpt:
[java] **** About to insert child ****
[java] 22:29:53,689 DEBUG SessionImpl: opened session at timestamp: 4614465919750144
[java] 22:29:53,690 DEBUG JDBCTransaction: begin
[java] 22:29:53,692 DEBUG ConnectionManager: opening JDBC connection
[java] 22:29:53,693 DEBUG DriverManagerConnectionProvider: total checked-out connections: 0
[java] 22:29:53,694 DEBUG DriverManagerConnectionProvider: using pooled JDBC connection, pool size: 0
[java] 22:29:53,696 DEBUG JDBCTransaction: current autocommit status: false
[java] **** Getting parent ****
[java] 22:29:53,702 DEBUG DefaultLoadEventListener: loading entity: [hib3jdk14Test.biDirOneToManyTest01.Parent#1]
[java] 22:29:53,704 DEBUG DefaultLoadEventListener: attempting to resolve: [hib3jdk14Test.biDirOneToManyTest01.Parent#1]
[java] 22:29:53,705 DEBUG DefaultLoadEventListener: object not resolved in any cache: [hib3jdk14Test.biDirOneToManyTest01.Parent#1]
[java] 22:29:53,727 DEBUG BasicEntityPersister: Materializing entity: [hib3jdk14Test.biDirOneToManyTest01.Parent#1]
[java] 22:29:53,728 DEBUG Loader: loading entity: [hib3jdk14Test.biDirOneToManyTest01.Parent#1]
[java] 22:29:53,734 DEBUG AbstractBatcher: about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
[java] 22:29:53,734 DEBUG SQL: select parent0_.parent_id as parent1_0_, parent0_.version as version0_0_, parent0_.parent_name as parent3_0_0_ from parent parent0_ where parent0_.parent_id=?
[java] Hibernate: select parent0_.parent_id as parent1_0_, parent0_.version as version0_0_, parent0_.parent_name as parent3_0_0_ from parent parent0_ where parent0_.parent_id=?
[java] 22:29:53,734 DEBUG AbstractBatcher: preparing statement
[java] 22:29:53,746 DEBUG LongType: binding '1' to parameter: 1
[java] 22:29:53,749 DEBUG AbstractBatcher: about to open ResultSet (open ResultSets: 0, globally: 0)
[java] 22:29:53,749 DEBUG Loader: processing result set
[java] 22:29:53,749 DEBUG Loader: result set row: 0
[java] 22:29:53,749 DEBUG Loader: result row: EntityKey[hib3jdk14Test.biDirOneToManyTest01.Parent#1]
[java] 22:29:53,750 DEBUG Loader: Initializing object from ResultSet: [hib3jdk14Test.biDirOneToManyTest01.Parent#1]
[java] 22:29:53,754 DEBUG BasicEntityPersister: Hydrating entity: [hib3jdk14Test.biDirOneToManyTest01.Parent#1]
[java] 22:29:53,754 DEBUG IntegerType: returning '0' as column: version0_0_
[java] 22:29:53,754 DEBUG StringType: returning 'Parent 1' as column: parent3_0_0_
[java] 22:29:53,754 DEBUG TwoPhaseLoad: Version: 0
[java] 22:29:53,755 DEBUG Loader: done processing result set (1 rows)
[java] 22:29:53,755 DEBUG AbstractBatcher: about to close ResultSet (open ResultSets: 1, globally: 1)
[java] 22:29:53,755 DEBUG AbstractBatcher: about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
[java] 22:29:53,755 DEBUG AbstractBatcher: closing statement
[java] 22:29:53,757 DEBUG Loader: total objects hydrated: 1
[java] 22:29:53,758 DEBUG TwoPhaseLoad: resolving associations for [hib3jdk14Test.biDirOneToManyTest01.Parent#1]
[java] 22:29:53,763 DEBUG CollectionLoadContext: creating collection wrapper:[hib3jdk14Test.biDirOneToManyTest01.Parent.children#1]
[java] 22:29:53,764 DEBUG TwoPhaseLoad: done materializing entity [hib3jdk14Test.biDirOneToManyTest01.Parent#1]
[java] 22:29:53,764 DEBUG PersistenceContext: initializing non-lazy collections
[java] 22:29:53,764 DEBUG Loader: done entity load
[java] **** Creating new Child ****
[java] 22:29:53,765 DEBUG DefaultInitializeCollectionEventListener: initializing collection [hib3jdk14Test.biDirOneToManyTest01.Parent.children#1]
[java] 22:29:53,766 DEBUG DefaultInitializeCollectionEventListener: checking second-level cache
[java] 22:29:53,766 DEBUG DefaultInitializeCollectionEventListener: collection not cached
[java] 22:29:53,766 DEBUG Loader: loading collection: [hib3jdk14Test.biDirOneToManyTest01.Parent.children#1]
[java] 22:29:53,766 DEBUG AbstractBatcher: about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
[java] 22:29:53,766 DEBUG SQL: select children0_.parent_id as parent4_1_, children0_.child_id as child1_1_, children0_.child_id as child1_0_, children0_.version as version1_0_, children0_.child_name as child3_1_0_, children0_.parent_id as parent4_1_0_ from child01 children0_ where children0_.parent_id=?
[java] Hibernate: select children0_.parent_id as parent4_1_, children0_.child_id as child1_1_, children0_.child_id as child1_0_, children0_.version as version1_0_, children0_.child_name as child3_1_0_, children0_.parent_id as parent4_1_0_ from child01 children0_ where children0_.parent_id=?
[java] 22:29:53,766 DEBUG AbstractBatcher: preparing statement
[java] 22:29:53,767 DEBUG LongType: binding '1' to parameter: 1
[java] 22:29:53,767 DEBUG AbstractBatcher: about to open ResultSet (open ResultSets: 0, globally: 0)
[java] 22:29:53,769 DEBUG Loader: result set contains (possibly empty) collection: [hib3jdk14Test.biDirOneToManyTest01.Parent.children#1]
[java] 22:29:53,769 DEBUG CollectionLoadContext: uninitialized collection: initializing
[java] 22:29:53,770 DEBUG Loader: processing result set
[java] 22:29:53,770 DEBUG Loader: done processing result set (0 rows)
[java] 22:29:53,770 DEBUG AbstractBatcher: about to close ResultSet (open ResultSets: 1, globally: 1)
[java] 22:29:53,770 DEBUG AbstractBatcher: about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
[java] 22:29:53,821 DEBUG AbstractBatcher: closing statement
[java] 22:29:53,822 DEBUG Loader: total objects hydrated: 0
[java] 22:29:53,824 DEBUG CollectionLoadContext: 1 collections were found in result set for role: hib3jdk14Test.biDirOneToManyTest01.Parent.children
[java] 22:29:53,825 DEBUG CollectionLoadContext: collection fully initialized: [hib3jdk14Test.biDirOneToManyTest01.Parent.children#1]
[java] 22:29:53,827 DEBUG CollectionLoadContext: 1 collections initialized for role: hib3jdk14Test.biDirOneToManyTest01.Parent.children
[java] 22:29:53,828 DEBUG PersistenceContext: initializing non-lazy collections
[java] 22:29:53,830 DEBUG Loader: done loading collection
[java] 22:29:53,831 DEBUG DefaultInitializeCollectionEventListener: collection initialized
[java] **** Saving Child ****
[java] 22:29:53,834 DEBUG DefaultSaveOrUpdateEventListener: saving transient instance
[java] 22:29:53,835 DEBUG AbstractSaveEventListener: generated identifier: 1, using strategy: org.hibernate.id.Assigned
[java] 22:29:53,836 DEBUG AbstractSaveEventListener: saving [hib3jdk14Test.biDirOneToManyTest01.Child01#1]
[java] 22:29:53,838 DEBUG Versioning: Seeding: 0
[java] 22:29:53,840 DEBUG JDBCTransaction: commit
[java] 22:29:53,841 DEBUG SessionImpl: automatically flushing session
[java] 22:29:53,842 DEBUG AbstractFlushingEventListener: flushing session
[java] 22:29:53,844 DEBUG AbstractFlushingEventListener: processing flush-time cascades
[java] 22:29:53,845 DEBUG AbstractFlushingEventListener: dirty checking collections
[java] 22:29:53,870 DEBUG CollectionEntry: Collection dirty: [hib3jdk14Test.biDirOneToManyTest01.Parent.children#1]
[java] 22:29:53,872 DEBUG AbstractFlushingEventListener: Flushing entities and processing referenced collections
[java] 22:29:53,874 DEBUG DefaultFlushEntityEventListener: Updating entity: [hib3jdk14Test.biDirOneToManyTest01.Parent#1]
[java] 22:29:53,875 DEBUG Versioning: Incrementing: 0 to 1
[java] 22:29:53,880 DEBUG Collections: Collection found: [hib3jdk14Test.biDirOneToManyTest01.Parent.children#1], was: [hib3jdk14Test.biDirOneToManyTest01.Parent.children#1] (initialized)
[java] 22:29:53,881 DEBUG AbstractFlushingEventListener: Processing unreferenced collections
[java] 22:29:53,883 DEBUG AbstractFlushingEventListener: Scheduling collection removes/(re)creates/updates
[java] 22:29:53,886 DEBUG AbstractFlushingEventListener: Flushed: 1 insertions, 1 updates, 0 deletions to 2 objects
[java] 22:29:53,887 DEBUG AbstractFlushingEventListener: Flushed: 0 (re)creations, 1 updates, 0 removals to 1 collections
[java] 22:29:53,889 DEBUG Printer: listing entities:
[java] 22:29:53,890 DEBUG Printer: hib3jdk14Test.biDirOneToManyTest01.Child01{childName=child01 1, childId=1, parent=hib3jdk14Test.biDirOneToManyTest01.Parent#1, version=0}
[java] 22:29:53,893 DEBUG Printer: hib3jdk14Test.biDirOneToManyTest01.Parent{parentId=1, parentName=Parent 1, children=[hib3jdk14Test.biDirOneToManyTest01.Child01#1], version=0}
[java] 22:29:53,895 DEBUG AbstractFlushingEventListener: executing flush
[java] 22:29:53,896 DEBUG BasicEntityPersister: Inserting entity: [hib3jdk14Test.biDirOneToManyTest01.Child01#1]
[java] 22:29:53,897 DEBUG BasicEntityPersister: Version: 0
[java] 22:29:53,899 DEBUG AbstractBatcher: about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
[java] 22:29:53,900 DEBUG SQL: insert into child01 (version, child_name, parent_id, child_id) values (?, ?, ?, ?)
[java] Hibernate: insert into child01 (version, child_name, parent_id, child_id) values (?, ?, ?, ?)
[java] 22:29:53,903 DEBUG AbstractBatcher: preparing statement
[java] 22:29:53,904 DEBUG BasicEntityPersister: Dehydrating entity: [hib3jdk14Test.biDirOneToManyTest01.Child01#1]
[java] 22:29:53,906 DEBUG IntegerType: binding '0' to parameter: 1
[java] 22:29:53,907 DEBUG StringType: binding 'child01 1' to parameter: 2
[java] 22:29:53,911 DEBUG LongType: binding '1' to parameter: 3
[java] 22:29:53,912 DEBUG LongType: binding '1' to parameter: 4
[java] 22:29:53,914 DEBUG AbstractBatcher: Adding to batch
[java] 22:29:53,915 DEBUG AbstractBatcher: Executing batch size: 1
[java] 22:29:53,917 DEBUG AbstractBatcher: about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
[java] 22:29:53,918 DEBUG AbstractBatcher: closing statement
[java] 22:29:53,924 DEBUG ConnectionManager: running Session.finalize()
[java] 22:29:53,948 DEBUG BasicEntityPersister: Updating entity: [hib3jdk14Test.biDirOneToManyTest01.Parent#1]
[java] 22:29:53,950 DEBUG BasicEntityPersister: Existing version: 0 -> New version: 1
[java] 22:29:53,951 DEBUG AbstractBatcher: about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
[java] 22:29:53,952 DEBUG SQL: update parent set version=?, parent_name=? where parent_id=? and version=?
[java] Hibernate: update parent set version=?, parent_name=? where parent_id=? and version=?
[java] 22:29:53,955 DEBUG AbstractBatcher: preparing statement
[java] 22:29:53,957 DEBUG BasicEntityPersister: Dehydrating entity: [hib3jdk14Test.biDirOneToManyTest01.Parent#1]
[java] 22:29:53,958 DEBUG IntegerType: binding '1' to parameter: 1
[java] 22:29:53,960 DEBUG StringType: binding 'Parent 1' to parameter: 2
[java] 22:29:53,961 DEBUG LongType: binding '1' to parameter: 3
[java] 22:29:53,963 DEBUG IntegerType: binding '0' to parameter: 4
[java] 22:29:53,965 DEBUG AbstractBatcher: about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
[java] 22:29:53,967 DEBUG AbstractBatcher: closing statement
[java] 22:29:53,980 DEBUG AbstractFlushingEventListener: post flush
[java] 22:29:53,982 DEBUG JDBCContext: before transaction completion
[java] 22:29:53,983 DEBUG SessionImpl: before transaction completion
[java] 22:29:53,985 DEBUG JDBCTransaction: committed JDBC Connection
[java] 22:29:53,986 DEBUG JDBCContext: after transaction completion
[java] 22:29:53,987 DEBUG SessionImpl: after transaction completion
[java] 22:29:53,989 DEBUG SessionImpl: closing session
[java] 22:29:53,990 DEBUG ConnectionManager: closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
[java] 22:29:53,991 DEBUG DriverManagerConnectionProvider: returning connection to pool, pool size: 1
[java] 22:29:53,993 DEBUG JDBCContext: after transaction completion
[java] 22:29:53,994 DEBUG SessionImpl: after transaction completion
[java] **** After inserting child ****

As shown above I have set up a bidirectional one to many association between a Parent and Child01. In the Parent I have an addChild method which adds the Child01 to the collection of children in the Parent and sets the Parent in the Child01 soas to maintain the bidirectional association. When I create a Child01 I use the addChild method in the Parent - but this is triggering a select of all children for this Parent and an update of the Parent in addition to the desired insert of the Child01.
Have I done something incorrect in the mapping/code to cause this? How can I prevent this from happening? In my real application the end user may not have update permission on the Parent which will cause the transaction to fail. Also there may be many thousands of children for a given Parent so the select would kill performance.
Thanks for any advice.
Grainne.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 1:45 am 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
Hi,

Just a short question (that may be a hint for you): did you notice that the update on parent sets the version number from 0 to 1?

This is the issue. Hibernate considers that a change in the children implies a new version of the parent - which may be good or not, depending on what you want.

Erik


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 12:40 pm 
Newbie

Joined: Mon Aug 30, 2004 1:37 am
Posts: 16
Thanks for the reply Erik.

I did notice the update to the version but thought that was a consequence of Hibernate thinking something had been updated. I also noticed the entry in the debug output
Quote:
Collection dirty: [hib3jdk14Test.biDirOneToManyTest01.Parent.children#1


However, in the mappinng I set the
Quote:
inverse=true

on the set of children in the Parent - my understanding from the documentation http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#example-parentchild-bidir section 22.2 is that this should prevent the extra update. Am I missing something here? Do I need to do something extra to prevent this?

Grainne.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 2:20 pm 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
As far as I understand Hibernate (which is quite questionnable ;-),
Code:
inverse="true"

has nothing to do with versioning.
The fact that you add a new child is seen as a change to the parent and if you think of it it makes sense: the parent is not longer in the same "state" after a child has been added.

The book Hibernate in action describes this issue in details - but I don't think I can afford cut/pasting Hibernate in action in this forum :-). So I can only recommend that you buy it instead (a PDF version can be bought online) and read page 171.

My feeling is therefore that what you expect is simply not correct (it would defeat the versioning mechanism) - but maybe a "real" Hibernate Guru can correct me if I'm wrong.

Erik


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 6:07 pm 
Newbie

Joined: Mon Aug 30, 2004 1:37 am
Posts: 16
Thanks again Erik. I have a well thumbed copy of HIA and have read the section you mention regarding versioning.

I tend to look at things from the point of view of the database.
I add a Child01 to a Parent - that means that I create a child01 row with a foreign key reference to the primay key on the parent. That is all, the parent for the child01 is unaffected. In my experience with relational databases, it is more the exception than the rule that you want to update the version on the parent row when a child row is added.

From Hibernate's point of view, I am using assigned Ids therefore I need to use versioning so that Hibernate will know if an entity is new or not, so it knows whether to insert or update. I also need optimistic locking so I would use versioning for that in any case.

Why does Hibernate want to update the version of the Parent when a Child01 is added? Probably so it can manage the state of the link between the the Parent and the Child01. By setting inverse=true I am telling Hibernate I want that link managed from the Child01 end of the association. Shouldn't the version of the Parent not be updated in that case? It would be nice if there were an interaction between the two settings to accommodate this.

Bottom line, my situation is that
1. I need to use versioning since I am using assigned ids
2. I want to maintain the bidirectionality of the association
3. I do not want the Parent's version updated when I add a new Child01 (some users will only have select permission on this table).
4. I do not want all the children of the Parent retrieved from the database when I insert a new Child01 (could be thousands of children).
Is there any way I can achieve this? Some setting in the mapping, a class which can be extended etc.?

Thanks for any suggestions,
Grainne.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 6:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
<set optimistic-lock="false">

refer to the Hibernate reference docs


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 7:01 pm 
Newbie

Joined: Mon Aug 30, 2004 1:37 am
Posts: 16
Thanks Gavin! That is exactly it.
Again thanks to Erik for your replies.

Grainne.


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