6 Replies Latest reply on Jul 31, 2008 7:42 AM by cagdas tatlici

    embedded objects

    cagdas tatlici Newbie

      Hi all,

      Can't we version the *toOne relations in an embedded object or am I doing something wrong?

      Or is there a workaround?

      Thanks, Cagdas

        • 1. Re: embedded objects
          Adam Warski Master

          Hello,

          can you have relations in embedded objects at all, in JPA?. Javadoc for @Embeddable (http://java.sun.com/javaee/5/docs/api/) says:


          Only Basic, Column, Lob, Temporal, and Enumerated mapping annotations may portably be used to map the persistent fields or properties of classes annotated as Embeddable.


          Adam

          • 2. Re: embedded objects
            cagdas tatlici Newbie

            Hello,

            Well, actually I got a little bit confused. I didn't know that javadoc said so, but we are able to use @JoinColumn and @ManyToOne annotations successfully in the embeddable objects.

            To make it clear,


            @Embeddable
            public class Address {
            
             @JoinColumn(name = "CITY_ID")
             @ManyToOne
             private City city;
            
             @Column
             private String streetName;
            
            ...
            }
            


            @Entity
            public class Company {
            
             @Id
             @Column
             private Long id;
            
             @Column
             private String name;
            
             @Embedded
             private Address address
            
            }
            


            Any idea?

            Thanks, Cagdas

            • 3. Re: embedded objects
              Adam Warski Master

              Hello,

              so it appears that Hibernate allows relations in embedded objects (which in Hibernate are components), while JPA doesn't - and I followed the JPA specification.

              I looked at the possibility of adding it, but it's not that straightforward. The problem is that when there is a relation in a component, the generated schema for the versions table contains only the id of the related entity (it can't contain simply a relation, as this would make it impossible to read that historic row if the entities are removed). And hence, the mapping doesn't correspond anymore to the component class - as the class contains a related object, not only its id.

              A solution would be to use < properties > instead of < component > when generating a mapping, but then, nested < properties > seem not to work (and I use < properties > to map relations).

              To sum up, I guess it would be possible to make it work, but with some effort. Unfortunately I'm away for the next two weeks so I won't be able to fix that quickly.

              But nevertheless, you can create a JIRA issue if you'd like that implemented.

              Adam

              • 4. Re: embedded objects
                cagdas tatlici Newbie

                Hi,

                You are right, hibernate supports the associations in the embedded objects unlike jpa as stated here http://opensource.atlassian.com/projects/hibernate/browse/ANN-226

                Thanks for your recommendation, but it won't be possible to discard the components at this point of our project. I will try to figure out another workaround.

                Cagdas

                • 5. Re: embedded objects
                  Adam Warski Master

                  Hello,

                  ok, if you can, please write how you solved the problem. If you don't manage to, create a JIRA, and I'll try solving it after I come back. It may be possible to do by creating a special case, which would handle relations in components.

                  --
                  Adam