-->
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.  [ 5 posts ] 
Author Message
 Post subject: How save child list for new object without update?
PostPosted: Mon Nov 19, 2007 2:21 pm 
Newbie

Joined: Mon Nov 19, 2007 1:57 pm
Posts: 14
Hello All,
I create bidirectional associations,
simular to paragraph "6.3.3. Bidirectional associations with indexed collections" from "Hibernate Reference Documentation",
(and http://www.hibernate.org/116.html#A12)
As collection I use list.

I add to parent object one child object.
When save new parent object, child object inserted, and after that updated.

Question: How I can remove update for child after saving new parent object,
or maybe someone know for what this update when save new object?

Possible, present way for unidirectional association without updating.

Thanks.

Hibernate version:
3.1.3
Mapping documents:
--*- Parent class collection mapping --
Code:
<list
            name="values2"
            cascade="all">
            <key
                column="PARENT_ID"
                not-null="true">
            </key>
            <list-index column="CHILD_IDX" />
            <one-to-many class="testhibernate.ChildEntity" />
        </list>

--*- child class mapping ---
Code:
<many-to-one
            name="parent"
            column="PARENT_ID"
            not-null="true"
            insert="false"
            update="false" />

Name and version of the database you are using:
Oracle 10.
The generated SQL (show_sql=true):
--*- sql after saving one parent object with child --
Code:
Hibernate:
    select
        SEQ.nextval
    from
        dual
Hibernate:
    select
        SEQ.nextval
    from
        dual
Hibernate:
    /* insert testhibernate.ParentEntity
        */ insert
        into
            A_TEST
            (id)
        values
            (?)
Hibernate:
    /* insert testhibernate.ChildEntity
        */ insert
        into
            A_TEST_CHILD
            (PARENT_ID, CHILD_IDX, id)
        values
            (?, ?, ?)
Hibernate:
    /* create one-to-many row testhibernate.ParentEntity.values2 */ update
        A_TEST_CHILD
    set
        PARENT_ID=?,
        CHILD_IDX=?
    where
        id=?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 21, 2007 8:22 am 
Newbie

Joined: Mon Nov 19, 2007 1:57 pm
Posts: 14
maybe someone have link about this question?


Top
 Profile  
 
 Post subject: Children Objects
PostPosted: Wed Nov 21, 2007 4:45 pm 
Newbie

Joined: Thu Nov 15, 2007 1:36 pm
Posts: 12
Location: Ahsburn, VA
Quote:
Question: How I can remove update for child after saving new parent object, or maybe someone know for what this update when save new object?


I'm have a very hard time understanding your English, and consequently the intent of your question; so, my apologies in advance if this misses the mark.


What I think you are asking is: when are children of an entity updated, and do I need to worry about this for performance?

My basic understanding is that if there is a one-to-many association, then if the child collection has become tainted, Hibernate has to synchronize it with insert/update/deletes. If the collection is not tainted, Hibernate is free optimize away the updates. Small sample code cases may not reveal this is actually happening.

In the case of a relationship between two classes, Hibernate should manage the container.

Code:
@Entity
class Stuff {
  ...
  // This stuff has lots of things
  @OneToMany( mappedBy = "parentStuff", cascade = CascadeType.ALL )
  private List<Thing> children = new ArrayList<Thing>();
  ...

  // Accessors
  public List<Thing> getChildren() { return children; }

  private void setChildren( List<Thing> children ) { this.children = children; }

  // Helpers
  public Thing createThing() {
    Thing t = new Thing();
    addThing(t);
    return t;
  }

  public Thing addThing( Thing t ) {
    assert t != null;
    t.setParent(this);  // ...the stuff
    children.add(t);
    return this;
  }
}

@Entity Thing {
  ...
  // This thing belongs to some particular stuff
  @ManyToOne( targetEntity = Stuff.class )
  private Stuff parentStuff;
  ...

  // Accessors (these are called by the parent owning object only)
  public Stuff getParent() { return parentStuff; }
  public void setParent( Stuff parent ) { this.parentStuff= parent; }
}


You'd use the createThing() as a factory on an instance of Stuff. That causes one container object to be managed; you don't want to go replacing container objects out from under Hibernate.

At this point Hibernate does all the optimal things.

-wls


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 22, 2007 1:36 pm 
Newbie

Joined: Mon Nov 19, 2007 1:57 pm
Posts: 14
Hello wls,
thanks for response,
sorry for English,
question is a little different:
Code:
Code:
ParentEntity parent=new ParentEntity();
ChildEntity child=new ChildEntity();

List childList=new ArrayList();
child.setParent(parent);
childList.add(child);
parent.setValues2(childList);

Generate active SQL (i omit sequences):
Code:
insert into A_TEST (id) values (?)
insert into A_TEST_CHILD (PARENT_ID, CHILD_IDX, id) values(?, ?, ?)
update A_TEST_CHILD  set PARENT_ID=?, CHILD_IDX=?  where id=?


I think third statement (update) is unnecessary for that code;

Question: How i can remove third statement for saving new values?
Or explain please why this update required.

Wls, write please if question not clear.


Top
 Profile  
 
 Post subject: Re: How save child list for new object without update?
PostPosted: Tue May 26, 2009 9:14 am 
Newbie

Joined: Fri Apr 18, 2008 11:26 am
Posts: 11
Location: London
Use inverse="true" at the Parent end mapping and all should be fine :-)

_________________
Kumar


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