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.
Open up the .java file and write a method named getAllChildNodes()
.
Hibernate doesn't write code for you, it maps fields in your database to your code. That's all. If you want to have extra logic in your domain/model classes besides the normal getters and setters for your properties, you'll have to add them yourself.
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
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.