OpenJPA
  1. OpenJPA
  2. OPENJPA-1976

EntityManager.find method cause errors

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Not a Problem
    • Affects Version/s: 2.1.0
    • Fix Version/s: None
    • Component/s: jpa
    • Labels:
    • Environment:
      Glassfish 3.1, JSF 2.1.0, Apache OpenJPA 2.1.0, MySQL 5.5.10, Eclipse Helios IDE, Windows XP Service Pack 3

      Description

      Scenario 1: The EntityManager.find method is unable to pass a String value as its second parameter and caused the following error:

      <openjpa-2.1.0-r422266:1071316 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: The given value "214736890D96ED598E1D3050F1F025A7" cannot be converted into an identity for "com.ckd.model.BookModel". The value is the wrong type (java.lang.String).

      java.lang.NumberFormatException: For input string: "214736890D96ED598E1D3050F1F025A7"

      It seems from the errors above that the EntityManager.find method will only accept a numeric value as its second parameter.

      Scenario 2: The EntityManager.find method is unable to pass a Long value and caused the following error:

      <openjpa-2.1.0-r422266:1071316 fatal user error> org.apache.openjpa.persistence.ArgumentException: An error occurred while processing registered class "class com.ckd.model.BookModel".

      java.lang.IllegalStateException: WEB9031: WebappClassLoader unable to load resource [org.apache.openjpa.util.LongId], because it has not yet been started, or was already stopped

      The columns used for testing are from a MySQL 5.x table and have the following definitions:

      ID SMALLINT(5)
      HASHID VARCHAR(32)

      The value of HASHID is used to test Scenario 1; while the value of ID is used to test Scenario 2.

      Conclusion: The above scenarios show that the EntityManager.find method is currently unusable/buggy and needs to be investigated for the next release.

      1. SCENARIO 2 STACKTRACE.txt
        39 kB
        Chuong Pham
      2. SCENARIO 1 STACKTRACE.txt
        36 kB
        Chuong Pham
      3. ENTITY.txt
        2 kB
        Chuong Pham

        Issue Links

          Activity

          Hide
          Rick Curtis added a comment -

          Can you post the Entity that are are having problems with along with the full exception stacks?

          Thanks,
          Rick

          Show
          Rick Curtis added a comment - Can you post the Entity that are are having problems with along with the full exception stacks? Thanks, Rick
          Hide
          Chuong Pham added a comment - - edited

          Thanks for your prompt response, Rick. I have attached ENTITY.TXT, SCENARIO 1 STACKTRACE.TXT and SCENARIO 2 STACKTRACE.TXT for your perusal. Please let me know if you need further information.

          Thanks
          Chuong

          Show
          Chuong Pham added a comment - - edited Thanks for your prompt response, Rick. I have attached ENTITY.TXT, SCENARIO 1 STACKTRACE.TXT and SCENARIO 2 STACKTRACE.TXT for your perusal. Please let me know if you need further information. Thanks Chuong
          Hide
          Rick Curtis added a comment -

          Chuong -

          Lets start off with the javadoc for javax.persistence.EntityManager.find(Class<T> entityClass, Object primaryKey).


          <T> T find(Class<T> entityClass, Object primaryKey)

          Find by primary key.

          Parameters:
          entityClass -
          primaryKey -
          Returns:
          the found entity instance or null if the entity does not exist
          Throws:
          IllegalStateException - if this EntityManager has been closed.
          IllegalArgumentException - if the first argument does not denote an entity type or the second argument is not a valid type for that entity's primary key

          The last part of the IllegalArgumentException states an exception will be thrown when "the second argument is not a valid type for that entity's primary key". By looking at your BookModel there is one column annotated as the ID column and that column is not a java.lang.String. I'm not sure what your intent was, but it was wrong. You can't call EntityManager.find with a non-primary key value. (Your primary key is a Long, but you passed a String.)

          Now on to stacktrace 2. If you dig down through that you will see that OpenJPA is getting an IllegalStateException[1] from the application server. After doing a little bit of googling around, I was able to find a number of posts that talk about Glassfish having problems reloading classes after WAR redeploy. I'm not sure if that is your scenario, but it surely this isn't an OpenJPA problem. Perhaps you could try to restart the application server? The net of this is that you're going to have to work with the glassfish folks to figure out why we can't load that class.

          In the future, please utilize the users/dev mailing lists[2] to ask questions and wait to open a JIRA until we're sure there is a bug. I'm closing this JIRA as there is nothing to do here. If you would like to continue asking questions, please move this over to the users mailing list.

          Thanks,
          Rick

          [1] Caused by: java.lang.IllegalStateException: WEB9031: WebappClassLoader unable to load resource [org.apache.openjpa.util.LongId], because it has not yet been started, or was already stopped
          at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1410) <-- Glassfish
          at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1368)
          at au.com.ckd.model.BookModel.pcNewObjectIdInstance(BookModel.java)

          [2] http://openjpa.apache.org/mailing-lists.html

          Show
          Rick Curtis added a comment - Chuong - Lets start off with the javadoc for javax.persistence.EntityManager.find(Class<T> entityClass, Object primaryKey). <T> T find(Class<T> entityClass, Object primaryKey) Find by primary key. Parameters: entityClass - primaryKey - Returns: the found entity instance or null if the entity does not exist Throws: IllegalStateException - if this EntityManager has been closed. IllegalArgumentException - if the first argument does not denote an entity type or the second argument is not a valid type for that entity's primary key — The last part of the IllegalArgumentException states an exception will be thrown when "the second argument is not a valid type for that entity's primary key". By looking at your BookModel there is one column annotated as the ID column and that column is not a java.lang.String. I'm not sure what your intent was, but it was wrong. You can't call EntityManager.find with a non-primary key value. (Your primary key is a Long, but you passed a String.) Now on to stacktrace 2. If you dig down through that you will see that OpenJPA is getting an IllegalStateException [1] from the application server. After doing a little bit of googling around, I was able to find a number of posts that talk about Glassfish having problems reloading classes after WAR redeploy. I'm not sure if that is your scenario, but it surely this isn't an OpenJPA problem. Perhaps you could try to restart the application server? The net of this is that you're going to have to work with the glassfish folks to figure out why we can't load that class. In the future, please utilize the users/dev mailing lists [2] to ask questions and wait to open a JIRA until we're sure there is a bug. I'm closing this JIRA as there is nothing to do here. If you would like to continue asking questions, please move this over to the users mailing list. Thanks, Rick [1] Caused by: java.lang.IllegalStateException: WEB9031: WebappClassLoader unable to load resource [org.apache.openjpa.util.LongId] , because it has not yet been started, or was already stopped at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1410) <-- Glassfish at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1368) at au.com.ckd.model.BookModel.pcNewObjectIdInstance(BookModel.java) [2] http://openjpa.apache.org/mailing-lists.html
          Hide
          Chuong Pham added a comment -

          Thanks for the info, Rick.

          As for passing a String value as a second parameter to the EntityManager.find method, I realised after I posted the issue that as per OpenJPA doc that the second parameter can only accepts a primary key value. I suppose my comment regarding passing a String value to the EntityManager.find method should have been: Can I pass a String value as a second parameter to the EntityManager.find method in a future release of the OpenJPA? I noticed that some of the examples I found for EclipseLink suggested that it can accept a String and not restricted to a primary key. I was hoping that OpenJPA can do the same - well not, in the current release anyway. The reason for passing a [hashed] string is because I'm getting the value of the id parameter from the URL to be used with the EntityManager.find method, for example bookview?id=214736890D96ED598E1D3050F1F025A7 as opposed to bookview?id=5148 - which is the actual primary key of the record in the table. For me, it would be "safer" to pass a hashed value to prevent anyone from parameter tampering - even though I have server-side validation enforced on parameters - still IMHO, it would be a safer alternative to passing a primary key. The hashed value I have created [via database trigger] uses AES encryption and, as far as I know, has not been compromised by collusion or otherwise. I hope this explains why I am using a String as opposed to a Long value.

          As for the IllegalStateException error with Glassfish, I have restarted Glassfish several times, even went so far as to delete the build directory of the project and delete the eclipseApps and autodeploy directories under Glassfish and start again - all without success. Also, I have logged a post in the Glassfish forum - but so far, the responses I have received does not indicate that this might be a Glassfish issue, though I'm waiting for more responses to confirm that. I apologise in advance if this IS a Glassfish issue and I have wasted your time.

          Thanks for the link to the OpenJPA User Mailing List. Much appreciated. I'll post the question there and see if anyone else has a similar issue as I have.

          Regards
          Chuong

          Show
          Chuong Pham added a comment - Thanks for the info, Rick. As for passing a String value as a second parameter to the EntityManager.find method, I realised after I posted the issue that as per OpenJPA doc that the second parameter can only accepts a primary key value. I suppose my comment regarding passing a String value to the EntityManager.find method should have been: Can I pass a String value as a second parameter to the EntityManager.find method in a future release of the OpenJPA? I noticed that some of the examples I found for EclipseLink suggested that it can accept a String and not restricted to a primary key. I was hoping that OpenJPA can do the same - well not, in the current release anyway. The reason for passing a [hashed] string is because I'm getting the value of the id parameter from the URL to be used with the EntityManager.find method, for example bookview?id=214736890D96ED598E1D3050F1F025A7 as opposed to bookview?id=5148 - which is the actual primary key of the record in the table. For me, it would be "safer" to pass a hashed value to prevent anyone from parameter tampering - even though I have server-side validation enforced on parameters - still IMHO, it would be a safer alternative to passing a primary key. The hashed value I have created [via database trigger] uses AES encryption and, as far as I know, has not been compromised by collusion or otherwise. I hope this explains why I am using a String as opposed to a Long value. As for the IllegalStateException error with Glassfish, I have restarted Glassfish several times, even went so far as to delete the build directory of the project and delete the eclipseApps and autodeploy directories under Glassfish and start again - all without success. Also, I have logged a post in the Glassfish forum - but so far, the responses I have received does not indicate that this might be a Glassfish issue, though I'm waiting for more responses to confirm that. I apologise in advance if this IS a Glassfish issue and I have wasted your time. Thanks for the link to the OpenJPA User Mailing List. Much appreciated. I'll post the question there and see if anyone else has a similar issue as I have. Regards Chuong
          Hide
          Chuong Pham added a comment -

          The error "java.lang.IllegalStateException: WEB9031" relates to a problem with the OpenJPA enhancer (See Issue 1410 at https://issues.apache.org/jira/browse/OPENJPA-1410%3E). When you execute Sun's JDK the OpenJPA's dynamic enhancer starts by default. This action, in turn, stuffs up Glassfish's classloader class - hence, the "WEB9031" error.

          For those who experience this same issue, a simple workaround is to do the enhancement at build time - which I did in ANT with `org.apache.openjpa.ant.PCEnhancerTask` - and add this property to your "persistence.xml" to shut off the dynamic enhancer: "<property name="openjpa.DynamicEnhancementAgent" value="false"/>".

          Also, it wouldn't hurt to throw this in your "persistence.xml" as well "<property name="openjpa.RuntimeUnenhancedClasses" value="unsupported" />".

          Show
          Chuong Pham added a comment - The error "java.lang.IllegalStateException: WEB9031" relates to a problem with the OpenJPA enhancer (See Issue 1410 at https://issues.apache.org/jira/browse/OPENJPA-1410%3E ). When you execute Sun's JDK the OpenJPA's dynamic enhancer starts by default. This action, in turn, stuffs up Glassfish's classloader class - hence, the "WEB9031" error. For those who experience this same issue, a simple workaround is to do the enhancement at build time - which I did in ANT with `org.apache.openjpa.ant.PCEnhancerTask` - and add this property to your "persistence.xml" to shut off the dynamic enhancer: "<property name="openjpa.DynamicEnhancementAgent" value="false"/>". Also, it wouldn't hurt to throw this in your "persistence.xml" as well "<property name="openjpa.RuntimeUnenhancedClasses" value="unsupported" />".

            People

            • Assignee:
              Rick Curtis
              Reporter:
              Chuong Pham
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development