aspose file tools*
The moose likes Java in General and the fly likes Creating objects using Reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating objects using Reflection" Watch "Creating objects using Reflection" New topic
Author

Creating objects using Reflection

Senthil Kumar Sekar
Ranch Hand

Joined: Aug 08, 2010
Posts: 45

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
Bartender

Joined: Oct 14, 2005
Posts: 18124
    
    8

No XML relevance. Moving.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Yes Rob. it worked perfect. there should be some other reason for Hibernate forcing to have default constructors in POJO then.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating objects using Reflection
 
Similar Threads
how to use one-to-one association
getting error with Hibernate
how to use Xdoclet in standalone application
How to update using Hibernate
HashMap entries with duplicate keys...