Uploaded image for project: 'MariaDB Connector/J'
  1. MariaDB Connector/J
  2. CONJ-28

native query returns strange object instead of String

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.1.0
    • Fix Version/s: 1.1.2
    • Component/s: None
    • Labels:
      None
    • Environment:
      Ubuntu 12.04, JBoss Application Server EAP 5.1.2, MySQL-Galera 5.5.29, OpenJDK 1.6.0_27

      Description

      MariaDB Java connector fails where MySQL connector, Postgres, Oracle etc works fine.


      I have a JPA bean with a simple method:

      public static CertificateInfo findFirstCertificateInfo(EntityManager entityManager, String issuerDN, String serialNumber) {
      CertificateInfo ret = null;
      final Query query = entityManager
      .createNativeQuery(
      "SELECT a.fingerprint, a.subjectDN, a.cAFingerprint, a.status, a.type, a.serialNumber, a.expireDate, a.revocationDate, a.revocationReason, "
      + "a.username, a.tag, a.certificateProfileId, a.updateTime FROM CertificateData a WHERE a.issuerDN=:issuerDN AND a.serialNumber=:serialNumber",
      "CertificateInfoSubset2");
      query.setParameter("issuerDN", issuerDN);
      query.setParameter("serialNumber", serialNumber);
      query.setMaxResults(1);
      @SuppressWarnings("unchecked")
      final List<Object[]> resultList = (List<Object[]>) query.getResultList();
      if (!resultList.isEmpty()) {
      Object[] fields = resultList.get(0);
      // The order of the results are defined by the SqlResultSetMapping annotation
      System.out.println("object: "+fields[0]);
      System.out.println("type: "+fields[0].getClass().getName());

      String fingerprint = (String) fields[0];
      String subjectDN = (String) fields[1];
      String cafp = (String) fields[2];
      int status = ValueExtractor.extractIntValue(fields[3]);
      int type = ValueExtractor.extractIntValue(fields[4]);
      long expireDate = ValueExtractor.extractLongValue(fields[5]);
      long revocationDate = ValueExtractor.extractLongValue(fields[6]);
      int revocationReason = ValueExtractor.extractIntValue(fields[7]);
      String username = (String) fields[8];
      String tag = (String) fields[9];
      int cProfId = ValueExtractor.extractIntValue(fields[10]);
      long updateTime;
      if (fields[11] == null)

      { updateTime = 0; // Might be null in an upgraded installation }

      else

      { updateTime = ValueExtractor.extractLongValue(fields[11]); }

      ret = new CertificateInfo(fingerprint, cafp, serialNumber, issuerDN, subjectDN, status, type, expireDate, revocationDate,
      revocationReason, username, tag, cProfId, updateTime);
      }
      return ret;
      }


      @SqlResultSetMapping(name = "CertificateInfoSubset2", columns =

      { @ColumnResult(name = "fingerprint"), @ColumnResult(name = "subjectDN"), @ColumnResult(name = "cAFingerprint"), @ColumnResult(name = "status"), @ColumnResult(name = "type"), @ColumnResult(name = "expireDate"), @ColumnResult(name = "revocationDate"), @ColumnResult(name = "revocationReason"), @ColumnResult(name = "username"), @ColumnResult(name = "tag"), @ColumnResult(name = "certificateProfileId"), @ColumnResult(name = "updateTime") }

      ),


      The table looks like:

      CREATE TABLE CertificateData (
      fingerprint VARCHAR(250) BINARY NOT NULL,
      base64Cert LONGTEXT,
      cAFingerprint VARCHAR(250) BINARY,
      certificateProfileId INT(11) NOT NULL,
      expireDate BIGINT(20) NOT NULL,
      issuerDN VARCHAR(250) BINARY NOT NULL,
      revocationDate BIGINT(20) NOT NULL,
      revocationReason INT(11) NOT NULL,
      rowProtection LONGTEXT,
      rowVersion INT(11) NOT NULL,
      serialNumber VARCHAR(250) BINARY NOT NULL,
      status INT(11) NOT NULL,
      subjectDN VARCHAR(250) BINARY NOT NULL,
      subjectKeyId VARCHAR(250) BINARY,
      tag VARCHAR(250) BINARY,
      type INT(11) NOT NULL,
      updateTime BIGINT(20) NOT NULL,
      username VARCHAR(250) BINARY,
      PRIMARY KEY (fingerprint)
      );


      Running the query I get:

      java.lang.ClassCastException: [B cannot be cast to java.lang.String
      at org.cesecore.certificates.certificate.CertificateData.findFirstCertificateInfo(CertificateData.java:760)
      at org.cesecore.certificates.certificate.CertificateStoreSessionBean.findFirstCertificateInfo(CertificateStoreSessionBean.java:387)


      CertificateData.java:760 is the line from above:
      String fingerprint = (String) fields[0];

      The System.out.println debug output prints strange things:
      [STDOUT] (http-0.0.0.0-8443-1) object: [B@25073d81
      [STDOUT] (http-0.0.0.0-8443-1) type: [B

        Gliffy Diagrams

          Activity

          Hide
          wlad Vladislav Vaintroub added a comment -

          I'll run some experiments and tell my findings.

          Show
          wlad Vladislav Vaintroub added a comment - I'll run some experiments and tell my findings.
          Hide
          wlad Vladislav Vaintroub added a comment -

          Oh I see. MariaDB JDBC handles "varbinary" and "varchar binary" as same type. ConnectorJ handles them differently (though documentation is misleading here)

          Show
          wlad Vladislav Vaintroub added a comment - Oh I see. MariaDB JDBC handles "varbinary" and "varchar binary" as same type. ConnectorJ handles them differently (though documentation is misleading here)
          Hide
          primetomas Tomas Gustavsson added a comment -

          I'll be more than happy to test a snapshot.

          Show
          primetomas Tomas Gustavsson added a comment - I'll be more than happy to test a snapshot.
          Hide
          wlad Vladislav Vaintroub added a comment -

          If you are unable to build yourself (using bzr to get the sources, and maven to build,as described here https://kb.askmonty.org/en/about-the-mariadb-java-client/) , in this case you can used attached file, build by myself.

          Show
          wlad Vladislav Vaintroub added a comment - If you are unable to build yourself (using bzr to get the sources, and maven to build,as described here https://kb.askmonty.org/en/about-the-mariadb-java-client/ ) , in this case you can used attached file, build by myself.
          Hide
          primetomas Tomas Gustavsson added a comment -

          Tested the new version. It seems to work just fine.

          Awesome! Thanks, nice to be able to move all Maria.

          Show
          primetomas Tomas Gustavsson added a comment - Tested the new version. It seems to work just fine. Awesome! Thanks, nice to be able to move all Maria.

            People

            • Assignee:
              wlad Vladislav Vaintroub
              Reporter:
              primetomas Tomas Gustavsson
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 2 hours, 30 minutes
                2h 30m