-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: JPA 2.1 Native Query Result mapping problem Hibernate 4 , 5
PostPosted: Thu Nov 19, 2015 2:22 am 
Newbie

Joined: Wed Nov 18, 2015 1:25 pm
Posts: 1
Posts: 32

[Post New]posted Today 12:07:34 PM
Quote Edit

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. i have to use hibernate so, how to solve it hibernate

Error : column "contactid" found


Error reproduce steps :

step 1 :
public class ProblemApps {

public static void main(String args[]){

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




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 */;


Top
 Profile  
 
 Post subject: Re: JPA 2.1 Native Query Result mapping problem Hibernate 4 , 5
PostPosted: Sat Dec 05, 2015 5:07 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The table name and the entity name don't match, so you need to add the following @Table annotation:

Code:
@Entity
@Table(name="customer"")
public class CutomerVM {
    ...
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.