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?