Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Creating objects using Reflection

 
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
 
Marshal
Posts: 25674
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No XML relevance. Moving.
 
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
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic