-->
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: Hibernate annotations - child objects not saved
PostPosted: Tue Sep 15, 2009 8:41 am 
Newbie

Joined: Fri Aug 14, 2009 8:06 am
Posts: 19
Hi there,

I created a class (code attached below) that has child elements of the same type. I used CascadeType. All but when I save a newly created object, the child objects are not saved. I did a bit of debugging and discovered some really weird behavior.

I'll list the code here first (I left out all the code that is irrelevant) to facilitate explaining what happens:

Code:
@Entity
@SequenceGenerator(name = "SEQ_DATAITEM", sequenceName = "dataitem_sequence")
public class DataItem implements Comparable<DataItem>, Serializable, DataChangeListener {
   private static final long serialVersionUID = 1L;

   private Long id;
   private DataItem parent;
   private final UniqueList<DataItem> subData = new UniqueList<DataItem>();

   protected DataItem() {
      super(); // Used by Hibernate
   }

   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_DATAITEM")
   public Long getId() {
      return id;
   }

   protected void setId(Long id) {
      this.id = id;
   }

   @ManyToOne
   @JoinColumn(name = "parent_fk")
   public DataItem getParent() {
      return parent;
   }

   protected void setParent(DataItem parent) {
      this.parent = parent;
   }

   @OneToMany(cascade = CascadeType.ALL, mappedBy = "parent")
   public List<DataItem> getAllSubData() {
      return subData;
   }

   protected void setAllSubData(List<DataItem> subData) {
      this.subData.clear();
      this.subData.addAll(subData);
   }

   public DataItem addSubData(String string) {
      DataItem item = getSubData(string);

      if (item == null) {
         item = new DataItem(this, string);
         subData.add(item);
      }

      return item;
   }

   public DataItem getSubData(String string) {
      DataItem item = null;
      int index = 0;

      while (index < subData.size() && item == null) {
         DataItem subItem = subData.get(index);

         if (subItem.toString().equals(string)) {
            item = subItem;
         }

         index++;
      }

      return item;
   }
}


The test code I execute is the following:

Code:
DataItem dataItem = Data.ACCENT.add("test"); // Just creates a new DataItem

dataItem.addSubData("subdata 1");
dataItem.addSubData("subdata 2");
session.beginTransaction();
session.save(dataItem);
session.getTransaction().commit();

reset(); // closes and reopens the db

session.beginTransaction();
List<DataItem> results = session.createQuery("from DataItem").list();

Assert.assertEquals(1, results.size());

dataItem = results.get(0);
Assert.assertSame(Data.ACCENT, dataItem.getRootData());
Assert.assertEquals("test", dataItem.getData());
Assert.assertEquals(2, dataItem.getAllSubData().size());

for (DataItem subItem : dataItem.getAllSubData()) {
   Assert.assertSame(subItem.getParent(), dataItem);
   Assert.assertTrue(subItem.getData().startsWith("subdata"));
}


Now, I set a breakpoint in the DataItem.setAllSubData() method and what happens is that, when I clear this.subData, the parameter that is passed is being cleared too although they are different objects. By the way, this happens during the save operation.

Is there anyone who has an idea on how to tackle this? By the way, UniqueList is a list that won't allow duplicates.

Thanks in advance,
Stef


Top
 Profile  
 
 Post subject: Re: Hibernate annotations - child objects not saved
PostPosted: Fri Sep 18, 2009 7:09 am 
Newbie

Joined: Fri Aug 14, 2009 8:06 am
Posts: 19
Well, saving the subitems solves the problem. This solution is workable for me although I'd still like to figure out how the entire object could be saved. I might need this later on.

Cheers,
Stef


Top
 Profile  
 
 Post subject: Re: Hibernate annotations - child objects not saved
PostPosted: Fri Sep 18, 2009 7:28 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Saving both sides of the association will be necessary the first time the data is being saved.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.