-->
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.  [ 2 posts ] 
Author Message
 Post subject: @Embedded property not rehydrating on an entity ....
PostPosted: Mon Sep 17, 2007 7:08 pm 
Beginner
Beginner

Joined: Sun Feb 20, 2005 12:14 am
Posts: 49
Using Hibernate 3.2.2

Problem:
When I save an entity that has an embedded component, without giving any values to the embedded component and then rehydrate the entity; the component is null.

Code:
@Embeddable
public class MyComponent
{
   @Column(columnDefinition="varchar(10)")
   private String text1;
   
   @Column(columnDefinition="varchar(10)")
   private String text2;

   public void setText1(String text1)
   {
      this.text1 = text1;
   }

   public String getText1()
   {
      return text1;
   }

   public void setText2(String text2)
   {
      this.text2 = text2;
   }

   public String getText2()
   {
      return text2;
   }

}

@Entity
public class MyObject
{
   @GenericGenerator(name = "system-uuid", strategy = "uuid")
   @GeneratedValue(generator = "system-uuid")
     @Id
     private String id;

     @Version
     private Long version = null;
 
   private String myInfo;

   @Embedded
     private MyComponent myComponent;
 
   public MyObject()
   {
      myComponent = new MyComponent();
   }
   
   public GoogleOrder(String myInfo)
   {
      myComponent = new MyComponent();      
      
      this.myInfo = myInfo;
   }
   
   public String getMyInfo()
   {
      return this.myInfo;
   }

   public void setMyInfo(String myInfo)
   {
      this.myInfo = myInfo;
   }

   public MyComponent getMyComponent()
   {
      return this.myComponent;
   }
}


Now in my code I do:
Code:
....
MyObject myobj = new MyObject();
myobj.setMyInfo("I am confused");
this.session.saveOrUpdate(myobj);
....
....


At this point I can see that the myobject table has a row persisted to it (the text1 and text2 columns are NULL as expected since I did not set any values on the myComponent property)

Later on in my application, I get this same object that I saved above and try to access the component:

Code:
myobj.getMyComponent().setText1("123 XYZ"); <--------- I get a null pointer exception here.


While tracing through the java debugger I see that the myComponent property in the myobj instance is null.

Question:

Why is this the case (Since the constructor of MyObject initializes the myComponent property)? What am I missing?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 11:20 am 
Beginner
Beginner

Joined: Sun Feb 20, 2005 12:14 am
Posts: 49
In trying to figure this problem out I modified my code a little. Basically when I first create myobj, I store some dummy data in the myComponent property like this:

This:
Code:
....
MyObject myobj = new MyObject();
myobj.setMyInfo("I am confused");
this.session.saveOrUpdate(myobj);
....
....


Became this:
Code:
....
MyObject myobj = new MyObject();
myobj.setMyInfo("I am confused");
myobj.getMyComponent().setText1("Dummy text");
this.session.saveOrUpdate(myobj);
....
....


Later on in my application, I get this same object that I saved above and try to access the component. Now when my code executes:

Code:
myobj.getMyComponent().setText1("123 XYZ");


the myComponent property is no longer null and I no longer get the NullPointerException.

Why is it that the component gets rehyrdated when there is a value specified for one of the component's property, but if I do not specify any value for any of the component's properties, I get a null property back?

Is this a bug or am I doing something wonky?

Thanks.


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