1717package com .faforever .api .config .elide ;
1818
1919import com .google .common .base .Preconditions ;
20- import com .yahoo .elide .core .datastore .DataStore ;
2120import com .yahoo .elide .core .datastore .DataStoreTransaction ;
22- import com .yahoo .elide .core .dictionary .EntityDictionary ;
23- import com .yahoo .elide .core .utils .TypeHelper ;
24- import org .hibernate .ScrollMode ;
21+ import com .yahoo .elide .datastores .jpa .JpaDataStore ;
2522import org .hibernate .Session ;
26- import org .slf4j .Logger ;
27- import org .slf4j .LoggerFactory ;
2823import org .springframework .transaction .PlatformTransactionManager ;
2924import org .springframework .transaction .TransactionDefinition ;
3025import org .springframework .transaction .TransactionStatus ;
3126import org .springframework .transaction .support .DefaultTransactionDefinition ;
3227
3328import javax .persistence .EntityManager ;
34- import javax .persistence .metamodel .EntityType ;
35- import java .util .Objects ;
36- import java .util .Set ;
3729
38- /**
39- * Spring Hibernate DataStore.
40- *
41- * @author olOwOlo
42- */
43- public class SpringHibernateDataStore implements DataStore {
44-
45- private static final Logger logger = LoggerFactory .getLogger (SpringHibernateDataStore .class );
30+ public class SpringHibernateDataStore extends JpaDataStore {
4631
4732 protected final PlatformTransactionManager txManager ;
4833 protected final EntityManager entityManager ;
49- protected final boolean isScrollEnabled ;
50- protected final ScrollMode scrollMode ;
5134 protected final HibernateTransactionSupplier transactionSupplier ;
5235
53- /**
54- * Constructor.
55- *
56- * @param txManager Spring PlatformTransactionManager
57- * @param entityManager EntityManager
58- * @param isScrollEnabled Whether or not scrolling is enabled on driver
59- * @param scrollMode Scroll mode to use for scrolling driver
60- */
61- public SpringHibernateDataStore (PlatformTransactionManager txManager ,
62- EntityManager entityManager ,
63- boolean isScrollEnabled ,
64- ScrollMode scrollMode ) {
65- this (txManager , entityManager , isScrollEnabled , scrollMode , SpringHibernateTransaction ::new );
36+ public SpringHibernateDataStore (
37+ PlatformTransactionManager txManager ,
38+ EntityManager entityManager
39+ ) {
40+ this (txManager , entityManager , SpringHibernateTransaction ::new );
6641 }
6742
68- /**
69- * <p>Constructor.</p>
70- * Useful for extending the store and relying on existing code to instantiate custom hibernate transaction.
71- *
72- * @param txManager Spring PlatformTransactionManager
73- * @param entityManager EntityManager factory
74- * @param isScrollEnabled Whether or not scrolling is enabled on driver
75- * @param scrollMode Scroll mode to use for scrolling driver
76- * @param transactionSupplier Supplier for transaction
77- */
78- protected SpringHibernateDataStore (PlatformTransactionManager txManager ,
79- EntityManager entityManager ,
80- boolean isScrollEnabled ,
81- ScrollMode scrollMode ,
82- HibernateTransactionSupplier transactionSupplier ) {
43+ protected SpringHibernateDataStore (
44+ PlatformTransactionManager txManager ,
45+ EntityManager entityManager ,
46+ HibernateTransactionSupplier transactionSupplier
47+ ) {
48+ super (() -> entityManager , null );
8349 this .txManager = txManager ;
8450 this .entityManager = entityManager ;
85- this .isScrollEnabled = isScrollEnabled ;
86- this .scrollMode = scrollMode ;
8751 this .transactionSupplier = transactionSupplier ;
8852 }
8953
90- @ Override
91- public void populateEntityDictionary (EntityDictionary dictionary ) {
92- Set <EntityType <?>> entities = entityManager .getMetamodel ().getEntities ();
93- /* bind all entities */
94- entities .stream ()
95- .map (EntityType ::getJavaType )
96- .filter (Objects ::nonNull )
97- .forEach (mappedClass -> {
98- try {
99- // Ignore this result.
100- // We are just checking to see if it throws an exception meaning that
101- // provided class was _not_ an entity.
102- dictionary .lookupEntityClass (TypeHelper .getClassType (mappedClass ));
103- // Bind if successful
104- dictionary .bindEntity (mappedClass );
105- } catch (IllegalArgumentException e ) {
106- // Ignore this entity
107- // Turns out that hibernate may include non-entity types in this list when using things
108- // like envers. Since they are not entities, we do not want to bind them into the entity
109- // dictionary
110- logger .debug ("Ignoring entity" , e );
111- }
112- });
113- }
114-
11554 @ Override
11655 public DataStoreTransaction beginTransaction () {
11756 // begin a spring transaction
@@ -123,15 +62,28 @@ public DataStoreTransaction beginTransaction() {
12362 Session session = entityManager .unwrap (Session .class );
12463 Preconditions .checkNotNull (session );
12564
126- return transactionSupplier .get (session , txManager , txStatus , isScrollEnabled , scrollMode );
65+ return transactionSupplier .get (session , txManager , txStatus );
66+ }
67+
68+ @ Override
69+ public DataStoreTransaction beginReadTransaction () {
70+ // begin a spring transaction
71+ DefaultTransactionDefinition def = new DefaultTransactionDefinition ();
72+ def .setName ("elide read transaction" );
73+ def .setReadOnly (true );
74+ TransactionStatus txStatus = txManager .getTransaction (def );
75+
76+ Session session = entityManager .unwrap (Session .class );
77+ Preconditions .checkNotNull (session );
78+
79+ return transactionSupplier .get (session , txManager , txStatus );
12780 }
12881
12982 /**
13083 * Functional interface for describing a method to supply a custom Hibernate transaction.
13184 */
13285 @ FunctionalInterface
13386 public interface HibernateTransactionSupplier {
134- SpringHibernateTransaction get (Session session , PlatformTransactionManager txManager ,
135- TransactionStatus txStatus , boolean isScrollEnabled , ScrollMode scrollMode );
87+ SpringHibernateTransaction get (Session session , PlatformTransactionManager txManager , TransactionStatus txStatus );
13688 }
13789}
0 commit comments