5 Replies Latest reply on Feb 8, 2006 10:48 PM by Chris Malan

    Entity Beans extending a POJO - will I ever get this to work

    Peter Russell Newbie

      Since EJBs are now POJOs I would have expected that I could define an Entity Bean that extended a base class with standard methods.

      I have been unable to achieve this and get a nnumber of exceptions regarding overiding ID properties among others.

      Is this something that should work, or am I never going to get it to work?

      thanks in advance

      Pete

        • 1. Re: Entity Beans extending a POJO - will I ever get this to
          Alexander Hartner Expert

          Could you be a bit more specific what you are trying to override and what exceptions you are getting. I have implemented inheritance from POJO's, but I also had problems inheriting annotations. Basic services worked fine.

          • 2. Re: Entity Beans extending a POJO - will I ever get this to
            Peter Russell Newbie

            Here is the Base Class

            public class BaseDeptDTOImpl implements Serializable {
            
             private static final Log log = LogFactory.getLog(BaseDeptDTO.class);
            
            
             /** Primary key. */
             private int deptId;
            
             /**
             * auto_increment
             *
             * Get the primary key.
             *
             * @return The primary key.
             */
             @javax.persistence.Id @javax.persistence.GeneratedValue(strategy=javax.persistence.GenerationType.AUTO)
             @javax.persistence.Column(name = "dept_id")
             public int getDeptId() {
             return deptId;
             }
            
             /**
             * auto_increment
             *
             * Set the primary key.
             *
             * @param deptId The primary key.
             */
             public void setDeptId(int deptId) {
             this.deptId = deptId;
             }
            
             /** Regular field. */
             private java.lang.String deptName;
            
             /**
             * Get the deptName.
             *
             * @return The deptName.
             */
             @javax.persistence.Column(name = "dept_name")
             public java.lang.String getDeptName() {
             return deptName;
             }
            
             /**
             * Set the deptName.
             *
             * @param deptName The deptName.
             */
             public void setDeptName(java.lang.String deptName) {
             this.deptName = deptName;
             }
            
             /** Association. */
             private java.util.Collection<tv.probability.referenceimplementation.dto.EmpDTO> emps;
             /**
             * Get the emps.
             *
             * @return The emps.
             */
             @javax.persistence.OneToMany(cascade = javax.persistence.CascadeType.ALL, fetch = javax.persistence.FetchType.EAGER)
             @javax.persistence.JoinColumn(name = "dept_id")
             public java.util.Collection<tv.probability.referenceimplementation.dto.EmpDTO> getEmps() {
             return emps;
             }
             /**
             * Set the emps.
             *
             * @param emps The emps.
             */
             public void setEmps(java.util.Collection<tv.probability.referenceimplementation.dto.EmpDTO> emps) {
             this.emps = emps;
             }
            }
            


            and here is the extending class

            @javax.persistence.Entity
            @org.hibernate.annotations.Entity(dynamicInsert = true)
            @javax.persistence.Table(name = "dept")
            public class DeptDTOImpl extends BaseDeptDTOImpl implements DeptDTO {
             private static final Log log = LogFactory.getLog(DeptDTOImpl.class);
            
             /** Primary key. */
             private int deptId;
            
             /**
             * auto_increment
             *
             * Get the primary key.
             *
             * @return The primary key.
             */
             @javax.persistence.Id @javax.persistence.GeneratedValue(strategy=javax.persistence.GenerationType.AUTO)
             @javax.persistence.Column(name = "dept_id")
             public int getDeptId() {
             return deptId;
             }
            
             /**
             * auto_increment
             *
             * Set the primary key.
             *
             * @param deptId The primary key.
             */
             public void setDeptId(int deptId) {
             this.deptId = deptId;
             }
            }
            


            This compiles and runs, but the inherited deptName is not persisted to the database, it is ommited from the SQL entirely.
            If I add the entity annotations to the Base class:

            @javax.persistence.Entity
            @org.hibernate.annotations.Entity(dynamicInsert = true)
            @javax.persistence.Table(name = "dept")
            

            I get the exception:

            FATAL 05-02 09:00:24,776 (PersistenceXmlLoader.java:parsePersistenceUnit:101) -refImplDB JTA
            ERROR 05-02 09:00:28,605 (AbstractController.java:incrementState:350) -Error installing to Start: name=persistence.units:jar=embeddable-ejb.jar,unitName=refImplDB state=Create
            org.hibernate.AnnotationException: Unable to define/override @Id(s) on a subclass: tv.probability.referenceimplementation.dto.DeptDTOImpl
            



            btw. I am running this using EMbeddable EJBs and JUnit.

            • 3. Re: Entity Beans extending a POJO - will I ever get this to
              Alexander Hartner Expert

              I don't know if it is a bug, but I was also unable to inherit annotations. In my case I tried to inherit the @Version and @Id annotations.

              • 4. Re: Entity Beans extending a POJO - will I ever get this to
                Eugene Onischenko Newbie

                Please read ejb 3 spec. In PFD (chapter 2.1.9.2) it is explicitly written that any annotations on non-entity superclass are ignored.

                You can use @MappedSuperclass annotation to solve the problem.

                • 5. Re: Entity Beans extending a POJO - will I ever get this to
                  Chris Malan Novice

                  Hi Peter,

                  I have successfully extended from a base class in several applications. Other problems crop up so I don't have things too easy, but not this one.

                  You will find the solution here:
                  http://docs.jboss.org/ejb3/app-server/tutorial/singleinheritance/single.html

                  Also, the extended class should have no primary key - the pk is only in the base class.

                  Do use the single table inheritance method, it works well and is amenable to some changes in your inheritance hierarchy later on, should it be needed.

                  Hope this helps.