-->
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: update problem?
PostPosted: Tue Nov 25, 2003 10:07 am 
Beginner
Beginner

Joined: Thu Nov 20, 2003 12:29 pm
Posts: 39
Hey folks,

i have a parent class with a one-to-many-relationship to a child class!

i initialise an instance of the parent class ,open inside this class a session and save it
session.save(this);

then i add a child
addChild(child); (like in the hibernate 2 reference)

and then open again a session inside the parent and update the parent
session.update(this);

-> then i have 2 parents and 1 child in my database instead of 1 parent and one child!

anybody know, what i'm doing wrong?

Johannes


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 10:12 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Show us the code and the hibernate logs

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 10:28 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Do you use composite ID ? Then override equals() and hashCode().

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 11:08 am 
Beginner
Beginner

Joined: Thu Nov 20, 2003 12:29 pm
Posts: 39
no use not composite id's

the JasBean is the parent, the KeyValueTuple the child:
but i said something wrong, i call JasBean::set() two times (see source), and in the end 1 JasBean is persistent and 3 KeyValueTuples

that means everytime, when i update the JasBean, all KeyValueTuples related to the JasBean are save with new id's in the database
(add first - 1 is saved
add second 2 are saved - at all 3 are there
)

(i just put in the relations, id and the hybernate methods)

JasBean:
Code:
...
private Set keyValueTuples = new HashSet();
private long uniqueSequence;
...
   /**@hibernate.id  generator-class="native" type="long" 
   */
   private Long getUniqueSequence() {
      return uniqueSequence;
   }

   private void setUniqueSequence(Long puniqueSequence) {
      uniqueSequence = puniqueSequence;
   }
...
public void set(String key, String value) {
      
   try {

      KeyValueTuple keyValueTuple = new KeyValueTuple();
      keyValueTuple.setKey(key);
      keyValueTuple.setValue((String) value);

      if (keyValueTuples.isEmpty()) {
         save(this);
      }
      addKeyValueTuple(keyValueTuple);
                update(this);

      } catch (Exception e) {
      }
}
//RELATIONS
/**
    *
    * @hibernate.set role="KeyValueTuples"
    *                lazy="false"
    *                cascade="all"
    *                inverse=true"
    * @hibernate.collection-key column="fkJasBean"
    * @hibernate.collection-one-to-many class="org.weta.jas.hibernate.KeyValueTuple"
    */

   public Set getKeyValueTuples() {
      return keyValueTuples;
   }

   public void setKeyValueTuples(Set pset) {
      keyValueTuples = pset;
   }

   public void addKeyValueTuple(KeyValueTuple pkeyValueTuple) {
      pkeyValueTuple.setJasBean(this);
      keyValueTuples.add(pkeyValueTuple);
   }


KeyValueTuple:
Code:
...
private long uniqueSequence;

   /**@hibernate.id  generator-class="native" type="long" */
public long getUniqueSequence() {
      return uniqueSequence;
   }

   public void setUniqueSequence(long puniqueSequence) {
      uniqueSequence = puniqueSequence;
   }

...
//RELATIONS
/**
    * @hibernate.many-to-one class="org.weta.jas.hibernate.JasBeanHiber"
    *                        cascade="all"
     *                        column="fkJasBean"
     *                        not-null="true" 
    */
   public JasBeanHiber getJasBean() {
      return jasBean;
   }

   public void setJasBean(JasBeanHiber pjasBean) {
      jasBean = pjasBean;
   }


The hibernate methods :
Code:
public void save(Object object) throws Exception {

      openSession();
      session.save(object);
      closeSession();
   }

public void update(Object object) throws Exception {

      openSession();
      session.update(object);
      closeSession();

   }

private void openSession() throws Exception {

      
      sessionFactory = new Configuration().configure().buildSessionFactory();
      session = sessionFactory.openSession();
   }

   private void closeSession() throws Exception {

      session.close();
   }


bye


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 11:43 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Sorry but I can't get your pb.

Can you show :
* the caller code
* hibernate logs
* your DB state a the end.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 11:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Hi,

I think your id-Properties are the problem. This looks strange to me anyways, sometimes you use Long in getters/setters and long as class properties ... how does this even compile?

Try using Long as properties everywhere, and see if it works out. Using primitive types as id-properties has caused me some grevieance allready.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 11:52 am 
Beginner
Beginner

Joined: Thu Nov 20, 2003 12:29 pm
Posts: 39
yes, sorry, i'm a little bit confused in moment and mixed up different "code-versions".
I try to get this right, and then i call again!

thanks for now


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.