ike*_*ke3 5 java oracle hibernate
我有一个具有一对多关联的休眠实体:
@Entity
public class Parent {
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
@Cascade(CascadeType.ALL)
private Set<Child> children = new HashSet<Child>();
@Version
private Date version;
}
@Entity
public class Child {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PARENT_ID")
private Parent parent;
@Basic
private String key;
}
Run Code Online (Sandbox Code Playgroud)
*为清楚起见,删除了一些注释
子实体映射到具有复合主键(KEY 和 PARENT_ID)的表上。问题是,当两个用户将同一个 Child(具有相同的键)添加到同一个 Parent 时,级联保存(session.saveOrUpdate(parent))会因 Child 的主键违规而不是乐观锁失败而失败。
如果用户除了集合之外还更改了 Parent 实体中的某些其他属性,则乐观锁可以正常工作。
我可以向 Parent 类添加一些虚构的属性,并在每次集合更改时更改它,它会起作用,但它看起来像一个黑客。
或者我可以将复合主键替换为代理键(通过添加@Id)。
问题是:在这种情况下,推荐的实现乐观锁定的方法是什么?
只有单向集合更改才会传播到父实体版本。因为您使用的是双向关联,所以@ManyToOne控制该关联的是一侧,因此在父端集合中添加/删除实体不会影响父实体版本。
但是,您仍然可以将更改从子实体传播到父实体。OPTIMISTIC_FORCE_INCREMENT这要求您在子实体被修改时传播锁。
简而言之,您需要让所有实体实施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();
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您需要两个事件侦听器:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
和
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);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以按如下方式注册:
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
}
}
Run Code Online (Sandbox Code Playgroud)
然后提供RootAwareFlushEntityEventListenerIntegrator通过 Hibernate 配置属性提供:
configuration.put(
"hibernate.integrator_provider",
(IntegratorProvider) () -> Collections.singletonList(
RootAwareEventListenerIntegrator.INSTANCE
)
);
Run Code Online (Sandbox Code Playgroud)
现在,当您修改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);
Run Code Online (Sandbox Code Playgroud)
父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
Run Code Online (Sandbox Code Playgroud)
小智 -1
首先我认为你需要声明你的主键并定义如何生成 PK。例子 :
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
Run Code Online (Sandbox Code Playgroud)
然后,向父级添加新子级的最佳方法应该是这样的(在父级):
public Child addChild() {
Child child = new Child()
if (childList== null) {
childList= new ArrayList<childList>();
}
child.setparent(this);
childList.add(child);
return child;
}
Run Code Online (Sandbox Code Playgroud)
当孩子已经存在时,只需执行相同的操作,但不创建新的孩子。
我认为它应该可以解决你的一些问题。
| 归档时间: |
|
| 查看次数: |
1740 次 |
| 最近记录: |