java-覆盖EclipseLink EntityManager
作者:互联网
有没有办法覆盖EclipseLink提供的EntityManager?
我想重写EclipseLink的EntityManager.remove()方法-我不希望物理移除(仅逻辑上).
扩展适当的EntityManager很容易:
public class MyEntityManager extends org.eclipse.persistence.internal.jpa.EntityManagerImpl {
public MyEntityManager(String sessionName) {
super(sessionName);
}
public void remove(Object entity) {
AbstractEntity en = (AbstractEntity)entity;
en.setAuditRD(33333);
this.merge(en);
}
}
但是如何将其传递给EclipseLink的工厂以创建MyEntityManager而不是org.eclipse.persistence.internal.jpa.EntityManagerImpl?也许有什么参数可以传递给persistence.xml文件?
解决方法:
在persistence.xml类中,您可以使用provider元素来决定.只需将您的提供程序(而不是EntityManager)的实现放在那里. JPA规范摘录:
8.2.1.4 provider
The provider element specifies the name of the persistence provider’s javax.persistence.spi.PersistenceProvider
class. The provider element is optional, but should be specified if
the application is dependent upon a particular persistence provider
being used.
文件示例:
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="myapp">
<provider>org.mydomain.ejb.MyPersistenceProvider</provider>
<jta-data-source>java:/DefaultDS</jta-data-source>
</persistence-unit>
</persistence>
标签:jpa,override,eclipselink,java 来源: https://codeday.me/bug/20191121/2054976.html