Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, Hibernate annotations, How to add methods to POJO object?

I am using hibernate annotations. How to add methods to POJO object? For example i have "getChildNodes" method, associated with database, but i want also add recursive method "getAllChildNodes". I get "org.hibernate.PropertyNotFoundException: Could not find a setter for property progress in class" exception when i do it.

like image 978
RussianBear Avatar asked Jun 11 '10 13:06

RussianBear


2 Answers

If I interpret this as "how do I add a method that is NOT related to persistence" then you need to use the @Transient annotation on the getAllChildNodes() method

like image 92
Mike Q Avatar answered Sep 22 '22 16:09

Mike Q


There are two ways of defining the structure of your entity.

  • using annotations on the instance variables of your entity or
  • using annotations on the getter methods of your entity

When using the annotations on getter methods, Hibernate assumes that every getXxx (and isXxx for boolean types) represents definition of a persistent property. And this holds even if that particular getter does not contain any annotations, as happens in your case.

Hibernate also expects to find a matching setter method for each persistent property. And in your case that is what's missing and causes the exception.

You can solve this problem by declaring your custom getter as @Transient that says this getter does not represent a persistent property. Another way would be to convert the entity to use annotations on the instance variables. The latter would be my personal choice.

like image 22
psp Avatar answered Sep 22 '22 16:09

psp