Shop Amazon Today and Save
Get more recipes like this one in my new book Hibernate Tips: More than 70 solutions to common Hibernate problems: https://goo.gl/XfywNk
Today I want to answer the question how you can map an optional entity attribute to an Optional.
Since the release of Java 8, I get this question quite often. Unfortunately, Hibernate and JPA 2.2 don’t support Optional as an attribute type.
But with a little trick, you can still use Optional as the return type of your getter methods. If you annotate the primary key attribute with an @Id annotation, you tell Hibernate to use field access to set and retrieve the entity attribute values. That means that Hibernate doesn’t call the getter or setter methods and that you can implement them in any way you like. So, you can wrap the optional attribute into an Optional instead of returning it directly.
If you like this video, please give me your thumbs up and share it with your friends and co-workers.
Like my channel? Subscribe!
➜ http://bit.ly/2cUsid8
Join the free Member Library:
https://goo.gl/dtyIIC
Read the accompanying post: https://www.thoughts-on-java.org/hibernate-tips-map-entity-attribute-optional/
Want to connect with me?
Blog: http://www.thoughts-on-java.org/
Twitter: https://twitter.com/thjanssen123
Facebook: https://www.facebook.com/thoughtsonjava
Shop Amazon Today and Save
More from my site
from
http://www.handyshowto.com/how-to/hibernate-tip-how-to-map-an-entity-attribute-to-an-optional/%20
No comments:
Post a Comment