简体   繁体   English

jpa:如何在java se开发中使用jpa

[英]jpa : how to use jpa in java se development

I am working on a java se project which use jpa to persist data with database. 我正在使用java的java se项目进行工作,以使用数据库持久化数据。

Right now i get a new entity manager for each database operation and close it when function call end. 现在,我为每个数据库操作获取一个新的实体管理器,并在函数调用结束时将其关闭。 like 喜欢

public void save(T entity){
  em = getNewEntity();
  em.persisty(entity);
  em.close;
}

but its very hard to manager the many-to-many relationship. 但是很难处理多对多关系。 for example 例如
i have A entity who have a many-to-many relationship with B entity. 我有一个与B实体有多对多关系的A实体。 and they have CascadeType.MERGE. 他们有CascadeType.MERGE。

once i create A, i create B , and run multiple thread to generate information for both A and B. but sometime B keep the outdated of A, if i merge B then A's data will be purged. 一旦我创建A,我就创建B,并运行多个线程来为A和B生成信息。但是有时B保持A的过时,如果我合并B则A的数据将被清除。

for example . 例如 。 A is music , B is music album , so one B will have a A list. A是音乐,B是音乐专辑,因此一个B将具有A列表。 Another thread will read music metadata and get music tag info and update A , B will be updated when a new A found in the FS. 另一个线程将读取音乐元数据并获取音乐标签信息并更新A,当在FS中找到新的A时,B将更新。
Since entity manager closed , the entities in B's A list is out of date which means when A update itself , B doesn't know A updated . 由于实体管理器已关闭,因此B的A列表中的实体已过期,这意味着当A更新自身时,B不知道A更新了。 Once Merge B , jpa will help us merge A using the entity in B 's A list. 合并B后,jpa将帮助我们使用B的A列表中的实体合并A。

in order to solve this problem, i decide to use one entity manager and put all database operation into one single thread. 为了解决此问题,我决定使用一个实体管理器,并将所有数据库操作放到一个线程中。 Then new problem comes out. 然后出现了新问题。

when the transaction is active and the transaction is supposed to open for the entity between 当交易处于活动状态并且应该为实体之间的交易打开时

transaction.begin();
....
transaction.end();

however there are many threads running , and those thread may change entity too. 但是,有许多线程正在运行,这些线程也可能会更改实体。

my question is: how to use jpa in java se development ?. 我的问题是:如何在Java SE开发中使用jpa? thanks 谢谢

If the relationship gets out of sync, you cannot blame JPA. 如果关系不同步,则不能责怪JPA。 It is the application's responsibility to keep the relationship sane. 保持关系理智是应用程序的责任。

In JPA, as in Java in general it is the responsibility of the application, or the object model to maintain relationships. 在JPA中,通常与Java中一样,由应用程序或对象模型负责维护关系。

Source 资源

As @home said, you need to synchronize properly. 正如@home所说,您需要正确同步。

That being said, the EntityManager is not thread safe so you need to solve this in some way. 话虽这么说,EntityManager不是线程安全的,所以您需要以某种方式解决此问题。 Synchronize your application, use one EntityManager per thread, use @PersistenceContext, etc. 同步您的应用程序,每个线程使用一个EntityManager,使用@PersistenceContext等。

Edit: Here is a question asked about synchrinizing access to the EntityManager. 编辑: 是一个有关同步访问EntityManager的问题。 I think it may be of use to you. 我认为这可能对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM