[
http://jira.jboss.com/jira/browse/HIBERNATE-50?page=comments#action_12351301 ]
Lajos Gathy commented on HIBERNATE-50:
--------------------------------------
I ran into this problem as well and I think it would be nice to do some improvements on
this issue. Here is my example:
@Embeddable
public class EAttrs implements Serializable
{
private Float x;
private Calendar y;
private String z;
/* getters and setters come here - with mapping annotations, neither of the attributes is
mandatory */
@Transient
public Boolean checkSomething() { /* do some checking on the attributes */ }
@Transient
public String calculateSomething() { /* calculate some data from the attribute values */
}
}
@Entity
public class SomeEntity implements Serializable
{
private String something;
private EAttrs someAttributes = new EAttrs();
@NotNull
public EAttrs getSomeAttributes() { return someAttributes; }
/* the rest of the getters and setters comes here */
}
In this example all attributes of EAttrs can be null. But in SomeEntity I annotate the
'someAttributes' property with @NotNull, because I need that object existing
always to be able to invoke its property getters or transient methods without checking
whether the embedded object is null every time, even if all its attributes are null. But
after fetching an entity - lets call it 'e1' - someAttributes will be null if all
of its attributes were null and I cannot invoke e1.getSomeAttributes().getX() or
e1.getSomeAttributes().checkSomething().
An other problem comes when I change the 'something' attribute of the 'e1'
and I try to merge it back to the persistence context, because 'someAttributes'
will be null and the entity will fail the not-null validation of that attribute.
My proposal is to add a boolean 'mandatory' field to @Embedded annotation and if
it is set to true, then the embedded object should be constructed automatically during
loading the entity when all its attributes are null. A better way would be to do this if
the @NotNull annotation is present on the embedded attribute. What do you think about
this?
Embedded Objects get set to null if all its members are null
------------------------------------------------------------
Key: HIBERNATE-50
URL:
http://jira.jboss.com/jira/browse/HIBERNATE-50
Project: Hibernate
Issue Type: Bug
Environment: WinXP SP2, JBoss 4.0.4 GA as well as JBoss 4.0.5 GA, installation
profile ejb3
Reporter: milan w?lke
Assigned To: Steve Ebersole
When having an embedded object only containing members of non-simple-types (e.g. String)
and fetching it with the entity manager the following happens:
If all columns, the embedded objects values are based on, are null the embedded object
itself is set to null even if a non-arg-constructor set it to a non-null value before. A
valid workaround is to include a member of a simple-type (e.g. int) in the embedded
object. Doing this not all the values can be set to null, which results in not setting the
embedded object itself to null, too.
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://jira.jboss.com/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira