How to fetch a @Transient property from the database using JPA and Hibernate?


#1

Example:

List<Subdivision> subdivision = session.createQuery(sql).list();

As far as I know in the Subdivision class, there must be a default constructor - it is there.

I have 3 fields in the Subdivision class:

  1.  @Id
     @GeneratedValue (strategy = GenerationType.IDENTITY)
     private int idSubdivision;
    
  2. @Column(name = "nameSubdivision", unique = true)
    private String nameSubdivision; 
    
  3.  @Transient 
     VBox vbox; (javafx.scene.layout.VBox)
    

The first two fields are filled in using session.createQuery (sql) .list ();

But I need the third field of vbox to be filled in the Subdivision class. It is filled in from me based on the value of the nameSubdivision field. In the database, the vbox field is of course not present, so it does not fill out.

Of course I can go through the generated List subdivision and fill in this field.

But I wanted to do everything at once. Can I access session.createQuery (sql) .list () - and override it so that it takes into account my field


#2

You have multiple options:

  1. You can use the @Generated annotation as explained in this article.
  2. You can use the @Formula annotation as explained in this article.