-
Notifications
You must be signed in to change notification settings - Fork 61
/
EntityManager.java
897 lines (840 loc) · 38.2 KB
/
EntityManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
/*
* Copyright (c) 2008, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Linda DeMichiel - 2.1
// Linda DeMichiel - 2.0
package javax.persistence;
import java.util.Map;
import java.util.List;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaUpdate;
import javax.persistence.criteria.CriteriaDelete;
/**
* Interface used to interact with the persistence context.
*
* <p> An <code>EntityManager</code> instance is associated with
* a persistence context. A persistence context is a set of entity
* instances in which for any persistent entity identity there is
* a unique entity instance. Within the persistence context, the
* entity instances and their lifecycle are managed.
* The <code>EntityManager</code> API is used
* to create and remove persistent entity instances, to find entities
* by their primary key, and to query over entities.
*
* <p> The set of entities that can be managed by a given
* <code>EntityManager</code> instance is defined by a persistence
* unit. A persistence unit defines the set of all classes that are
* related or grouped by the application, and which must be
* colocated in their mapping to a single database.
*
* @see Query
* @see TypedQuery
* @see CriteriaQuery
* @see PersistenceContext
* @see StoredProcedureQuery
*
* @since 1.0
*/
public interface EntityManager {
/**
* Make an instance managed and persistent.
* @param entity entity instance
* @throws EntityExistsException if the entity already exists.
* (If the entity already exists, the <code>EntityExistsException</code> may
* be thrown when the persist operation is invoked, or the
* <code>EntityExistsException</code> or another <code>PersistenceException</code> may be
* thrown at flush or commit time.)
* @throws IllegalArgumentException if the instance is not an
* entity
* @throws TransactionRequiredException if there is no transaction when
* invoked on a container-managed entity manager of that is of type
* <code>PersistenceContextType.TRANSACTION</code>
*/
public void persist(Object entity);
/**
* Merge the state of the given entity into the
* current persistence context.
* @param entity entity instance
* @return the managed instance that the state was merged to
* @throws IllegalArgumentException if instance is not an
* entity or is a removed entity
* @throws TransactionRequiredException if there is no transaction when
* invoked on a container-managed entity manager of that is of type
* <code>PersistenceContextType.TRANSACTION</code>
*/
public <T> T merge(T entity);
/**
* Remove the entity instance.
* @param entity entity instance
* @throws IllegalArgumentException if the instance is not an
* entity or is a detached entity
* @throws TransactionRequiredException if invoked on a
* container-managed entity manager of type
* <code>PersistenceContextType.TRANSACTION</code> and there is
* no transaction
*/
public void remove(Object entity);
/**
* Find by primary key.
* Search for an entity of the specified class and primary key.
* If the entity instance is contained in the persistence context,
* it is returned from there.
* @param entityClass entity class
* @param primaryKey primary key
* @return the found entity instance or null if the entity does
* not exist
* @throws IllegalArgumentException if the first argument does
* not denote an entity type or the second argument is
* is not a valid type for that entity's primary key or
* is null
*/
public <T> T find(Class<T> entityClass, Object primaryKey);
/**
* Find by primary key, using the specified properties.
* Search for an entity of the specified class and primary key.
* If the entity instance is contained in the persistence
* context, it is returned from there.
* If a vendor-specific property or hint is not recognized,
* it is silently ignored.
* @param entityClass entity class
* @param primaryKey primary key
* @param properties standard and vendor-specific properties
* and hints
* @return the found entity instance or null if the entity does
* not exist
* @throws IllegalArgumentException if the first argument does
* not denote an entity type or the second argument is
* is not a valid type for that entity's primary key or
* is null
* @since 2.0
*/
public <T> T find(Class<T> entityClass, Object primaryKey,
Map<String, Object> properties);
/**
* Find by primary key and lock.
* Search for an entity of the specified class and primary key
* and lock it with respect to the specified lock type.
* If the entity instance is contained in the persistence context,
* it is returned from there, and the effect of this method is
* the same as if the lock method had been called on the entity.
* <p> If the entity is found within the persistence context and the
* lock mode type is pessimistic and the entity has a version
* attribute, the persistence provider must perform optimistic
* version checks when obtaining the database lock. If these
* checks fail, the <code>OptimisticLockException</code> will be thrown.
* <p>If the lock mode type is pessimistic and the entity instance
* is found but cannot be locked:
* <ul>
* <li> the <code>PessimisticLockException</code> will be thrown if the database
* locking failure causes transaction-level rollback
* <li> the <code>LockTimeoutException</code> will be thrown if the database
* locking failure causes only statement-level rollback
* </ul>
* @param entityClass entity class
* @param primaryKey primary key
* @param lockMode lock mode
* @return the found entity instance or null if the entity does
* not exist
* @throws IllegalArgumentException if the first argument does
* not denote an entity type or the second argument is
* not a valid type for that entity's primary key or
* is null
* @throws TransactionRequiredException if there is no
* transaction and a lock mode other than <code>NONE</code> is
* specified or if invoked on an entity manager which has
* not been joined to the current transaction and a lock
* mode other than <code>NONE</code> is specified
* @throws OptimisticLockException if the optimistic version
* check fails
* @throws PessimisticLockException if pessimistic locking
* fails and the transaction is rolled back
* @throws LockTimeoutException if pessimistic locking fails and
* only the statement is rolled back
* @throws PersistenceException if an unsupported lock call
* is made
* @since 2.0
*/
public <T> T find(Class<T> entityClass, Object primaryKey,
LockModeType lockMode);
/**
* Find by primary key and lock, using the specified properties.
* Search for an entity of the specified class and primary key
* and lock it with respect to the specified lock type.
* If the entity instance is contained in the persistence context,
* it is returned from there.
* <p> If the entity is found
* within the persistence context and the lock mode type
* is pessimistic and the entity has a version attribute, the
* persistence provider must perform optimistic version checks
* when obtaining the database lock. If these checks fail,
* the <code>OptimisticLockException</code> will be thrown.
* <p>If the lock mode type is pessimistic and the entity instance
* is found but cannot be locked:
* <ul>
* <li> the <code>PessimisticLockException</code> will be thrown if the database
* locking failure causes transaction-level rollback
* <li> the <code>LockTimeoutException</code> will be thrown if the database
* locking failure causes only statement-level rollback
* </ul>
* <p>If a vendor-specific property or hint is not recognized,
* it is silently ignored.
* <p>Portable applications should not rely on the standard timeout
* hint. Depending on the database in use and the locking
* mechanisms used by the provider, the hint may or may not
* be observed.
* @param entityClass entity class
* @param primaryKey primary key
* @param lockMode lock mode
* @param properties standard and vendor-specific properties
* and hints
* @return the found entity instance or null if the entity does
* not exist
* @throws IllegalArgumentException if the first argument does
* not denote an entity type or the second argument is
* not a valid type for that entity's primary key or
* is null
* @throws TransactionRequiredException if there is no
* transaction and a lock mode other than <code>NONE</code> is
* specified or if invoked on an entity manager which has
* not been joined to the current transaction and a lock
* mode other than <code>NONE</code> is specified
* @throws OptimisticLockException if the optimistic version
* check fails
* @throws PessimisticLockException if pessimistic locking
* fails and the transaction is rolled back
* @throws LockTimeoutException if pessimistic locking fails and
* only the statement is rolled back
* @throws PersistenceException if an unsupported lock call
* is made
* @since 2.0
*/
public <T> T find(Class<T> entityClass, Object primaryKey,
LockModeType lockMode,
Map<String, Object> properties);
/**
* Get an instance, whose state may be lazily fetched.
* If the requested instance does not exist in the database,
* the <code>EntityNotFoundException</code> is thrown when the instance
* state is first accessed. (The persistence provider runtime is
* permitted to throw the <code>EntityNotFoundException</code> when
* <code>getReference</code> is called.)
* The application should not expect that the instance state will
* be available upon detachment, unless it was accessed by the
* application while the entity manager was open.
* @param entityClass entity class
* @param primaryKey primary key
* @return the found entity instance
* @throws IllegalArgumentException if the first argument does
* not denote an entity type or the second argument is
* not a valid type for that entity's primary key or
* is null
* @throws EntityNotFoundException if the entity state
* cannot be accessed
*/
public <T> T getReference(Class<T> entityClass,
Object primaryKey);
/**
* Synchronize the persistence context to the
* underlying database.
* @throws TransactionRequiredException if there is
* no transaction or if the entity manager has not been
* joined to the current transaction
* @throws PersistenceException if the flush fails
*/
public void flush();
/**
* Set the flush mode that applies to all objects contained
* in the persistence context.
* @param flushMode flush mode
*/
public void setFlushMode(FlushModeType flushMode);
/**
* Get the flush mode that applies to all objects contained
* in the persistence context.
* @return flushMode
*/
public FlushModeType getFlushMode();
/**
* Lock an entity instance that is contained in the persistence
* context with the specified lock mode type.
* <p>If a pessimistic lock mode type is specified and the entity
* contains a version attribute, the persistence provider must
* also perform optimistic version checks when obtaining the
* database lock. If these checks fail, the
* <code>OptimisticLockException</code> will be thrown.
* <p>If the lock mode type is pessimistic and the entity instance
* is found but cannot be locked:
* <ul>
* <li> the <code>PessimisticLockException</code> will be thrown if the database
* locking failure causes transaction-level rollback
* <li> the <code>LockTimeoutException</code> will be thrown if the database
* locking failure causes only statement-level rollback
* </ul>
* @param entity entity instance
* @param lockMode lock mode
* @throws IllegalArgumentException if the instance is not an
* entity or is a detached entity
* @throws TransactionRequiredException if there is no
* transaction or if invoked on an entity manager which
* has not been joined to the current transaction
* @throws EntityNotFoundException if the entity does not exist
* in the database when pessimistic locking is
* performed
* @throws OptimisticLockException if the optimistic version
* check fails
* @throws PessimisticLockException if pessimistic locking fails
* and the transaction is rolled back
* @throws LockTimeoutException if pessimistic locking fails and
* only the statement is rolled back
* @throws PersistenceException if an unsupported lock call
* is made
*/
public void lock(Object entity, LockModeType lockMode);
/**
* Lock an entity instance that is contained in the persistence
* context with the specified lock mode type and with specified
* properties.
* <p>If a pessimistic lock mode type is specified and the entity
* contains a version attribute, the persistence provider must
* also perform optimistic version checks when obtaining the
* database lock. If these checks fail, the
* <code>OptimisticLockException</code> will be thrown.
* <p>If the lock mode type is pessimistic and the entity instance
* is found but cannot be locked:
* <ul>
* <li> the <code>PessimisticLockException</code> will be thrown if the database
* locking failure causes transaction-level rollback
* <li> the <code>LockTimeoutException</code> will be thrown if the database
* locking failure causes only statement-level rollback
* </ul>
* <p>If a vendor-specific property or hint is not recognized,
* it is silently ignored.
* <p>Portable applications should not rely on the standard timeout
* hint. Depending on the database in use and the locking
* mechanisms used by the provider, the hint may or may not
* be observed.
* @param entity entity instance
* @param lockMode lock mode
* @param properties standard and vendor-specific properties
* and hints
* @throws IllegalArgumentException if the instance is not an
* entity or is a detached entity
* @throws TransactionRequiredException if there is no
* transaction or if invoked on an entity manager which
* has not been joined to the current transaction
* @throws EntityNotFoundException if the entity does not exist
* in the database when pessimistic locking is
* performed
* @throws OptimisticLockException if the optimistic version
* check fails
* @throws PessimisticLockException if pessimistic locking fails
* and the transaction is rolled back
* @throws LockTimeoutException if pessimistic locking fails and
* only the statement is rolled back
* @throws PersistenceException if an unsupported lock call
* is made
* @since 2.0
*/
public void lock(Object entity, LockModeType lockMode,
Map<String, Object> properties);
/**
* Refresh the state of the instance from the database,
* overwriting changes made to the entity, if any.
* @param entity entity instance
* @throws IllegalArgumentException if the instance is not
* an entity or the entity is not managed
* @throws TransactionRequiredException if there is no
* transaction when invoked on a container-managed
* entity manager of type <code>PersistenceContextType.TRANSACTION</code>
* @throws EntityNotFoundException if the entity no longer
* exists in the database
*/
public void refresh(Object entity);
/**
* Refresh the state of the instance from the database, using
* the specified properties, and overwriting changes made to
* the entity, if any.
* <p> If a vendor-specific property or hint is not recognized,
* it is silently ignored.
* @param entity entity instance
* @param properties standard and vendor-specific properties
* and hints
* @throws IllegalArgumentException if the instance is not
* an entity or the entity is not managed
* @throws TransactionRequiredException if there is no
* transaction when invoked on a container-managed
* entity manager of type <code>PersistenceContextType.TRANSACTION</code>
* @throws EntityNotFoundException if the entity no longer
* exists in the database
* @since 2.0
*/
public void refresh(Object entity,
Map<String, Object> properties);
/**
* Refresh the state of the instance from the database,
* overwriting changes made to the entity, if any, and
* lock it with respect to given lock mode type.
* <p>If the lock mode type is pessimistic and the entity instance
* is found but cannot be locked:
* <ul>
* <li> the <code>PessimisticLockException</code> will be thrown if the database
* locking failure causes transaction-level rollback
* <li> the <code>LockTimeoutException</code> will be thrown if the
* database locking failure causes only statement-level
* rollback.
* </ul>
* @param entity entity instance
* @param lockMode lock mode
* @throws IllegalArgumentException if the instance is not
* an entity or the entity is not managed
* @throws TransactionRequiredException if invoked on a
* container-managed entity manager of type
* <code>PersistenceContextType.TRANSACTION</code> when there is
* no transaction; if invoked on an extended entity manager when
* there is no transaction and a lock mode other than <code>NONE</code>
* has been specified; or if invoked on an extended entity manager
* that has not been joined to the current transaction and a
* lock mode other than <code>NONE</code> has been specified
* @throws EntityNotFoundException if the entity no longer exists
* in the database
* @throws PessimisticLockException if pessimistic locking fails
* and the transaction is rolled back
* @throws LockTimeoutException if pessimistic locking fails and
* only the statement is rolled back
* @throws PersistenceException if an unsupported lock call
* is made
* @since 2.0
*/
public void refresh(Object entity, LockModeType lockMode);
/**
* Refresh the state of the instance from the database,
* overwriting changes made to the entity, if any, and
* lock it with respect to given lock mode type and with
* specified properties.
* <p>If the lock mode type is pessimistic and the entity instance
* is found but cannot be locked:
* <ul>
* <li> the <code>PessimisticLockException</code> will be thrown if the database
* locking failure causes transaction-level rollback
* <li> the <code>LockTimeoutException</code> will be thrown if the database
* locking failure causes only statement-level rollback
* </ul>
* <p>If a vendor-specific property or hint is not recognized,
* it is silently ignored.
* <p>Portable applications should not rely on the standard timeout
* hint. Depending on the database in use and the locking
* mechanisms used by the provider, the hint may or may not
* be observed.
* @param entity entity instance
* @param lockMode lock mode
* @param properties standard and vendor-specific properties
* and hints
* @throws IllegalArgumentException if the instance is not
* an entity or the entity is not managed
* @throws TransactionRequiredException if invoked on a
* container-managed entity manager of type
* <code>PersistenceContextType.TRANSACTION</code> when there is
* no transaction; if invoked on an extended entity manager when
* there is no transaction and a lock mode other than <code>NONE</code>
* has been specified; or if invoked on an extended entity manager
* that has not been joined to the current transaction and a
* lock mode other than <code>NONE</code> has been specified
* @throws EntityNotFoundException if the entity no longer exists
* in the database
* @throws PessimisticLockException if pessimistic locking fails
* and the transaction is rolled back
* @throws LockTimeoutException if pessimistic locking fails and
* only the statement is rolled back
* @throws PersistenceException if an unsupported lock call
* is made
* @since 2.0
*/
public void refresh(Object entity, LockModeType lockMode,
Map<String, Object> properties);
/**
* Clear the persistence context, causing all managed
* entities to become detached. Changes made to entities that
* have not been flushed to the database will not be
* persisted.
*/
public void clear();
/**
* Remove the given entity from the persistence context, causing
* a managed entity to become detached. Unflushed changes made
* to the entity if any (including removal of the entity),
* will not be synchronized to the database. Entities which
* previously referenced the detached entity will continue to
* reference it.
* @param entity entity instance
* @throws IllegalArgumentException if the instance is not an
* entity
* @since 2.0
*/
public void detach(Object entity);
/**
* Check if the instance is a managed entity instance belonging
* to the current persistence context.
* @param entity entity instance
* @return boolean indicating if entity is in persistence context
* @throws IllegalArgumentException if not an entity
*/
public boolean contains(Object entity);
/**
* Get the current lock mode for the entity instance.
* @param entity entity instance
* @return lock mode
* @throws TransactionRequiredException if there is no
* transaction or if the entity manager has not been
* joined to the current transaction
* @throws IllegalArgumentException if the instance is not a
* managed entity and a transaction is active
* @since 2.0
*/
public LockModeType getLockMode(Object entity);
/**
* Set an entity manager property or hint.
* If a vendor-specific property or hint is not recognized, it is
* silently ignored.
* @param propertyName name of property or hint
* @param value value for property or hint
* @throws IllegalArgumentException if the second argument is
* not valid for the implementation
* @since 2.0
*/
public void setProperty(String propertyName, Object value);
/**
* Get the properties and hints and associated values that are in effect
* for the entity manager. Changing the contents of the map does
* not change the configuration in effect.
* @return map of properties and hints in effect for entity manager
* @since 2.0
*/
public Map<String, Object> getProperties();
/**
* Create an instance of <code>Query</code> for executing a
* Jakarta Persistence query language statement.
* @param qlString a Jakarta Persistence query string
* @return the new query instance
* @throws IllegalArgumentException if the query string is
* found to be invalid
*/
public Query createQuery(String qlString);
/**
* Create an instance of <code>TypedQuery</code> for executing a
* criteria query.
* @param criteriaQuery a criteria query object
* @return the new query instance
* @throws IllegalArgumentException if the criteria query is
* found to be invalid
* @since 2.0
*/
public <T> TypedQuery<T> createQuery(CriteriaQuery<T> criteriaQuery);
/**
* Create an instance of <code>Query</code> for executing a criteria
* update query.
* @param updateQuery a criteria update query object
* @return the new query instance
* @throws IllegalArgumentException if the update query is
* found to be invalid
* @since 2.1
*/
public Query createQuery(CriteriaUpdate updateQuery);
/**
* Create an instance of <code>Query</code> for executing a criteria
* delete query.
* @param deleteQuery a criteria delete query object
* @return the new query instance
* @throws IllegalArgumentException if the delete query is
* found to be invalid
* @since 2.1
*/
public Query createQuery(CriteriaDelete deleteQuery);
/**
* Create an instance of <code>TypedQuery</code> for executing a
* Jakarta Persistence query language statement.
* The select list of the query must contain only a single
* item, which must be assignable to the type specified by
* the <code>resultClass</code> argument.
* @param qlString a Jakarta Persistence query string
* @param resultClass the type of the query result
* @return the new query instance
* @throws IllegalArgumentException if the query string is found
* to be invalid or if the query result is found to
* not be assignable to the specified type
* @since 2.0
*/
public <T> TypedQuery<T> createQuery(String qlString, Class<T> resultClass);
/**
* Create an instance of <code>Query</code> for executing a named query
* (in the Jakarta Persistence query language or in native SQL).
* @param name the name of a query defined in metadata
* @return the new query instance
* @throws IllegalArgumentException if a query has not been
* defined with the given name or if the query string is
* found to be invalid
*/
public Query createNamedQuery(String name);
/**
* Create an instance of <code>TypedQuery</code> for executing a
* Jakarta Persistence query language named query.
* The select list of the query must contain only a single
* item, which must be assignable to the type specified by
* the <code>resultClass</code> argument.
* @param name the name of a query defined in metadata
* @param resultClass the type of the query result
* @return the new query instance
* @throws IllegalArgumentException if a query has not been
* defined with the given name or if the query string is
* found to be invalid or if the query result is found to
* not be assignable to the specified type
* @since 2.0
*/
public <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass);
/**
* Create an instance of <code>Query</code> for executing
* a native SQL statement, e.g., for update or delete.
* If the query is not an update or delete query, query
* execution will result in each row of the SQL result
* being returned as a result of type Object[] (or a result
* of type Object if there is only one column in the select
* list.) Column values are returned in the order of their
* appearance in the select list and default JDBC type
* mappings are applied.
* @param sqlString a native SQL query string
* @return the new query instance
*/
public Query createNativeQuery(String sqlString);
/**
* Create an instance of <code>Query</code> for executing
* a native SQL query.
* @param sqlString a native SQL query string
* @param resultClass the class of the resulting instance(s)
* @return the new query instance
*/
public Query createNativeQuery(String sqlString, Class resultClass);
/**
* Create an instance of <code>Query</code> for executing
* a native SQL query.
* @param sqlString a native SQL query string
* @param resultSetMapping the name of the result set mapping
* @return the new query instance
*/
public Query createNativeQuery(String sqlString, String resultSetMapping);
/**
* Create an instance of <code>StoredProcedureQuery</code> for executing a
* stored procedure in the database.
* <p>Parameters must be registered before the stored procedure can
* be executed.
* <p>If the stored procedure returns one or more result sets,
* any result set will be returned as a list of type Object[].
* @param name name assigned to the stored procedure query
* in metadata
* @return the new stored procedure query instance
* @throws IllegalArgumentException if a query has not been
* defined with the given name
* @since 2.1
*/
public StoredProcedureQuery createNamedStoredProcedureQuery(String name);
/**
* Create an instance of <code>StoredProcedureQuery</code> for executing a
* stored procedure in the database.
* <p>Parameters must be registered before the stored procedure can
* be executed.
* <p>If the stored procedure returns one or more result sets,
* any result set will be returned as a list of type Object[].
* @param procedureName name of the stored procedure in the
* database
* @return the new stored procedure query instance
* @throws IllegalArgumentException if a stored procedure of the
* given name does not exist (or the query execution will
* fail)
* @since 2.1
*/
public StoredProcedureQuery createStoredProcedureQuery(String procedureName);
/**
* Create an instance of <code>StoredProcedureQuery</code> for executing a
* stored procedure in the database.
* <p>Parameters must be registered before the stored procedure can
* be executed.
* <p>The <code>resultClass</code> arguments must be specified in the order in
* which the result sets will be returned by the stored procedure
* invocation.
* @param procedureName name of the stored procedure in the
* database
* @param resultClasses classes to which the result sets
* produced by the stored procedure are to
* be mapped
* @return the new stored procedure query instance
* @throws IllegalArgumentException if a stored procedure of the
* given name does not exist (or the query execution will
* fail)
* @since 2.1
*/
public StoredProcedureQuery createStoredProcedureQuery(
String procedureName, Class... resultClasses);
/**
* Create an instance of <code>StoredProcedureQuery</code> for executing a
* stored procedure in the database.
* <p>Parameters must be registered before the stored procedure can
* be executed.
* <p>The <code>resultSetMapping</code> arguments must be specified in the order
* in which the result sets will be returned by the stored
* procedure invocation.
* @param procedureName name of the stored procedure in the
* database
* @param resultSetMappings the names of the result set mappings
* to be used in mapping result sets
* returned by the stored procedure
* @return the new stored procedure query instance
* @throws IllegalArgumentException if a stored procedure or
* result set mapping of the given name does not exist
* (or the query execution will fail)
*/
public StoredProcedureQuery createStoredProcedureQuery(
String procedureName, String... resultSetMappings);
/**
* Indicate to the entity manager that a JTA transaction is
* active and join the persistence context to it.
* <p>This method should be called on a JTA application
* managed entity manager that was created outside the scope
* of the active transaction or on an entity manager of type
* <code>SynchronizationType.UNSYNCHRONIZED</code> to associate
* it with the current JTA transaction.
* @throws TransactionRequiredException if there is
* no transaction
*/
public void joinTransaction();
/**
* Determine whether the entity manager is joined to the
* current transaction. Returns false if the entity manager
* is not joined to the current transaction or if no
* transaction is active
* @return boolean
* @since 2.1
*/
public boolean isJoinedToTransaction();
/**
* Return an object of the specified type to allow access to the
* provider-specific API. If the provider's <code>EntityManager</code>
* implementation does not support the specified class, the
* <code>PersistenceException</code> is thrown.
* @param cls the class of the object to be returned. This is
* normally either the underlying <code>EntityManager</code> implementation
* class or an interface that it implements.
* @return an instance of the specified class
* @throws PersistenceException if the provider does not
* support the call
* @since 2.0
*/
public <T> T unwrap(Class<T> cls);
/**
* Return the underlying provider object for the <code>EntityManager</code>,
* if available. The result of this method is implementation
* specific.
* <p>The <code>unwrap</code> method is to be preferred for new applications.
* @return underlying provider object for EntityManager
*/
public Object getDelegate();
/**
* Close an application-managed entity manager.
* After the close method has been invoked, all methods
* on the <code>EntityManager</code> instance and any
* <code>Query</code>, <code>TypedQuery</code>, and
* <code>StoredProcedureQuery</code> objects obtained from
* it will throw the <code>IllegalStateException</code>
* except for <code>getProperties</code>,
* <code>getTransaction</code>, and <code>isOpen</code> (which will return false).
* If this method is called when the entity manager is
* joined to an active transaction, the persistence
* context remains managed until the transaction completes.
* @throws IllegalStateException if the entity manager
* is container-managed
*/
public void close();
/**
* Determine whether the entity manager is open.
* @return true until the entity manager has been closed
*/
public boolean isOpen();
/**
* Return the resource-level <code>EntityTransaction</code> object.
* The <code>EntityTransaction</code> instance may be used serially to
* begin and commit multiple transactions.
* @return EntityTransaction instance
* @throws IllegalStateException if invoked on a JTA
* entity manager
*/
public EntityTransaction getTransaction();
/**
* Return the entity manager factory for the entity manager.
* @return EntityManagerFactory instance
* @throws IllegalStateException if the entity manager has
* been closed
* @since 2.0
*/
public EntityManagerFactory getEntityManagerFactory();
/**
* Return an instance of <code>CriteriaBuilder</code> for the creation of
* <code>CriteriaQuery</code> objects.
* @return CriteriaBuilder instance
* @throws IllegalStateException if the entity manager has
* been closed
* @since 2.0
*/
public CriteriaBuilder getCriteriaBuilder();
/**
* Return an instance of <code>Metamodel</code> interface for access to the
* metamodel of the persistence unit.
* @return Metamodel instance
* @throws IllegalStateException if the entity manager has
* been closed
* @since 2.0
*/
public Metamodel getMetamodel();
/**
* Return a mutable EntityGraph that can be used to dynamically create an
* EntityGraph.
* @param rootType class of entity graph
* @return entity graph
* @since 2.1
*/
public <T> EntityGraph<T> createEntityGraph(Class<T> rootType);
/**
* Return a mutable copy of the named EntityGraph. If there
* is no entity graph with the specified name, null is returned.
* @param graphName name of an entity graph
* @return entity graph
* @since 2.1
*/
public EntityGraph<?> createEntityGraph(String graphName);
/**
* Return a named EntityGraph. The returned EntityGraph
* should be considered immutable.
* @param graphName name of an existing entity graph
* @return named entity graph
* @throws IllegalArgumentException if there is no EntityGraph of
* the given name
* @since 2.1
*/
public EntityGraph<?> getEntityGraph(String graphName);
/**
* Return all named EntityGraphs that have been defined for the provided
* class type.
* @param entityClass entity class
* @return list of all entity graphs defined for the entity
* @throws IllegalArgumentException if the class is not an entity
* @since 2.1
*/
public <T> List<EntityGraph<? super T>> getEntityGraphs(Class<T> entityClass);
}