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

hibernate Instantiation Exception: No default constructor for entity

 
Pramod Kumar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Query q = em.createNamedQuery("User.byUserId");
q.setParameter("userId", userId);


--> user = (User) getSingleResult(q);

at this point I am getting following exception.
org.hibernate.InstantiationException: No default constructor for entity

Do I need to have a default constructor? Is it compulsory that do I need to have the default constructor?
Thanks,
Pramod.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your POJO needs a default, no args constructor. You don't need to define one unless you have define a constructor that takes arguments in you User class?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Copied from other topic (please don't post the same question in more than one topic):
Originally posted by Pramod Kumar:
Thanks for the response below... I have 2 args construcotor in User class still I am getting this exception, once I keep the no arg constructor I didn't see this exception.

I am using enum for one colum and while inserting I am able to insert in to database while retreving I am getting the following exception.


Exception
------
No enum const class

Unknown name value for enum class com.UserRole: 0


------------------------------------------------


UserRole is an enum.

I am setting the values as

UserRole userRole = UserRole.DUSER;

and it is stored as 0 in database, while retreving I am getting the above exception.

[ October 23, 2007: Message edited by: Paul Sturrock ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have 2 args construcotor in User class still I am getting this exception, once I keep the no arg constructor I didn't see this exception


In Java, the compiler will always supply a default constructor unless you define the constructor yourself. If your POJO has a constructor that takes arguments, then you don't have a default constructor, and how can Hibernate instantiate this? Where does it get the values for the constructor from?

All your Hibernate classes must have a constructor that doesn't take any parameters.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic