2010-05-24 74 views
4

考慮兩個實體父和子。休眠版本化父實體

  • 孩子是父母的短暫集合的一部分
  • 孩子有多對一映射於母公司與FetchType.LAZY

兩者都顯示相同的形式向用戶上。當用戶保存數據時,我們首先更新父實例,然後更新子集合(都使用合併)。

現在到了棘手的部分。當用戶只修改表單上的Child屬性時,hibernate髒檢查不會更新父實例,因此不會增加該實體的樂觀鎖定版本號。

我希望看到情況,其中只有Parent是版本化的,每當我調用Parent的合併時,即使實際更新未在db中執行,版本也會始終更新。

+0

在這種情況下,您可能想回答自己的問題。有人可能會來,併爲此投票。或者更好的是,這可能會幫助正在尋找類似事物的人。 – 2010-05-24 08:18:42

回答

3

我想我想通了。合併後被調用一個附加的實例引用被返回。當我使用entityManager.lock(更新,LockModeType.WRITE)獲得顯式鎖定時;那麼即使父實例未在db中更新,版本號也會增加。

另外,我將分離的實例版本與持久實例版本進行比較。如果它們不匹配,那麼Parent在db中被更新,並且版本號也發生了變化。這使版本號保持一致。否則,即使合併操作改變了它,entityManager.lock也會增加版本號。

仍在尋找解決方案當合並期間實體不髒時,如何使hibernate增加版本。

+0

+1用於鎖定解決方案 – oedo 2010-05-24 09:10:15

0

我不認爲你可以強迫hibernate增加一個未更改對象的版本號,因爲如果沒有任何變化(出於顯而易見的原因),它將不會執行任何db UPDATE查詢。

你可以做一個討厭的黑客,如添加一個新的領域的對象,手動增加,但個人,這似乎是浪費時間和資源。我會去你的明確的鎖定解決方案,因爲這似乎給你你想要的,沒有不必要的嘲弄。

1

您可以將更改從子實體傳播到父實體。這需要您每當修改子實體時傳播OPTIMISTIC_FORCE_INCREMENT鎖。

This article詳細解釋了你應該實現這種用例的方式。

總之,你需要把所有的實體實施RootAware接口:

public interface RootAware<T> { 
    T root(); 
} 

@Entity(name = "Post") 
@Table(name = "post") 
public class Post { 

    @Id 
    private Long id; 

    private String title; 

    @Version 
    private int version; 

    //Getters and setters omitted for brevity 
} 

@Entity(name = "PostComment") 
@Table(name = "post_comment") 
public class PostComment 
    implements RootAware<Post> { 

    @Id 
    private Long id; 

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

    private String review; 

    //Getters and setters omitted for brevity 

    @Override 
    public Post root() { 
     return post; 
    } 
} 

@Entity(name = "PostCommentDetails") 
@Table(name = "post_comment_details") 
public class PostCommentDetails 
    implements RootAware<Post> { 

    @Id 
    private Long id; 

    @ManyToOne(fetch = FetchType.LAZY) 
    @MapsId 
    private PostComment comment; 

    private int votes; 

    //Getters and setters omitted for brevity 

    @Override 
    public Post root() { 
     return comment.getPost(); 
    } 
} 

然後,你需要兩個事件偵聽器:

public static class RootAwareInsertEventListener 
    implements PersistEventListener { 

    private static final Logger LOGGER = 
     LoggerFactory.getLogger(RootAwareInsertEventListener.class); 

    public static final RootAwareInsertEventListener INSTANCE = 
     new RootAwareInsertEventListener(); 

    @Override 
    public void onPersist(PersistEvent event) throws HibernateException { 
     final Object entity = event.getObject(); 

     if(entity instanceof RootAware) { 
      RootAware rootAware = (RootAware) entity; 
      Object root = rootAware.root(); 
      event.getSession().lock(root, LockMode.OPTIMISTIC_FORCE_INCREMENT); 

      LOGGER.info("Incrementing {} entity version because a {} child entity has been inserted", root, entity); 
     } 
    } 

    @Override 
    public void onPersist(PersistEvent event, Map createdAlready) 
     throws HibernateException { 
     onPersist(event); 
    } 
} 

public static class RootAwareInsertEventListener 
    implements PersistEventListener { 

    private static final Logger LOGGER = 
     LoggerFactory.getLogger(RootAwareInsertEventListener.class); 

    public static final RootAwareInsertEventListener INSTANCE = 
     new RootAwareInsertEventListener(); 

    @Override 
    public void onPersist(PersistEvent event) throws HibernateException { 
     final Object entity = event.getObject(); 

     if(entity instanceof RootAware) { 
      RootAware rootAware = (RootAware) entity; 
      Object root = rootAware.root(); 
      event.getSession().lock(root, LockMode.OPTIMISTIC_FORCE_INCREMENT); 

      LOGGER.info("Incrementing {} entity version because a {} child entity has been inserted", root, entity); 
     } 
    } 

    @Override 
    public void onPersist(PersistEvent event, Map createdAlready) 
     throws HibernateException { 
     onPersist(event); 
    } 
} 

這你可以註冊如下:

public class RootAwareEventListenerIntegrator 
    implements org.hibernate.integrator.spi.Integrator { 

    public static final RootAwareEventListenerIntegrator INSTANCE = 
     new RootAwareEventListenerIntegrator(); 

    @Override 
    public void integrate(
      Metadata metadata, 
      SessionFactoryImplementor sessionFactory, 
      SessionFactoryServiceRegistry serviceRegistry) { 

     final EventListenerRegistry eventListenerRegistry = 
       serviceRegistry.getService(EventListenerRegistry.class); 

     eventListenerRegistry.appendListeners(EventType.PERSIST, RootAwareInsertEventListener.INSTANCE); 
     eventListenerRegistry.appendListeners(EventType.FLUSH_ENTITY, RootAwareUpdateAndDeleteEventListener.INSTANCE); 
    } 

    @Override 
    public void disintegrate(
      SessionFactoryImplementor sessionFactory, 
      SessionFactoryServiceRegistry serviceRegistry) { 
     //Do nothing 
    } 
} 

,然後通過一個Hibernate配置屬性提供RootAwareFlushEntityEventListenerIntegrator

configuration.put(
    "hibernate.integrator_provider", 
    (IntegratorProvider)() -> Collections.singletonList(
     RootAwareEventListenerIntegrator.INSTANCE 
    ) 
); 

現在,當你修改PostCommentDetails實體:

PostCommentDetails postCommentDetails = entityManager.createQuery(
    "select pcd " + 
    "from PostCommentDetails pcd " + 
    "join fetch pcd.comment pc " + 
    "join fetch pc.post p " + 
    "where pcd.id = :id", PostCommentDetails.class) 
.setParameter("id", 2L) 
.getSingleResult(); 

postCommentDetails.setVotes(15); 

Post實體版本的修改,以及:

SELECT pcd.comment_id AS comment_2_2_0_ , 
     pc.id AS id1_1_1_ , 
     p.id AS id1_0_2_ , 
     pcd.votes AS votes1_2_0_ , 
     pc.post_id AS post_id3_1_1_ , 
     pc.review AS review2_1_1_ , 
     p.title AS title2_0_2_ , 
     p.version AS version3_0_2_ 
FROM post_comment_details pcd 
INNER JOIN post_comment pc ON pcd.comment_id = pc.id 
INNER JOIN post p ON pc.post_id = p.id 
WHERE pcd.comment_id = 2 

UPDATE post_comment_details 
SET votes = 15 
WHERE comment_id = 2 

UPDATE post 
SET version = 1 
where id = 1 AND version = 0