[jboss-jira] [JBoss JIRA] Commented: (EJBTHREE-686) Bidirectional @OneToOne with CascadeType.ALL and optional=false on the non-owning side:

Dirk Mahler (JIRA) jira-events at lists.jboss.org
Mon Nov 16 04:53:30 EST 2009


    [ https://jira.jboss.org/jira/browse/EJBTHREE-686?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12494793#action_12494793 ] 

Dirk Mahler commented on EJBTHREE-686:
--------------------------------------

The problem still exists with Hibernate Core 3.3.2.GA and Hibernate Entity Manager 3.4.0.GA. Adding CascadeType.PERSIST on both sides of the relation does not help. I think this issue should be re-opened.

> Bidirectional @OneToOne with CascadeType.ALL and optional=false on the non-owning side:
> ---------------------------------------------------------------------------------------
>
>                 Key: EJBTHREE-686
>                 URL: https://jira.jboss.org/jira/browse/EJBTHREE-686
>             Project: EJB 3.0
>          Issue Type: Bug
>          Components: EJB3 Extensions
>    Affects Versions: EJB 3.0 RC6 - PFD, EJB 3.0 RC7 - FD, EJB 3.0 RC8 - FD
>            Reporter: Jeremy Norris
>            Assignee: Emmanuel Bernard
>
> Consider the following two classes:
> ----- class X:
> @Entity
> @Table(name="x")
> public class X
> {
>     private Integer id;
>     private Y y;
>     
>     public X()
>     {
>         this.y = new Y(this);
>     }
>     @Id
>     @GeneratedValue
>     @Column(name = "id", nullable=false, updatable=false)
>     public Integer getId()
>     {
>         return id;
>     }
>     protected void setId(Integer id)
>     {
>         this.id = id;
>     }
>     @OneToOne(cascade={CascadeType.ALL}, mappedBy="x")
>     public Y getY()
>     {
>         return y;
>     }
>     protected void setY(Y y)
>     {
>         this.y = y;
>     }
> }
> ----- class Y:
> @Entity
> @Table(name="y")
> public class Y
> {
>     private Integer id;
>     
>     private X x;
>     
>     protected Y()
>     {
>     }
>     public Y(X x)
>     {
>         this.x = x;
>         x.setY(this);
>     }
>     @Id
>     @GeneratedValue
>     @Column(name = "id", nullable=false, updatable=false)
>     public Integer getId()
>     {
>         return id;
>     }
>     protected void setId(Integer id)
>     {
>         this.id = id;
>     }
>     @OneToOne
>     @JoinColumn(name="x_id", nullable=false)
>     public X getX()
>     {
>         return x;
>     }
>     protected void setX(X x)
>     {
>         this.x = x;
>     }
> }
> ----- X is persisted as follows:
> // Note: The constructor creates a new dependent Y instance, and hooks it up appropriately:
> X x = new X();
>         
> em.persist(x);
> -----
> This works fine and persists as expected.  However, if I add "optional=false" to the X. at OneToOne declaration, it fails with the following exception:
> javax.ejb.EJBTransactionRolledbackException: javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: ... Y.x
> Y.x definitely set.  Also, "x == x.getY().getX()" is true.  (Interestingly, this only happens if I put the "optional=false" on the X side.  "optional=false" on the Y side is fine).  This seems like a bug unless there is something I don't understand about "optional", but it seems pretty simply - The spec says the following about this attribute: 
> (Optional) Whether the association is optional.
> If set to false then a non-null relationship must
> always exist.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://jira.jboss.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the jboss-jira mailing list