1 Reply Latest reply on Jan 2, 2008 8:53 AM by Todd Gould

    Confused EJB3 Beginner - Ignored EJB3 Annotations

    Todd Gould Novice

      I'm sure this is a simple beginner error, but I've been pouring over both the JBoss and Hibernate docs and wikis and and am not finding the key missing points.

      I'm using JBoss 4.2.2.GA with it's included hibernate3. I'm attempting to build EJB3 standard Entities. I have several classes that are annotated with the javax.persistence.Entity anotation (@Entity) as well as the associated @Column, @Id, etc. All of the annotations I use are from the javax.persistence package (i.e. I'm not using any hibernate or JBoss specific ones). My classes are built into a jar with an associated persistence.xml file which maps to my Datasource (a PostgreSQL one) and sets the hibernate.hbm2ddl.auto property to create. This jar fle is then bundled together with another MDB bean in an ear and deployed.

      It all deploys fine and associated table are created. However, it appears that my Annotations are ignored! It seems that hibernate is reflecting upon the classes in question and building it's own view of them for persistance purposes. Specifically, the @Column name and length attributes are NOT as coded in my @Column annotations - they are based upon the actual class attribute name and what must be default lengths. Additionally, the @Enumerated (EnumType.STRING) annotation is similiarly ignored as the created DB tables have integer types.

      I've been looking at many different version of documentation as EJB3 support within both hibernae and JBoss have evolved. Yet, I'm fundamentaly missing something. Hence I'm starting to question my understanding.

      Is there some configuration that must be done to cause hibernate to honor the Annotations in preference to it's reflect and/or to disable it's reflection? Is it sufficient to have just EJB3 annotated classes with a persistence.xml file or are other configurations and/or files still required in and EJB3 environment - the book I've been working from, 'EJB3 In Action', seems to indicate that this is all it takes.

      Are the hbm.xml and/or hibernate config files still required? If so, can they be built from the Anotations? How? It seems that a HAR based deployment once was preferable, but perhaps no longer is? I've additonally added a hibernate-service.xml service module in the ear's jboss-app that refernces the org.jboss.hibernate.jmx.Hibernate mbean. This made no difference in the ignored annotations behavior? Is this approach still needed/relavant?

      Any and all guidence to a bewildered EJB3 beginner will be greatly apreciated!

      TIA!

        • 1. Re: Confused EJB3 Beginner - Ignored EJB3 Annotations
          Todd Gould Novice

          Aha! It turns out that my Annotations are not being ignored - I just did not understand that I can (apparently) not mix both field and method level Annotations.

          My class has several read-only attributes (i.e. there is no associated set methods). In general, I would prefer to put my Annotations on the method level for code clarity sake. However, when doing this for the read-only attributes, I got an error complaining that a setter could not be found. This error message went away when I moved those Annotations to the field. Apparently though, this had the undersired effect of making all attributes 'field' access and effectively ignored all of my other method-based Annotations for columne name, length, etc.

          So, my questions now are:

          1. Is there an EJB3 standard way of mixing both field and method access types? I would like to have my default for method, but be able to override in exception cases such as the read-only attribute discussed above.

          2. Is there a better way to handle read-only attributes that do NOT have an associated setter?

          TIA!