Lines 12-17
Link Here
|
12 |
******************************************************************************/ |
12 |
******************************************************************************/ |
13 |
package org.eclipse.persistence.testing.tests.jpa.fetchgroups; |
13 |
package org.eclipse.persistence.testing.tests.jpa.fetchgroups; |
14 |
|
14 |
|
|
|
15 |
import org.eclipse.persistence.exceptions.ValidationException; |
16 |
import java.io.ByteArrayInputStream; |
17 |
import java.io.ByteArrayOutputStream; |
18 |
import java.io.InputStream; |
19 |
import java.io.ObjectInputStream; |
20 |
import java.io.ObjectOutputStream; |
21 |
import java.io.OutputStream; |
22 |
|
15 |
import java.io.IOException; |
23 |
import java.io.IOException; |
16 |
import java.io.Serializable; |
24 |
import java.io.Serializable; |
17 |
import java.util.HashMap; |
25 |
import java.util.HashMap; |
Lines 185-190
Link Here
|
185 |
public void findEmptyFetchGroup_setUnfetchedSalary() throws Exception { |
193 |
public void findEmptyFetchGroup_setUnfetchedSalary() throws Exception { |
186 |
EntityManager em = createEntityManager(); |
194 |
EntityManager em = createEntityManager(); |
187 |
int minId = minimumEmployeeId(em); |
195 |
int minId = minimumEmployeeId(em); |
|
|
196 |
beginTransaction(em); |
188 |
|
197 |
|
189 |
assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
198 |
assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
190 |
|
199 |
|
Lines 237-242
Link Here
|
237 |
for (PhoneNumber phone : emp.getPhoneNumbers()) { |
246 |
for (PhoneNumber phone : emp.getPhoneNumbers()) { |
238 |
assertNoFetchGroup(phone); |
247 |
assertNoFetchGroup(phone); |
239 |
} |
248 |
} |
|
|
249 |
commitTransaction(em); |
250 |
closeEntityManager(em); |
240 |
} |
251 |
} |
241 |
|
252 |
|
242 |
/** |
253 |
/** |
Lines 288-293
Link Here
|
288 |
@Test |
299 |
@Test |
289 |
public void singleResultEmptyFetchGroup() throws Exception { |
300 |
public void singleResultEmptyFetchGroup() throws Exception { |
290 |
EntityManager em = createEntityManager(); |
301 |
EntityManager em = createEntityManager(); |
|
|
302 |
beginTransaction(em); |
291 |
|
303 |
|
292 |
Query query = em.createQuery("SELECT e FROM Employee e WHERE e.id = :ID"); |
304 |
Query query = em.createQuery("SELECT e FROM Employee e WHERE e.id = :ID"); |
293 |
query.setParameter("ID", minimumEmployeeId(em)); |
305 |
query.setParameter("ID", minimumEmployeeId(em)); |
Lines 338-343
Link Here
|
338 |
for (PhoneNumber phone : emp.getPhoneNumbers()) { |
350 |
for (PhoneNumber phone : emp.getPhoneNumbers()) { |
339 |
assertNoFetchGroup(phone); |
351 |
assertNoFetchGroup(phone); |
340 |
} |
352 |
} |
|
|
353 |
commitTransaction(em); |
354 |
closeEntityManager(em); |
341 |
} |
355 |
} |
342 |
|
356 |
|
343 |
/** |
357 |
/** |
Lines 346-351
Link Here
|
346 |
@Test |
360 |
@Test |
347 |
public void resultListEmptyFetchGroup() throws Exception { |
361 |
public void resultListEmptyFetchGroup() throws Exception { |
348 |
EntityManager em = createEntityManager(); |
362 |
EntityManager em = createEntityManager(); |
|
|
363 |
beginTransaction(em); |
349 |
|
364 |
|
350 |
Query query = em.createQuery("SELECT e FROM Employee e WHERE e.id = :ID"); |
365 |
Query query = em.createQuery("SELECT e FROM Employee e WHERE e.id = :ID"); |
351 |
query.setParameter("ID", minimumEmployeeId(em)); |
366 |
query.setParameter("ID", minimumEmployeeId(em)); |
Lines 399-404
Link Here
|
399 |
assertNoFetchGroup(phone); |
414 |
assertNoFetchGroup(phone); |
400 |
} |
415 |
} |
401 |
assertEquals(5, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
416 |
assertEquals(5, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
|
|
417 |
commitTransaction(em); |
418 |
closeEntityManager(em); |
402 |
} |
419 |
} |
403 |
|
420 |
|
404 |
/** |
421 |
/** |
Lines 407-412
Link Here
|
407 |
@Test |
424 |
@Test |
408 |
public void resultListPeriodFetchGroup() throws Exception { |
425 |
public void resultListPeriodFetchGroup() throws Exception { |
409 |
EntityManager em = createEntityManager(); |
426 |
EntityManager em = createEntityManager(); |
|
|
427 |
beginTransaction(em); |
410 |
|
428 |
|
411 |
Query query = em.createQuery("SELECT e FROM Employee e WHERE e.id = :ID"); |
429 |
Query query = em.createQuery("SELECT e FROM Employee e WHERE e.id = :ID"); |
412 |
query.setParameter("ID", minimumEmployeeId(em)); |
430 |
query.setParameter("ID", minimumEmployeeId(em)); |
Lines 461-471
Link Here
|
461 |
assertNoFetchGroup(phone); |
479 |
assertNoFetchGroup(phone); |
462 |
} |
480 |
} |
463 |
assertEquals(5, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
481 |
assertEquals(5, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
|
|
482 |
commitTransaction(em); |
483 |
closeEntityManager(em); |
464 |
} |
484 |
} |
465 |
|
485 |
|
466 |
@Test |
486 |
@Test |
467 |
public void managerFetchGroup() throws Exception { |
487 |
public void managerFetchGroup() throws Exception { |
468 |
EntityManager em = createEntityManager(); |
488 |
EntityManager em = createEntityManager(); |
|
|
489 |
beginTransaction(em); |
469 |
|
490 |
|
470 |
// Use q query since find will only use default fetch group |
491 |
// Use q query since find will only use default fetch group |
471 |
// Query query = em.createQuery("SELECT e FROM Employee e WHERE e.id = :ID"); |
492 |
// Query query = em.createQuery("SELECT e FROM Employee e WHERE e.id = :ID"); |
Lines 510-520
Link Here
|
510 |
} |
531 |
} |
511 |
|
532 |
|
512 |
assertEquals(3 + nSqlToAdd, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
533 |
assertEquals(3 + nSqlToAdd, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
|
|
534 |
commitTransaction(em); |
535 |
closeEntityManager(em); |
513 |
} |
536 |
} |
514 |
|
537 |
|
515 |
@Test |
538 |
@Test |
516 |
public void managerFetchGroupWithJoinFetch() throws Exception { |
539 |
public void managerFetchGroupWithJoinFetch() throws Exception { |
517 |
EntityManager em = createEntityManager(); |
540 |
EntityManager em = createEntityManager(); |
|
|
541 |
beginTransaction(em); |
518 |
|
542 |
|
519 |
// int minId = minimumEmployeeId(em); |
543 |
// int minId = minimumEmployeeId(em); |
520 |
// assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
544 |
// assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
Lines 558-568
Link Here
|
558 |
} |
582 |
} |
559 |
|
583 |
|
560 |
assertEquals(3, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
584 |
assertEquals(3, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
|
|
585 |
commitTransaction(em); |
586 |
closeEntityManager(em); |
561 |
} |
587 |
} |
562 |
|
588 |
|
563 |
@Test |
589 |
@Test |
564 |
public void employeeNamesFetchGroup() throws Exception { |
590 |
public void employeeNamesFetchGroup() throws Exception { |
565 |
EntityManager em = createEntityManager(); |
591 |
EntityManager em = createEntityManager(); |
|
|
592 |
beginTransaction(em); |
566 |
|
593 |
|
567 |
int minId = minimumEmployeeId(em); |
594 |
int minId = minimumEmployeeId(em); |
568 |
assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
595 |
assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
Lines 611-616
Link Here
|
611 |
// If manager_id field is null then getManager() does not trigger an sql. |
638 |
// If manager_id field is null then getManager() does not trigger an sql. |
612 |
assertEquals(4, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
639 |
assertEquals(4, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
613 |
} |
640 |
} |
|
|
641 |
commitTransaction(em); |
642 |
closeEntityManager(em); |
614 |
} |
643 |
} |
615 |
|
644 |
|
616 |
@Test |
645 |
@Test |
Lines 648-653
Link Here
|
648 |
@Test |
677 |
@Test |
649 |
public void verifyUnfetchedAttributes() throws Exception { |
678 |
public void verifyUnfetchedAttributes() throws Exception { |
650 |
EntityManager em = createEntityManager(); |
679 |
EntityManager em = createEntityManager(); |
|
|
680 |
beginTransaction(em); |
651 |
|
681 |
|
652 |
TypedQuery<Employee> q = em.createQuery("SELECT e FROM Employee e WHERE e.id IN (SELECT MIN(p.id) FROM PhoneNumber p)", Employee.class); |
682 |
TypedQuery<Employee> q = em.createQuery("SELECT e FROM Employee e WHERE e.id IN (SELECT MIN(p.id) FROM PhoneNumber p)", Employee.class); |
653 |
FetchGroup fg = new FetchGroup("Employee.empty"); |
683 |
FetchGroup fg = new FetchGroup("Employee.empty"); |
Lines 671-676
Link Here
|
671 |
|
701 |
|
672 |
assertTrue(emp.getPhoneNumbers().size() > 0); |
702 |
assertTrue(emp.getPhoneNumbers().size() > 0); |
673 |
assertEquals(3, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
703 |
assertEquals(3, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
|
|
704 |
commitTransaction(em); |
705 |
closeEntityManager(em); |
674 |
} |
706 |
} |
675 |
|
707 |
|
676 |
@Test |
708 |
@Test |
Lines 925-1043
Link Here
|
925 |
public void partialMerge() throws Exception { |
957 |
public void partialMerge() throws Exception { |
926 |
EntityManager em = createEntityManager(); |
958 |
EntityManager em = createEntityManager(); |
927 |
// Search for an Employee with an Address and Phone Numbers |
959 |
// Search for an Employee with an Address and Phone Numbers |
928 |
TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e WHERE e.address IS NOT NULL AND e.id IN (SELECT MIN(p.id) FROM PhoneNumber p)", Employee.class); |
960 |
try { |
929 |
|
961 |
beginTransaction(em); |
930 |
// Load only its names and phone Numbers |
962 |
TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e WHERE e.address IS NOT NULL AND e.id IN (SELECT MIN(p.id) FROM PhoneNumber p)", Employee.class); |
931 |
FetchGroup fetchGroup = new FetchGroup(); |
963 |
|
932 |
fetchGroup.addAttribute("firstName"); |
964 |
// Load only its names and phone Numbers |
933 |
fetchGroup.addAttribute("lastName"); |
965 |
FetchGroup fetchGroup = new FetchGroup(); |
934 |
FetchGroup phonesFG = phoneDescriptor.getFetchGroupManager().createFullFetchGroup(); |
966 |
fetchGroup.addAttribute("firstName"); |
935 |
// that ensures the owner is not instantiated |
967 |
fetchGroup.addAttribute("lastName"); |
936 |
phonesFG.addAttribute("owner.id"); |
968 |
FetchGroup phonesFG = phoneDescriptor.getFetchGroupManager().createFullFetchGroup(); |
937 |
phonesFG.removeAttribute("status"); |
969 |
// that ensures the owner is not instantiated |
938 |
phonesFG.setShouldLoad(true); |
970 |
phonesFG.addAttribute("owner.id"); |
939 |
fetchGroup.addAttribute("phoneNumbers", phonesFG); |
971 |
phonesFG.removeAttribute("status"); |
940 |
|
972 |
phonesFG.setShouldLoad(true); |
941 |
// Make sure the FetchGroup also forces the relationships to be loaded |
973 |
fetchGroup.addAttribute("phoneNumbers", phonesFG); |
942 |
fetchGroup.setShouldLoad(true); |
974 |
|
943 |
query.setHint(QueryHints.FETCH_GROUP, fetchGroup); |
975 |
// Make sure the FetchGroup also forces the relationships to be loaded |
944 |
|
976 |
fetchGroup.setShouldLoad(true); |
945 |
Employee emp = query.getSingleResult(); |
977 |
query.setHint(QueryHints.FETCH_GROUP, fetchGroup); |
946 |
|
978 |
|
947 |
// Detach Employee through Serialization |
979 |
Employee emp = query.getSingleResult(); |
948 |
Employee detachedEmp = (Employee) SerializationHelper.clone(emp); |
980 |
|
949 |
// Modify the detached Employee inverting the names, adding a phone number, and setting the salary |
981 |
// Detach Employee through Serialization |
950 |
detachedEmp.setFirstName(emp.getLastName()); |
982 |
Employee detachedEmp; |
951 |
detachedEmp.setLastName(emp.getFirstName()); |
983 |
if (isOnServer()) { |
952 |
detachedEmp.addPhoneNumber(new PhoneNumber("TEST", "999", "999999")); |
984 |
detachedEmp = (Employee) clone(emp); |
953 |
// NOte that salary was not part of the original FetchGroupdetachedEmp.setSalary(1); |
985 |
} else { |
954 |
detachedEmp.setSalary(1); |
986 |
detachedEmp = (Employee) SerializationHelper.clone(emp); |
955 |
|
987 |
} |
956 |
beginTransaction(em); |
988 |
|
957 |
// Merge the detached employee |
989 |
// Modify the detached Employee inverting the names, adding a phone number, and setting the salary |
958 |
em.merge(detachedEmp); |
990 |
detachedEmp.setFirstName(emp.getLastName()); |
959 |
// Flush the changes to the database |
991 |
detachedEmp.setLastName(emp.getFirstName()); |
960 |
em.flush(); |
992 |
detachedEmp.addPhoneNumber(new PhoneNumber("TEST", "999", "999999")); |
961 |
rollbackTransaction(em); |
993 |
// NOte that salary was not part of the original FetchGroupdetachedEmp.setSalary(1); |
|
|
994 |
detachedEmp.setSalary(1); |
995 |
|
996 |
// Merge the detached employee |
997 |
em.merge(detachedEmp); |
998 |
// Flush the changes to the database |
999 |
em.flush(); |
1000 |
} finally { |
1001 |
rollbackTransaction(em); |
1002 |
closeEntityManager(em); |
1003 |
} |
962 |
} |
1004 |
} |
963 |
|
1005 |
|
964 |
public void copyGroupMerge() { |
1006 |
public void copyGroupMerge() { |
965 |
// Search for an Employee with an Address and Phone Numbers |
1007 |
// Search for an Employee with an Address and Phone Numbers |
966 |
EntityManager em = createEntityManager(); |
1008 |
// unWrap not supported on 1.0 |
967 |
TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e WHERE e.id IN (SELECT MIN(ee.id) FROM Employee ee)", Employee.class); |
1009 |
if (! isJPA10()) { |
968 |
Employee emp = query.getSingleResult(); |
1010 |
EntityManager em = createEntityManager(); |
969 |
assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
1011 |
try { |
970 |
System.out.println(">>> Employee retrieved"); |
1012 |
beginTransaction(em); |
971 |
|
1013 |
TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e WHERE e.id IN (SELECT MIN(ee.id) FROM Employee ee)", Employee.class); |
972 |
// Copy only its names and phone Numbers |
1014 |
Employee emp = query.getSingleResult(); |
973 |
AttributeGroup group = new CopyGroup(); |
1015 |
assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
974 |
group.addAttribute("firstName"); |
1016 |
System.out.println(">>> Employee retrieved"); |
975 |
group.addAttribute("lastName"); |
1017 |
|
976 |
group.addAttribute("address"); |
1018 |
// Copy only its names and phone Numbers |
977 |
|
1019 |
AttributeGroup group = new CopyGroup(); |
978 |
Employee empCopy = (Employee) em.unwrap(JpaEntityManager.class).copy(emp, group); |
1020 |
group.addAttribute("firstName"); |
979 |
assertEquals(2, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
1021 |
group.addAttribute("lastName"); |
980 |
System.out.println(">>> Employee copied"); |
1022 |
group.addAttribute("address"); |
981 |
|
1023 |
|
982 |
// Modify the detached Employee inverting the names, adding a phone number, and setting the salary |
1024 |
Employee empCopy = (Employee) em.unwrap(JpaEntityManager.class).copy(emp, group); |
983 |
empCopy.setFirstName(emp.getLastName()); |
1025 |
assertEquals(2, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
984 |
empCopy.setLastName(emp.getFirstName()); |
1026 |
System.out.println(">>> Employee copied"); |
985 |
|
1027 |
|
986 |
// Note that salary was not part of the original FetchGroup |
1028 |
// Modify the detached Employee inverting the names, adding a phone number, and setting the salary |
987 |
//empCopy.setSalary(1); |
1029 |
empCopy.setFirstName(emp.getLastName()); |
988 |
|
1030 |
empCopy.setLastName(emp.getFirstName()); |
989 |
beginTransaction(em); |
1031 |
|
990 |
// Merge the detached employee |
1032 |
// Note that salary was not part of the original FetchGroup |
991 |
em.merge(empCopy); |
1033 |
//empCopy.setSalary(1); |
992 |
System.out.println(">>> Sparse merge complete"); |
1034 |
|
993 |
|
1035 |
// Merge the detached employee |
994 |
// Flush the changes to the database |
1036 |
em.merge(empCopy); |
995 |
em.flush(); |
1037 |
System.out.println(">>> Sparse merge complete"); |
996 |
System.out.println(">>> Flush complete"); |
1038 |
|
997 |
|
1039 |
// Flush the changes to the database |
998 |
rollbackTransaction(em); |
1040 |
em.flush(); |
|
|
1041 |
System.out.println(">>> Flush complete"); |
1042 |
|
1043 |
} finally { |
1044 |
rollbackTransaction(em); |
1045 |
closeEntityManager(em); |
1046 |
} |
1047 |
} |
999 |
} |
1048 |
} |
1000 |
|
1049 |
|
1001 |
public void copyGroupMerge2() { |
1050 |
public void copyGroupMerge2() { |
1002 |
// Search for an Employee with an Address and Phone Numbers |
1051 |
// Search for an Employee with an Address and Phone Numbers |
1003 |
EntityManager em = createEntityManager(); |
1052 |
// unWrap not supported on 1.0 |
1004 |
TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e", Employee.class); |
1053 |
if (! isJPA10()) { |
1005 |
query.setHint(QueryHints.BATCH, "e.address"); |
1054 |
EntityManager em = createEntityManager(); |
1006 |
List<Employee> employees = query.getResultList(); |
1055 |
try { |
1007 |
assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
1056 |
beginTransaction(em); |
1008 |
System.out.println(">>> Employees retrieved"); |
1057 |
TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e", Employee.class); |
1009 |
|
1058 |
query.setHint(QueryHints.BATCH, "e.address"); |
1010 |
// Copy only its names and phone Numbers |
1059 |
List<Employee> employees = query.getResultList(); |
1011 |
AttributeGroup group = new CopyGroup(); |
1060 |
assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
1012 |
group.addAttribute("firstName"); |
1061 |
System.out.println(">>> Employees retrieved"); |
1013 |
group.addAttribute("lastName"); |
1062 |
|
1014 |
group.addAttribute("address"); |
1063 |
// Copy only its names and phone Numbers |
1015 |
|
1064 |
AttributeGroup group = new CopyGroup(); |
1016 |
List<Employee> employeesCopy = (List<Employee>) em.unwrap(JpaEntityManager.class).copy(employees, group); |
1065 |
group.addAttribute("firstName"); |
1017 |
assertEquals(2, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
1066 |
group.addAttribute("lastName"); |
1018 |
System.out.println(">>> Employees copied"); |
1067 |
group.addAttribute("address"); |
1019 |
|
1068 |
|
1020 |
beginTransaction(em); |
1069 |
List<Employee> employeesCopy = (List<Employee>) em.unwrap(JpaEntityManager.class).copy(employees, group); |
1021 |
for(Employee empCopy : employeesCopy) { |
1070 |
assertEquals(2, getQuerySQLTracker(em).getTotalSQLSELECTCalls()); |
1022 |
// Modify the detached Employee inverting the names, adding a phone number, and setting the salary |
1071 |
System.out.println(">>> Employees copied"); |
1023 |
String firstName = empCopy.getFirstName(); |
1072 |
|
1024 |
String lastName = empCopy.getLastName(); |
1073 |
for(Employee empCopy : employeesCopy) { |
1025 |
empCopy.setFirstName(lastName); |
1074 |
// Modify the detached Employee inverting the names, adding a phone number, and setting the salary |
1026 |
empCopy.setLastName(firstName); |
1075 |
String firstName = empCopy.getFirstName(); |
1027 |
|
1076 |
String lastName = empCopy.getLastName(); |
1028 |
// Note that salary was not part of the original FetchGroup |
1077 |
empCopy.setFirstName(lastName); |
1029 |
//empCopy.setSalary(1); |
1078 |
empCopy.setLastName(firstName); |
1030 |
|
1079 |
|
1031 |
// Merge the detached employee |
1080 |
// Note that salary was not part of the original FetchGroup |
1032 |
em.merge(empCopy); |
1081 |
//empCopy.setSalary(1); |
|
|
1082 |
|
1083 |
// Merge the detached employee |
1084 |
em.merge(empCopy); |
1085 |
} |
1086 |
System.out.println(">>> Sparse merge complete"); |
1087 |
|
1088 |
// Flush the changes to the database |
1089 |
em.flush(); |
1090 |
System.out.println(">>> Flush complete"); |
1091 |
|
1092 |
} finally { |
1093 |
rollbackTransaction(em); |
1094 |
closeEntityManager(em); |
1095 |
} |
1033 |
} |
1096 |
} |
1034 |
System.out.println(">>> Sparse merge complete"); |
|
|
1035 |
|
1036 |
// Flush the changes to the database |
1037 |
em.flush(); |
1038 |
System.out.println(">>> Flush complete"); |
1039 |
|
1040 |
rollbackTransaction(em); |
1041 |
} |
1097 |
} |
1042 |
|
1098 |
|
1043 |
public void copyNoPk() { |
1099 |
public void copyNoPk() { |
Lines 1083-1092
Link Here
|
1083 |
} |
1139 |
} |
1084 |
} |
1140 |
} |
1085 |
} |
1141 |
} |
1086 |
|
|
|
1087 |
|
1142 |
|
1088 |
private <T> T serialize(Serializable entity) throws IOException, ClassNotFoundException { |
1143 |
private <T> T serialize(Serializable entity) throws IOException, ClassNotFoundException { |
1089 |
byte[] bytes = SerializationHelper.serialize(entity); |
1144 |
if (isOnServer()) { |
1090 |
return (T) SerializationHelper.deserialize(bytes); |
1145 |
ByteArrayOutputStream outStream = new ByteArrayOutputStream(512); |
|
|
1146 |
serialize(entity, outStream); |
1147 |
byte[] bytes = outStream.toByteArray(); |
1148 |
ByteArrayInputStream inStream = new ByteArrayInputStream(bytes); |
1149 |
return (T) deserialize(inStream); |
1150 |
} else { |
1151 |
byte[] bytes = SerializationHelper.serialize(entity); |
1152 |
return (T) SerializationHelper.deserialize(bytes); |
1153 |
} |
1091 |
} |
1154 |
} |
|
|
1155 |
|
1156 |
// The following private method are only for server testing since the entity couldn't be loaded if call from SerializationHelper. |
1157 |
private static Object clone(Serializable object) throws IOException, ClassNotFoundException { |
1158 |
ByteArrayOutputStream outStream = new ByteArrayOutputStream(512); |
1159 |
serialize(object, outStream); |
1160 |
byte[] bytes = outStream.toByteArray(); |
1161 |
return deserialize(bytes); |
1162 |
} |
1163 |
|
1164 |
private static void serialize(Serializable obj, OutputStream outputStream) throws IOException { |
1165 |
if (outputStream == null) { |
1166 |
throw ValidationException.invalidNullMethodArguments(); |
1167 |
} |
1168 |
ObjectOutputStream outStream = null; |
1169 |
|
1170 |
try { |
1171 |
outStream = new ObjectOutputStream(outputStream); |
1172 |
outStream.writeObject(obj); |
1173 |
} finally { |
1174 |
try { |
1175 |
if (outStream != null) { |
1176 |
outStream.close(); |
1177 |
} |
1178 |
} catch (IOException ex) { |
1179 |
// ignore; |
1180 |
} |
1181 |
} |
1182 |
} |
1183 |
|
1184 |
private static Object deserialize(InputStream inputStream) throws IOException, ClassNotFoundException { |
1185 |
if (inputStream == null) { |
1186 |
throw new IllegalArgumentException("The inputStream argument cannot be null"); |
1187 |
} |
1188 |
ObjectInputStream inStream = null; |
1189 |
try { |
1190 |
inStream = new ObjectInputStream(inputStream); |
1191 |
return inStream.readObject(); |
1192 |
} finally { |
1193 |
try { |
1194 |
if (inStream != null) { |
1195 |
inStream.close(); |
1196 |
} |
1197 |
} catch (IOException ex) { |
1198 |
// ignore |
1199 |
} |
1200 |
} |
1201 |
} |
1202 |
|
1203 |
private static Object deserialize(byte[] objectBytes) throws IOException, ClassNotFoundException { |
1204 |
if (objectBytes == null) { |
1205 |
throw ValidationException.invalidNullMethodArguments(); |
1206 |
} |
1207 |
ByteArrayInputStream inStream = new ByteArrayInputStream(objectBytes); |
1208 |
return deserialize(inStream); |
1209 |
} |
1092 |
} |
1210 |
} |