Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating objects using Reflection

 
Senthil Kumar Sekar
Ranch Hand
Posts: 45
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is that a constructor is made mandatory for POJO-->For instance hibernate asks the developer to have a constructor in the POJOs.

Can anybody what are the other ways a f/w or any container does to create instances other than below



and my POJO is



In this case i do not hav a default constructor and iam able to create instances as shown above without any issues.

Then why we should have a contructor as a mandatory?

Thanks.

Regards
Senthil Kumar Sekar
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No XML relevance. Moving.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that Hibernate uses Reflection Constructor methodology to create an instance. Try the below code and check for the error with your POJO.

The code fails when the getConstructor() method is called with the Pojo class
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason being that the automatically generated constructor has the same visibility as the class itself, in this case default visibility (no access modifier). Make the class public and that code will work.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Rob. it worked perfect. there should be some other reason for Hibernate forcing to have default constructors in POJO then.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic