Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Samdani masum
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


step 1 :


step 2 :



STEP 4 :




STEP 5 : SQL
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samdani masum wrote: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

Try setting the Hibernate dialect to the MySQL dialect by adding this line to the persistence.xml file
 
Samdani masum
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Samdani masum wrote: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

Try setting the Hibernate dialect to the MySQL dialect by adding this line to the persistence.xml file


Hi, already i am adding this line , but i am found same error , Actually my "CutomerVM " and Native query style is different .
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what about changing your native query to
 
Samdani masum
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:And what about changing your native query to


SELECT customerid, customername, contactid FROM customer WHERE customerid =:csid


At first you have know that same query (without add-contactid ), CustomerVm class already working in eclipse link.
But not work in hibernate .


"contactid " addition in sql is not my technical requirements. becasue i have need a solution when sql query element and class attribute is not same .
then how solve this problem.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samdani masum wrote:At first you have know that same query (without add-contactid ), CustomerVm class already working in eclipse link.
But not work in hibernate .


"contactid " addition in sql is not my technical requirements. becasue i have need a solution when sql query element and class attribute is not same .
then how solve this problem.

CustomerVM is an entity, so you should definitely read the 3.10.16.1 Returning Managed Entities from Native Queries section in the JPA 2.1 Specification. Especially this part is related to your issue
JSR 338: Java Persistence API Version 2.1, 3.10.16.1 Returning Managed Entities from Native Queries wrote:When an entity is to be returned from a native query, the SQL statement should select all of the columns that are mapped to the entity object. This should include foreign key columns to related entities. The results obtained when insufficient data is available are undefined.
So you are not selecting all columns mapped in the entity, so the results are undefined. And it seems EclipseLink simply returns the results whereas Hibernate throws a runtime exception. Both are valid implementations of the JPA 2.1 specification.

Hope it helps!
Kind regards,
Roel
 
Samdani masum
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:CustomerVM is an entity, so you should definitely read the 3.10.16.1 Returning Managed Entities from Native Queries section in the JPA 2.1 Specification. Especially this part is related to your issue
JSR 338: Java Persistence API Version 2.1, 3.10.16.1 Returning Managed Entities from Native Queries wrote:When an entity is to be returned from a native query, the SQL statement should select all of the columns that are mapped to the entity object. This should include foreign key columns to related entities. The results obtained when insufficient data is available are undefined.
So you are not selecting all columns mapped in the entity, so the results are undefined. And it seems EclipseLink simply returns the results whereas Hibernate throws a runtime exception. Both are valid implementations of the JPA 2.1 specification.

thanks Roel for you help.

But one thing ,you mark that customerVM is entity class . but CustomerVM is not entity class. it has no relation in db table Customer .
So, now I am thinking that no need to select all column for this non-entity class when use Native query in JPA.

for this , i am found one reference :


http://www.mastertheboss.com/jboss-frameworks/hibernate-jpa/query/using-native-queries-with-hibernate-and-jpa
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samdani masum wrote:But one thing ,you mark that customerVM is entity class . but CustomerVM is not entity class. it has no relation in db table Customer .

CustomerVM is definitely an entity class as it's marked with the @Entity annotation
 
Samdani masum
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Samdani masum wrote:But one thing ,you mark that customerVM is entity class . but CustomerVM is not entity class. it has no relation in db table Customer .

CustomerVM is definitely an entity class as it's marked with the @Entity annotation



CustomerVm class has entity annotation but it has not include @Table("customer") , so you can not tell this entity class .
and it has not complete mapping in db table .

So, i am tell that "CustomerVm" class not a entity class , just it is a pojo class.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samdani masum wrote:CustomerVm class has entity annotation but it has not include @Table("customer") , so you can not tell this entity class .
and it has not complete mapping in db table .

Maybe you should first read the Entities section of the Java EE 6 Tutorial.

It clearly mentions
Java EE 6 Tutorial, Entities wrote:An entity class must follow these requirements.
- The class must be annotated with the javax.persistence.Entity annotation.
 
Dave Tolls
Ranch Hand
Posts: 2103
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samdani masum wrote:CustomerVm class has entity annotation but it has not include @Table("customer")...


And just to add, from the @Table API:
"
If no Table annotation is specified for an entity class, the default values apply.
"
So it assumes there's a table, using whatever default naming Hibernate uses.
It may even have created one, depending on how it's been set up.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic