8 Replies Latest reply: Feb 15, 2012 10:42 AM by 909774 RSS

    Problem with JPA @Embeddable class field in  @Embedded

    909774
      I have a Embeddable class and Embadded in all of my JPA Entities, the version(in DBUniqueId) field is mandatory for some JPA Entities and for others it should not be present.

      My DBUser.java JPA Entity shouldn't have version field(DBUser table doesn't have one) and so I didn't include it in @AttributeOverrides of DBUser.java. when I try to run the JPA query it results in exception

      Query:
      List<DBUser> userList = (List<DBUser>)entityManager.createQuery(SELECT u FROM DBUser u WHERE u.userId.root=?1 AND u.userId.extension=?2).setParameter(1,"root1").setParameter(2,"ext1").getResultList ();

      And The Exception is:
      ---- Begin backtrace for Nested Throwables
      java.sql.SQLException: ORA-00904: "T0"."VERSION": invalid identifier

           at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:74)
           at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:131)

      How do I inform to JPA Entity to ignore only the version field of the Embeddable in my JPA Entity @Embedded place.
      So the above query can successful. Thank You!

      Please guide me to solve this problem.


      @Embeddable
      @MappedSuperclass
      public class DBUniqueId implements Serializable {

      /**
      * serialVersionUID.
      */
      private static final long serialVersionUID = 8903598739796209331L;
      /**
      * root.
      */
      private String root;
      /**
      * extension.
      */
      private String extension;
      /**
      * version.
      */
      private String version;

           //Getters & Setters

      }



      @Entity
      @Table(name = "MyTable", schema = "MySchema")
      public class DBUser implements Serializable {
      /**
      * unique user id.
      */
      private DBUniqueId uniqueUserId;
      /**
      * Get the unique user id.
      *
      * @return <code>DBUniqueId</code> - unique person id
      */
      @Embedded
      @AttributeOverrides( {
      @AttributeOverride(name = "root", column = @Column(name = "LUSR02_ID_ROOT", nullable = true, length = 50)),
      @AttributeOverride(name = "extension", column = @Column(name = "LUSR02_ID_EXT", nullable = true, length = 50))
      })
      public DBUniqueId getUserId () {
      return uniqueUserId;
      }
      /**
      * set user unique id.
      *
      * @param uniqueId - unique id of the person
      */
      public void setUserId (final DBUniqueId uniqueId) {
      this.uniqueUserId = uniqueId;
      }

      }
        • 1. Re: Problem with JPA @Embeddable class field in  @Embedded
          gimbal2
          In DbUser use a different embeddable object without the version.
          • 2. Re: Problem with JPA @Embeddable class field in  @Embedded
            rukbat
            Moderator Action:
            This post has been moved from APIs--> Concurrency
            to Enterprise Javabeans
            for closer topic alignment.

            Moderator Comment
            @ O.P.,
            Go back and read the OTN Forum's FAQ, in particular with respect to how code needs to be formatted within your post.
            https://wikis.oracle.com/display/Forums/Forums+FAQ
            Then go back and re-edit your post for better readability.
            • 3. Re: Problem with JPA @Embeddable class field in  @Embedded
              909774
              Hello gimbal2,

              Thanks for your response...

              In my project I have created a class with name DBVersionUniqueId but my lead said no to it.

              Only DBUniqueId should be used and make it work with or with out version fields.

              I tried experimenting around, but didn't succeed.

              Please Suggest some solution to make it work with DBUniqueId class.

              Thank You.
              • 4. Re: Problem with JPA @Embeddable class field in  @Embedded
                gimbal2
                Sudeep Naidu wrote:
                Hello gimbal2,

                Thanks for your response...

                In my project I have created a class with name DBVersionUniqueId but my lead said no to it.

                Only DBUniqueId should be used and make it work with or with out version fields.

                I tried experimenting around, but didn't succeed.

                Please Suggest some solution to make it work with DBUniqueId class.

                Thank You.
                No, let your lead write the code. He/she seems to be the expert. I certainly have no clue how to do it and honestly I also cannot find any reason why it should be possible. Let me put what is required in other words:

                - you have an entity with three properties, all of which are not transient. This means these properties map to three columns in the database according to the JPA specs.
                - you have a table with only two properties
                - you must and you shall use the object with three properties but at the same time one of the fields must all of a sudden be ignored by the persistence provider

                It makes no sense to want to do that.

                Correct answer: create a new object with only the two fields so the database and the object model match exactly
                Wrong answer: try to create workarounds that in six months time nobody is going to understand why the hell it was done that way
                • 5. Re: Problem with JPA @Embeddable class field in  @Embedded
                  r035198x
                  If you are using hibernate you could try overriding the SQL used for DBUser's CRUD operations:

                  http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-hibspec-customsql
                  • 6. Re: Problem with JPA @Embeddable class field in  @Embedded
                    gimbal2
                    r035198x wrote:
                    If you are using hibernate you could try overriding the SQL used for DBUser's CRUD operations:

                    http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-hibspec-customsql
                    Queue in workaround #1 for which nobody is going to understand in six months time why it was done that way.
                    • 7. Re: Problem with JPA @Embeddable class field in  @Embedded
                      r035198x
                      gimbal2 wrote:
                      r035198x wrote:
                      If you are using hibernate you could try overriding the SQL used for DBUser's CRUD operations:

                      http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-hibspec-customsql
                      Queue in workaround #1 for which nobody is going to understand in six months time why it was done that way.
                      Hibernate overrides to deal with legacy systems and structures are not uncommon in corporate world practice where legacy changes come at a big cost and risk. If Hibernate didn't have many of those overrides then its use would not have been so widespread in the corporate world.

                      I actually don't know if that override would work for the OP's scenario but I would give it a try if changing legacy was not an option.
                      • 8. Re: Problem with JPA @Embeddable class field in  @Embedded
                        909774
                        Hi,

                        I am not sure weather, you understood my post or not, just summarizing it in short.

                        First of all my DBUniqueId.java is not JPA entity, it is Embeddable or MappedSuperclass having three properties.

                        And DBUser.java is a JPA entity and it overrides only two leaving version like below.
                        @Embedded
                        @AttributeOverrides( {
                        @AttributeOverride(name = "root", column = @Column(name = "LUSR02_ID_ROOT", nullable = true, length = 50)),
                        @AttributeOverride(name = "extension", column = @Column(name = "LUSR02_ID_EXT", nullable = true, length = 50))
                        })
                        public DBUniqueId getUserId () {
                        return uniqueUserId;
                        }

                        When i try to query on DBUser.java JPA entity, it throws exception.
                        My understanding is that since DBUser.java is not overriding attribute 'extension' in AttributeOverride annotation, it should not throw exception when I query on DBUser entity.


                        For this problem, temporarly I have created DBVersionUniqueId.java and made it work and working around to find solution for the above problem.

                        I will post if I find any. Thank You!