11 Replies Latest reply on Aug 18, 2006 3:09 AM by snau2005

    My collection is not initializing; due to inheritance?

    smokingapipe

      I have a Person class, which keeps a List of MailingAddress objects. There is a sub-class of Person called Customer which adds a few fields. The MailingAddress itself is a sub-class of an Address object. Person has @OneToMany annotation on its MailingAddress collection, and Address has a @ManyToOne annotation on its Person member.

      When I use a PersistenceManager to load up a Customer from the DB, the list of MailingAddresses is always empty.

      Is this type of use supported? When I create a new Customer, I also create a new MailingAddress. I set the MailingAddress in the Customer and then I persist the Customer, and I can see it is all saved in the db. But when I load the Customer back, it gives me an empty List. I think it's not even a proxy; it's just a plain old Java List.

      I know there are two inheritances happening here: Customer is a subclass of Person, and MailingAddress is a subclass of Address. Should this be working?

      Thanks

        • 1. Re: My collection is not initializing; due to inheritance?
          smokingapipe

          I tried again and removed the inheritance part of it, still no luck in getting any objects in the collection when the object is fetched. This is with cascade=ALL. Do collections work in EJB yet? I have the EJB3 book and it has a whole section on collections but I can't get it to work in my app. Any ideas?

          • 2. Re: My collection is not initializing; due to inheritance?
            smokingapipe

            I tried again with a simple uni-directional mapping.

            Could someone confirm that collections are supported in EJB3? I know they worked in Hibernate 3, and that was great, but none of this seems to work within EJB 3. What is strange is there are example apps. I'll try to see if they work.

            • 3. Re: My collection is not initializing; due to inheritance?
              smokingapipe

              I don't get it. What is the point of having collection support in EJB if it can only save collections, but cannot make them available when the object is fetched from an EntityManager? This makes no sense. I looked back through the Seam examples, and they are good examples of esoteric things, like having conversations within conversations, but none of them deal with simple collections, leading me to wonder how I should handle entities that need to have multiple other objects "within" them? Any ideas?

              • 4. Re: My collection is not initializing; due to inheritance?
                smokingapipe

                I'm even more confused. I put in a check to see what class the List was after the object was fetched. That should be some kind of proxy class, internal to EJB etc. Instead it's a plain old LinkedList. EJB isn't even trying to set a proxy or do anything with that.

                So I'm not sure how to handle one-to-many relationships within my application. If EJB can save them, but not load them, that doesn't help me very much. At least Hibernate was able to both save and load them.

                • 5. Re: My collection is not initializing; due to inheritance?
                  smokingapipe

                  This is completely insane. I'm reading Enterprise JavaBeans 3.0, by Bill Burke, and it has all these examples with collections mapping, but when I actually run this within JBoss, no matter what I do, JBoss simply won't use a proxy for the Collection. What is a good approach for looking into EJB's fetching mechanism to see why it is not fetching the Collection here?

                  • 6. Re: My collection is not initializing; due to inheritance?
                    smokingapipe

                    More info: I just put a logger in my setter for the collection. Of course the setter is never called, so that means that at no point is EJB even trying to install a proxy in there. If anyone can show me an example of mapping a collection that works, and the collection is restored when the object is fetched, I would be quite amazed by that.

                    • 7. Re: My collection is not initializing; due to inheritance?
                      snau2005

                      please post code,
                      do your Person and Address classes have annotaion: @MappedSuperclass ?

                      • 8. Re: My collection is not initializing; due to inheritance?
                        smokingapipe

                        No, they both the abstract Person and Address classes use @Inheritance(strategy=InheritanceType.JOINED) . Will that work? Or should I switch to some other inheritance strategy?

                        I'm getting my code together to post it.

                        • 9. Re: My collection is not initializing; due to inheritance?
                          snau2005

                          I put here my example of 3 tables, i hope you asking of such example (in your case 2 tables)


                          @MappedSuperclass
                          public class IdTable implements Serializable {
                           @Id @GeneratedValue(strategy=GenerationType.TABLE)
                           public Long id;
                          
                          }
                          
                          @MappedSuperclass
                          public abstract class BasicPrimaryTable extends IdTable {
                          
                           @Version public Integer version;
                          }
                          
                          @Entity
                          public class SomeTable extends BasicPrimaryTable {
                          
                          }
                          
                          


                          if you dont want to extend some calss you need put like this:

                          @Embeddable
                          public class SomeClass {
                           @Column(nullable = false, length=255) public String name;
                          }
                          
                          @Entity
                          public class SomeTable {
                          @Embedded private SomeClass some;
                          }
                          



                          • 10. Re: My collection is not initializing; due to inheritance?
                            smokingapipe

                            But could someone confirm that EJB does support mapping a collection, and can load the items into the collection when the object is fetched? I see chapters on that in this book but I don't see any evidence that it is possible.

                            I notice that when I persist my Customer object, the collection is never changed. Upon persistence, the collection should be converted into a proxy, but this obviously does not happen, leading me to wonder if EJB is even capable of doing that. If I were programming it, I would have the EntityManager.persist() method go through and call setters on all the objects it is persisting to replace their Collections with EJB proxies, but it ignores them. Either EJB doesn't support that use, or there is a bug in the EJB implementation.

                            • 11. Re: My collection is not initializing; due to inheritance?
                              snau2005

                              there is fetch strategies:
                              FetchType.EAGER or FetchType.LAZY, default i think lazy

                               @OneToMany(mappedBy = "field", fetch=FetchType.EAGER)
                               @Cascade({CascadeType.ALL, CascadeType.DELETE_ORPHAN})
                               public List<SomeClass> somoes;