Hibernate - Declaring A Transient object in a pojo

25,696

Ok, different idea. I guess since you marked the property as transient hibernate will neither load nor stor the property.

One solution could be to make two classes. One without the b property. And the second one extending from the first using the mappedSuperclass annotation.

Or you could try mapping it with some formula that just provides some default value (e.g. false). So for the 'normal' case you hibernate would use the formula, resulting in some default value, and for your special query you use whatever logic is in that query.

Share:
25,696
kanso
Author by

kanso

I've had my B.Sc in software engineering at 2008, and since then had the privlige to work at big corportaes (Intel Research, Thomson Reuters), build the backbones of amazing startups (Outbrain, Ginger), complete my thesis, and keep learning new stuff all the time. I've done research in c and Java, backend in Java and Node, NLP in Groovy, scala and some C# (Hate it, sorry), some web in Angular, toyed around with native android apps and was a part of building an ionic cross-platform app. I love tera-incognita, and rather walk the bleeding cutting edge. As Plutarch said – “The mind is a fire to be kindled , not a vessel to be filled.” . So far , I’ve never been cold.

Updated on January 06, 2020

Comments

  • kanso
    kanso over 4 years

    I have a pojo that is object A , of table TableA

    TableA (int a1,int a2).

    To fill the table I run a query that returns (int a1, int a2, boolean b3) (and runs multiple data checks)

    b3 is not valid in TableA , but I still want to use the same pojo for both (it's a very big pojo , and it will be a major code duplication to have one just for the sake of the query)

    I've declared in the pojo

    @Transient
    Boolean getB3() {..}
    void setB3(Boolean b3) {..} 
    

    And in the query I declared that I expect to get the b3 value:

    <return> ...
    <return-property name="b3" column="b3"/>
    ...
    </return>
    

    But Hibernate just ignores the parameter and never use "setB3()".. When I remove the @Transient , it works (and then fail when inserting to the table , naturally) - so all the names are correct

    How can I fix this?

    Thanks!

  • kanso
    kanso over 15 years
    I didn't really get what you're saying , I'm sorry
  • Jens Schauder
    Jens Schauder over 15 years
    I am saying basically the same as the other answer
  • Jens Schauder
    Jens Schauder over 15 years
    whats so bad about 2 classes and an interface, as long as there is no duplication?