3 Replies Latest reply on Apr 7, 2005 7:17 PM by Emmanuel Bernard

    Persisting an embedded object that contains a collection of

    Chris Richardson Newbie

      I have an embedded object that contains a collection of entities:

      @Entity(access=AccessType.FIELD)
      class Foo {
      @Embedded
      private Bar bar;
      }
      
      @Embeddable(access=AccessType.FIELD)
      class Bar {
       private Collection<Baz> baz
      }
      
      @Entity
      class Baz {}


      I want the following mappings:
      * class Foo to table Foo
      * class Baz to table Baz
      * The one-to-many should be mapped to a foreign key in table baz to table Foo
      * Class Baz is essentially mapped to table Foo (although it doesn't have any fields)

      I have two questions:
      1. What annotations should I write according to the EJB 3.0 spec?
      2. Does JBoss eJB 3.0 preview 4 support it?

      Thanks


        • 1. Re: Persisting an embedded object that contains a collection
          Emmanuel Bernard Master

          This is not supported by the EJB3 spec. But the preview 5 should allow you to write something like that

          @Embeddable(access=AccessType.FIELD)
          class Bar {
           @OneToMany
           @JoinColumn(name="foo_id")
           private Collection<Baz> baz
          }


          Note that this kind of mapping is not recommanded. Use a join table for such unidirectional one to many.

          • 2. Re: Persisting an embedded object that contains a collection
            Chris Richardson Newbie

             

            "epbernard" wrote:
            This is not supported by the EJB3 spec. But the preview 5 should allow you to write something like that

            @Embeddable(access=AccessType.FIELD)
            class Bar {
             @OneToMany
             @JoinColumn(name="foo_id")
             private Collection<Baz> baz
            }


            Note that this kind of mapping is not recommanded. Use a join table for such unidirectional one to many.


            Thanks for the suggestion.

            Is this this explicitly disallowed in the EJB3 spec?
            I couldn't really see anything.

            Please could you explain why you recommend a join table instead of a foreign key mapping.

            • 3. Re: Persisting an embedded object that contains a collection
              Emmanuel Bernard Master

               

              "ceracm" wrote:
              Is this this explicitly disallowed in the EJB3 spec?
              I couldn't really see anything.

              Please could you explain why you recommend a join table instead of a foreign key mapping.

              The spec restrict the allowed mapping annotations in a embeddable object AFAIR.

              99% of the @OneToMany cases should use a bidirectional mapping at the object level. If for some good reasons it is explicitly unwanted, then these reasons should encourage you to decouple the 2 entities (from a DB POV) through a join table.