Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Hibernate ignore a method?

Tags:

This question is essentially the opposite of this one.

I have a method like so:

public boolean isVacant() {     return getEmployeeNum() != null && getEmployeeNum().equals("00000000"); } 

When I load it up, Hibernate is complaining that I have no attribute called vacant. But I don't want an attribute called vacant - I have no need to store that data - it's simply logic.

Hibernate says:

org.hibernate.PropertyNotFoundException: Could not find a setter for property vacant in class com.mycomp.myclass...

Is there an annotation I can add to my isVacant() method to make Hibernate ignore it?

like image 565
corsiKa Avatar asked Sep 10 '12 20:09

corsiKa


People also ask

How do I ignore a field in JPA?

To ignore a field, annotate it with @Transient so it will not be mapped by hibernate.

What is @transient in Hibernate?

@Transient annotation in JPA or Hibernate is used to indicate that a field is not to be persisted or ignore fields to save in the database. @Transient exist in javax. persistence package. It is used to annotate a property or field of an entity class, mapped superclass, or embeddable class.

What annotation can exclude fields and properties of entity from mapping in JPA?

When persisting Java objects into database records using an Object-Relational Mapping (ORM) framework, we often want to ignore certain fields. If the framework is compliant with the Java Persistence API (JPA), we can add the @Transient annotation to these fields.


1 Answers

Add @Transient to the method then Hibernate should ignore it.

To quote the Hibernate Documentation:

Every non static non transient property (field or method depending on the access type) of an entity is considered persistent, unless you annotate it as @Transient.

like image 95
RNJ Avatar answered Nov 15 '22 07:11

RNJ