技术控

    今日:0| 主题:63445
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] The best way to map a Java 1.8 Optional entity attribute with JPA and Hibernate

[复制链接]
初吻给了香烟 发表于 2016-10-3 19:14:14
218 8
Follow @vlad_mihalcea  Introduction

   StackOverflow is a neverending source of great questions. This time, we are covering this question about using  Java 1.8 Optional with JPA and Hibernate  .
   Java 1.8 introduced the java.util.Optional container object that may or may not contain a certain value. Combining Optional and streams is very handy. Therefore, you might want that some nullable entity attributes be exposed as Optional .
   This article is going to demonstrate what are caveats of using Optional with entity attributes, and how you can overcome them.
  Domain Model

  Let’s assume we have the following entity model:
   

The best way to map a Java 1.8 Optional entity attribute with JPA and Hibernate

The best way to map a Java 1.8 Optional entity attribute with JPA and Hibernate

   The Post entity is the root in our entity aggregate. There can be multiple PostComment(s) associated with a Post , and each PostComment can have an Attachment . Because the Attachment is not mandatory, it makes sense to use an Optional container for it.
  Optional and Serializable

   The java.util.Optional does not implement the Serializable interface. For this reason, we should never map an entity attribute as Optional because that will restrict the entity usage.
   For instance, we could have a detached instance stored in the HttpSession because we are operating a long conversation workflow . Every object stored in the HttpSession should be Serializable because the session may be clustered on several web nodes.
   If you’re using Java EE and Stateful Session Beans, then you must ensure that all entities are `Serializable, as otherwise the passivisation process would fail .
   For all these reasons, an entity attribute should not be mapped as a java.util.Optional .
  All is not lost

   But just because we cannot map an entity attribute as Optional , it does not mean we cannot expose it using an Optional container. If we are using field-based access persistence, then the underlying entity attribute can be mapped using the actual persisted type, while the getter method can use an Optional instead.
  1. @Entity(name = "PostComment")
  2. @Table(name = "post_comment")
  3. public static class PostComment
  4.     implements Serializable {

  5.     @Id
  6.     @GeneratedValue
  7.     private Long id;

  8.     private String review;

  9.     @ManyToOne(fetch = FetchType.LAZY)
  10.     private Post post;

  11.     @ManyToOne(fetch = FetchType.LAZY)
  12.     private Attachment attachment;

  13.     public Optional<Attachment> getAttachment() {
  14.         return Optional.ofNullable(attachment);
  15.     }

  16.     public void setAttachment(Attachment attachment) {
  17.         this.attachment = attachment;
  18.     }
  19.    
  20.     //Other getters and setters omitted for brevity
  21. }
复制代码
That’s it!
   If you’re using property-based access, then the getter must expose the actual persisted type, in which case you need to have a separate @Transient method that uses the Optional method return type.
  Testing time

  Assuming we have the following entities:
  1. byte[] coverContent = new byte[] {1, 2, 3};

  2. Post post = new Post();
  3. post.setId(1L);
  4. post.setTitle("High-Performance Java Persistence");
  5. entityManager.persist(post);

  6. PostComment comment1 = new PostComment();
  7. comment1.setPost(post);

  8. entityManager.persist(comment1);

  9. Attachment cover = new Attachment();
  10. cover.setContent(coverContent);
  11. entityManager.persist(cover);

  12. PostComment comment2 = new PostComment();
  13. comment2.setPost(post);
  14. comment2.setAttachment(cover);

  15. entityManager.persist(comment2);
复制代码
  If we alaready have a list of PostComment(s) :
  1. List<PostComment> comments = entityManager.createQuery(
  2.     "select pc " +
  3.     "from PostComment pc " +
  4.     "join pc.post p " +
  5.     "where p.id = :postId", PostComment.class)
  6. .setParameter("postId", 1L)
  7. .getResultList();
复制代码
  We can process Attachment(s) as follows:
  1. Attachment notAvailable = getNotAvaillableImage();

  2. List<Attachment> attachments = comments
  3. .stream()
  4. .map(pc -> pc.getAttachment()
  5. .orElse(notAvailable))
  6. .collect(Collectors.toList());
复制代码
  If there is not Attachment already set, we can use a default N/A image.
   If you enjoyed this article, I bet you are going to love my book as well.
   

The best way to map a Java 1.8 Optional entity attribute with JPA and Hibernate

The best way to map a Java 1.8 Optional entity attribute with JPA and Hibernate

  Conclusion

   When using JPA and Hibernate, you can make use of the Java 1.8 Optional in your Domain Model entities. However, you have to make sure not to use it as a persistent property type.
     Follow @vlad_mihalcea    If you liked this article, you might want to subscribe to my newsletter too.
明杨.校园 发表于 2016-10-4 01:14:38
洗洗更白白,顶顶更健康!
回复 支持 反对

使用道具 举报

范冬梅 发表于 2016-10-4 01:29:39
生活可以将就,生活也可以讲究!
回复 支持 反对

使用道具 举报

qjss 发表于 2016-10-4 02:31:15
放假前的节奏
回复 支持 反对

使用道具 举报

989541i 发表于 2016-10-4 05:32:53
元芳你怎么看?
回复 支持 反对

使用道具 举报

烨霖 发表于 2016-10-23 22:18:22
敢进男厕所的女汉子别隐身
回复 支持 反对

使用道具 举报

ljdms 发表于 2016-10-25 11:27:31
男人偷腥时的智商仅次于爱因斯坦!  
回复 支持 反对

使用道具 举报

乐瑶 发表于 2016-11-17 18:54:41
泥泞路上的奔驰,永远跑不过高速路上的夏利。说明:”平台很重要!”
回复 支持 反对

使用道具 举报

eqiverson 发表于 2016-11-20 23:06:19
爱情就像两个拉着橡皮筋的人,受伤的总是不愿意放手的那一个!
回复 支持 反对

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表