-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#788. PiperOrigin-RevId: 526028062
- Loading branch information
Showing
3 changed files
with
154 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
extensions/persist/test/com/google/inject/persist/jpa/TrackedEntityManagerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.google.inject.persist.jpa; | ||
|
||
import java.util.Map; | ||
import javax.persistence.Cache; | ||
import javax.persistence.EntityGraph; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.PersistenceUnitUtil; | ||
import javax.persistence.Query; | ||
import javax.persistence.SynchronizationType; | ||
import javax.persistence.criteria.CriteriaBuilder; | ||
import javax.persistence.metamodel.Metamodel; | ||
|
||
/** Proxy EntityManager that adds tracking capabilities, keeping a count of created objects. */ | ||
class TrackedEntityManagerFactory implements EntityManagerFactory { | ||
|
||
private final EntityManagerFactory delegate; | ||
private int entityManagerCreatedCount = 0; | ||
|
||
public TrackedEntityManagerFactory(EntityManagerFactory delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
public boolean hasCreatedSomeEntityManager() { | ||
return (entityManagerCreatedCount > 0); | ||
} | ||
|
||
public int getEntityManagerCreatedCount() { | ||
return entityManagerCreatedCount; | ||
} | ||
|
||
@Override | ||
public boolean isOpen() { | ||
return delegate.isOpen(); | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getProperties() { | ||
return delegate.getProperties(); | ||
} | ||
|
||
@Override | ||
public PersistenceUnitUtil getPersistenceUnitUtil() { | ||
return delegate.getPersistenceUnitUtil(); | ||
} | ||
|
||
@Override | ||
public Metamodel getMetamodel() { | ||
return delegate.getMetamodel(); | ||
} | ||
|
||
@Override | ||
public CriteriaBuilder getCriteriaBuilder() { | ||
return delegate.getCriteriaBuilder(); | ||
} | ||
|
||
@Override | ||
public Cache getCache() { | ||
return delegate.getCache(); | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
@Override | ||
public EntityManager createEntityManager(Map arg0) { | ||
entityManagerCreatedCount++; | ||
return delegate.createEntityManager(arg0); | ||
} | ||
|
||
@Override | ||
public EntityManager createEntityManager() { | ||
entityManagerCreatedCount++; | ||
return delegate.createEntityManager(); | ||
} | ||
|
||
@Override | ||
public EntityManager createEntityManager(SynchronizationType synchronizationType) { | ||
entityManagerCreatedCount++; | ||
return delegate.createEntityManager(synchronizationType); | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
@Override | ||
public EntityManager createEntityManager(SynchronizationType synchronizationType, Map map) { | ||
entityManagerCreatedCount++; | ||
return delegate.createEntityManager(synchronizationType, map); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
delegate.close(); | ||
} | ||
|
||
@Override | ||
public void addNamedQuery(String name, Query query) { | ||
delegate.addNamedQuery(name, query); | ||
} | ||
|
||
@Override | ||
public <T> T unwrap(Class<T> cls) { | ||
return delegate.unwrap(cls); | ||
} | ||
|
||
@Override | ||
public <T> void addNamedEntityGraph(String graphName, EntityGraph<T> entityGraph) { | ||
delegate.addNamedEntityGraph(graphName, entityGraph); | ||
} | ||
} |