1 Reply Latest reply on Jun 30, 2002 3:11 PM by Dain Sundstrom

    Can an EntityBean store a collection as an attribute?

    Alice Newbie

      I have an entity bean Documentbean. Each document can have many "comments". I want to model all these comments in an entity bean CommentsBean. So even though a document can have multiple comments, it will be represented by a one-to-one relationship between DocumentBean and CommentsBean. Now each CommentsBean (primary key will be the document id) will store a collection of comments (each comments has no other information associated so it doesn't make sense to model it as a separate entity bean).

      The design startegy is getting confusing for me because from database point of view, it will make sense to have two tables; one for Document and the other for Comments and have a one-to-many relationship. But from EJBs point of view, modeling "Comment" as a separate entity bean makes no sense to me as comapred to modeling the "collection of comments" as one entity bean.

      My questions are:

      1. Is it a good design choice?
      2. If yes, how do I store a "collection" in an entitybean? How will my setter and getter methods look like? (Remember each record in "Comments" table has a "collection" of comments).

      Thanks so much.

        • 1. Re: Can an EntityBean store a collection as an attribute?
          Dain Sundstrom Master

          > My questions are:
          >
          > 1. Is it a good design choice?

          No. Using a 1:N relationship and storing the comments in a separate table is a much better choice.

          > 2. If yes, how do I store a "collection" in an
          > entitybean? How will my setter and getter methods
          > look like? (Remember each record in "Comments" table
          > has a "collection" of comments).

          This is the reason why it is better to use a separate table. If you want to store a collection in an entity, the cmp engine will serialize the collection and store it into the database in serialized form. This means that only a Java virtual machine can understand that data in your database, an you can't query against the comments using SQL or EJB-QL.

          There are a lot of issues with the long term storage of serialized Java objects, and this is why Sun is moving towards XML for this job.