2 Replies Latest reply on Feb 10, 2006 9:46 AM by Peter Westlin

    Generic MappedSuperclass

    dunks80 Newbie

      Is there anyway to use a generic class as a mappedsuperclass? I have an abstract class Shipment which looks something like this...

      @MappedSuperclass
      
      public abstract class Shipment<ShipmentItemType extends ShipmentItem> extends Entity
      
      {
      
      
      
       protected List<ShipmentItemType> shippedItems;
      
       @OneToMany( fetch = FetchType.LAZY, mappedBy = "shipment", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
      
       public List<ShipmentItemType> getShippedItems()
      
       {
      
      
       return shippedItems;
      
       }
      
      
      
      
      
       public void setShippedItems(List<ShipmentItemType> shippedItems)
      
       {
      
       this.shippedItems = shippedItems;
      
       }
      }
      
      


      then i have a subclass ColorBreakShipment that looks something like this...

      @Entity
      @Table(name = "colorBreakShipment")
      public class ColorBreakShipment extends Shipment<ColorBreakShipmentItem>
      {
       /**
       *
       */
      
       private Manufacturer manufacturer;
      
       // attributes
       public ColorBreakShipment()
       {
       super();
       }
      
       @ManyToOne(fetch = FetchType.LAZY)
       @JoinColumn(name = "manufacturerId")
       public Manufacturer getManufacturer()
       {
       return manufacturer;
       }
      
       public void setManufacturer(Manufacturer manufacturer)
       {
       this.manufacturer = manufacturer;
       }
      
       @OneToMany(targetEntity=ColorBreakShipmentItem.class, fetch = FetchType.LAZY, mappedBy = "shipment", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
       public List<ColorBreakShipmentItem> getShippedItems()
       {
       return super.getShippedItems();
       }
      
      
       public void setShippedItems(List<ColorBreakShipmentItem> shippedItems)
       {
       super.setShippedItems(shippedItems);
       }
      
      
      
      }
      


      However when I try to test the class i get the following exception...
      ...
      org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity() defined: entity.ColorBreakShipment.shippedItems
      ...
      


      I even tried defining the get and set ShippedItems method in the super class as abstract methods but that didn't work either.

      Does anyone know if there is anyway to use the generic class as a mapped superclass or do I just have to duplicate the fields in all "Shipment" classes



        • 1. Re: Generic MappedSuperclass
          dunks80 Newbie

          I swear posting a question to the forum is the fastest way to guarantee you'll answer it yourself in five minutes...

          I was able to perform that mapping if i first declared the getters and setters for the ShippedItems attribute in a generic interface which my generic abstract class implemented and then my concrete class extended .. of course I've only run my unti tests against it and not done any extensive queries on the object so other problems may present themselves but it seems to work ok for now in case anyone else ever runs into the same problem

          • 2. Re: Generic MappedSuperclass
            Peter Westlin Newbie

            Answering your own questions isn't that bad you know. I do it all the time at the Webwork forum... :-)
            To be serious, it is much better to answer your own questions, if you do find the answer yourself, rather then leaving them unanswered.

            So, keep on answering! :-)

            Regards Peter