0 Replies Latest reply on Nov 18, 2015 1:33 PM by Gulam Samdani

    JPA 2.1 Native Query Result mapping problem Hibernate 4 , 5 version with wildfly

    Gulam Samdani Expert
      TThis example works in glassfish 4 but not work in wildfly 8/9/10 . Any one please help me .
                         
                           =========================================================================
                         
                         
      step 1 :
      public class ProblemApps {
          
        
           EntityManagerFactory emf = Persistence.createEntityManagerFactory("Test22PU");
           EntityManager em = emf.createEntityManager();
             int a = 1;
               try {
                   Query query = em.createNativeQuery("SELECT customerid,customername  FROM customer WHERE customerid =:csid", CutomerVM.class);
                   query.setParameter("csid", a);
                   CutomerVM cutomerVM = (CutomerVM) query.getSingleResult();
                 
                    System.out.println("ID : "+cutomerVM.getCustomerID());
                    System.out.println("Name : "+cutomerVM.getCustomerName());
               } catch (Exception e) {
                   e.printStackTrace();
               }
             
             
           }
         
      }

       

       

      step 2 :

       

      import javax.persistence.Column;

      import javax.persistence.Entity;

      import javax.persistence.Id;

       

       

      @Entity

      public class CutomerVM {

      @Id
      @Column(name = "customerid")
      int customerID;
      @Column(name="customername")
      String customerName;
      @Column(name="contactid")
      String contatID;
      public CutomerVM() {
      }

         

         

        STEP 4 :

       

       

        <?xml version="1.0" encoding="UTF-8"?>

        <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

      <persistence-unit name="Test22PU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
          <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/bank?zeroDateTimeBehavior=convertToNull"/>
          <property name="javax.persistence.jdbc.user" value="root"/>
          <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
          <property name="javax.persistence.jdbc.password" value="root"/>
          <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
        
        </properties>

        </persistence-unit>

       

      STEP 5 : SQL

                         
                         
      I am faceing a critical problem in JPA 2.1 with Hibernate orm 4/5 . any one please give me a suggestion ?
      But same code working in JPA2.1 with Eclipselink orm  all version.

         

      DROP TABLE IF EXISTS `customer`;
      CREATE TABLE `customer` (
        `customerid` int(10) NOT NULL AUTO_INCREMENT,
        `contactid` varchar(45) NOT NULL,
        `customername` varchar(45) NOT NULL,
        `status` tinyint(1) NOT NULL,
        `customertype` smallint(5) NOT NULL,
        PRIMARY KEY (`customerid`)
      ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

         

      --
      -- Dumping data for table `customer`
      --

         

      /*!40000 ALTER TABLE `customer` DISABLE KEYS */;
      INSERT INTO `customer` (`customerid`,`contactid`,`customername`,`status`,`customertype`) VALUES
      (1,'2000','samdani',0,1),
      (2,'2000','samdani',0,1),
      (3,'2000','samdani',0,1);

      /*!40000 ALTER TABLE `customer` ENABLE KEYS */;