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 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530
|
/*
* Copyright (c) 1998, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 IBM Corporation. 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:
// Oracle - initial API and implementation from Oracle TopLink
// 08/23/2010-2.2 Michael O'Brien
// - 323043: application.xml module ordering may cause weaving not to occur causing an NPE.
// warn if expected "_persistence_//_vh" method not found
// instead of throwing NPE during deploy validation.
// 11/19/2012-2.5 Guy Pelletier
// - 389090: JPA 2.1 DDL Generation Support (foreign key metadata support)
// 12/07/2012-2.5 Guy Pelletier
// - 389090: JPA 2.1 DDL Generation Support (foreign key metadata support)
// 08/12/2015-2.6 Mythily Parthasarathy
// - 474752: Address NPE for Embeddable with 1-M association
package org.eclipse.persistence.mappings;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import org.eclipse.persistence.annotations.BatchFetchType;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.descriptors.partitioning.PartitioningPolicy;
import org.eclipse.persistence.exceptions.DatabaseException;
import org.eclipse.persistence.exceptions.DescriptorException;
import org.eclipse.persistence.exceptions.QueryException;
import org.eclipse.persistence.exceptions.ValidationException;
import org.eclipse.persistence.expressions.Expression;
import org.eclipse.persistence.expressions.ExpressionBuilder;
import org.eclipse.persistence.history.AsOfClause;
import org.eclipse.persistence.indirection.ValueHolder;
import org.eclipse.persistence.indirection.ValueHolderInterface;
import org.eclipse.persistence.internal.descriptors.DescriptorIterator;
import org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor;
import org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor;
import org.eclipse.persistence.internal.expressions.ForUpdateOfClause;
import org.eclipse.persistence.internal.expressions.ObjectExpression;
import org.eclipse.persistence.internal.helper.ClassConstants;
import org.eclipse.persistence.internal.helper.DatabaseField;
import org.eclipse.persistence.internal.helper.Helper;
import org.eclipse.persistence.internal.helper.NonSynchronizedSubVector;
import org.eclipse.persistence.internal.helper.NonSynchronizedVector;
import org.eclipse.persistence.internal.identitymaps.CacheId;
import org.eclipse.persistence.internal.identitymaps.CacheKey;
import org.eclipse.persistence.internal.indirection.BasicIndirectionPolicy;
import org.eclipse.persistence.internal.indirection.ContainerIndirectionPolicy;
import org.eclipse.persistence.internal.indirection.DatabaseValueHolder;
import org.eclipse.persistence.internal.indirection.IndirectionPolicy;
import org.eclipse.persistence.internal.indirection.NoIndirectionPolicy;
import org.eclipse.persistence.internal.indirection.WeavedObjectBasicIndirectionPolicy;
import org.eclipse.persistence.internal.queries.AttributeItem;
import org.eclipse.persistence.internal.queries.JoinedAttributeManager;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
import org.eclipse.persistence.internal.security.PrivilegedClassForName;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.internal.sessions.ChangeRecord;
import org.eclipse.persistence.internal.sessions.MergeManager;
import org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet;
import org.eclipse.persistence.internal.sessions.UnitOfWorkImpl;
import org.eclipse.persistence.internal.sessions.remote.RemoteSessionController;
import org.eclipse.persistence.internal.sessions.remote.RemoteValueHolder;
import org.eclipse.persistence.logging.SessionLog;
import org.eclipse.persistence.queries.BatchFetchPolicy;
import org.eclipse.persistence.queries.Call;
import org.eclipse.persistence.queries.DatabaseQuery;
import org.eclipse.persistence.queries.FetchGroup;
import org.eclipse.persistence.queries.ObjectBuildingQuery;
import org.eclipse.persistence.queries.ObjectLevelModifyQuery;
import org.eclipse.persistence.queries.ObjectLevelReadQuery;
import org.eclipse.persistence.queries.ReadAllQuery;
import org.eclipse.persistence.queries.ReadObjectQuery;
import org.eclipse.persistence.queries.ReadQuery;
import org.eclipse.persistence.queries.ReportQuery;
import org.eclipse.persistence.sessions.DatabaseRecord;
import org.eclipse.persistence.sessions.remote.DistributedSession;
/**
* <b>Purpose</b>: Abstract class for relationship mappings
*/
public abstract class ForeignReferenceMapping extends DatabaseMapping {
/** Query parameter name used for IN batch ids. */
public static final String QUERY_BATCH_PARAMETER = "query-batch-parameter";
/** This is used only in descriptor proxy in remote session */
protected Class referenceClass;
protected String referenceClassName;
/** The session is temporarily used for initialization. Once used, it is set to null */
protected transient AbstractSession tempInitSession;
/** The descriptor of the reference class. */
protected transient ClassDescriptor referenceDescriptor;
/** This query is used to read referenced objects for this mapping. */
protected ReadQuery selectionQuery;
/** Indicates whether the referenced object is privately owned or not. */
protected boolean isPrivateOwned;
/**
* Indicates whether the referenced object should always be batch read on read all queries,
* and defines the type of batch fetch to use.
*/
protected BatchFetchType batchFetchType;
/** Implements indirection behavior */
protected IndirectionPolicy indirectionPolicy;
/** Indicates whether the selection query is TopLink generated or defined by the user. */
protected transient boolean hasCustomSelectionQuery;
/** Used to reference the other half of a bi-directional relationship. */
protected DatabaseMapping relationshipPartner;
/** Set by users, used to retrieve the backpointer for this mapping */
protected String relationshipPartnerAttributeName;
/** Cascading flags used by the EntityManager */
protected boolean cascadePersist;
protected boolean cascadeMerge;
protected boolean cascadeRefresh;
protected boolean cascadeRemove;
protected boolean cascadeDetach;
/** Flag used to determine if we need to weave the transient annotation on weaved fields.*/
protected boolean requiresTransientWeavedFields;
/** Define if the relationship should always be join fetched. */
protected int joinFetch = NONE;
/** Specify any INNER join on a join fetch. */
public static final int INNER_JOIN = 1;
/** Specify any OUTER join on a join fetch. */
public static final int OUTER_JOIN = 2;
/** Specify no join fetch, this is the default. */
public static final int NONE = 0;
/** This is a way (after cloning) to force the initialization of the selection criteria */
protected boolean forceInitializationOfSelectionCriteria;
/**
* Indicates whether and how pessimistic lock scope should be extended
*/
enum ExtendPessimisticLockScope {
// should not be extended.
NONE,
// should be extended in mapping's selectQuery.
TARGET_QUERY,
// should be extended in the source query.
SOURCE_QUERY,
// should be extended in a dedicated query (which doesn't do anything else).
DEDICATED_QUERY
}
ExtendPessimisticLockScope extendPessimisticLockScope;
/** Support delete cascading on the database relationship constraint. */
protected boolean isCascadeOnDeleteSetOnDatabase;
/** Allow the mapping's queries to be targeted at specific connection pools. */
protected PartitioningPolicy partitioningPolicy;
/** Allow the mapping's queries to be targeted at specific connection pools. */
protected String partitioningPolicyName;
/** Stores JPA metadata about whether another mapping is the owning mapping. Only populated for JPA models **/
protected String mappedBy;
protected ForeignReferenceMapping() {
this.isPrivateOwned = false;
this.hasCustomSelectionQuery = false;
this.useBasicIndirection();
this.cascadePersist = false;
this.cascadeMerge = false;
this.cascadeRefresh = false;
this.cascadeRemove = false;
this.requiresTransientWeavedFields = true;
this.forceInitializationOfSelectionCriteria = false;
this.extendPessimisticLockScope = ExtendPessimisticLockScope.NONE;
}
/**
* ADVANCED: Allows the retrieval of the owning mapping for a particular
* mapping. Note: This will only be set for JPA models
*
* @return
*/
public String getMappedBy() {
return mappedBy;
}
/**
* PUBLIC:
* Return the mapping's partitioning policy.
*/
public PartitioningPolicy getPartitioningPolicy() {
return partitioningPolicy;
}
/**
* PUBLIC:
* Set the mapping's partitioning policy.
* A PartitioningPolicy is used to partition, load-balance or replicate data across multiple difference databases
* or across a database cluster such as Oracle RAC.
* Partitioning can provide improved scalability by allowing multiple database machines to service requests.
* Setting a policy on a mapping will set the policy on all of its mappings.
*/
public void setPartitioningPolicy(PartitioningPolicy partitioningPolicy) {
this.partitioningPolicy = partitioningPolicy;
}
/**
* PUBLIC:
* Return the name of the mapping's partitioning policy.
* A PartitioningPolicy with the same name must be defined on the Project.
* A PartitioningPolicy is used to partition the data for a class across multiple difference databases
* or across a database cluster such as Oracle RAC.
* Partitioning can provide improved scalability by allowing multiple database machines to service requests.
*/
public String getPartitioningPolicyName() {
return partitioningPolicyName;
}
/**
* PUBLIC:
* Set the name of the mapping's partitioning policy.
* A PartitioningPolicy with the same name must be defined on the Project.
* A PartitioningPolicy is used to partition the data for a class across multiple difference databases
* or across a database cluster such as Oracle RAC.
* Partitioning can provide improved scalability by allowing multiple database machines to service requests.
*/
public void setPartitioningPolicyName(String partitioningPolicyName) {
this.partitioningPolicyName = partitioningPolicyName;
}
/**
* INTERNAL:
* Retrieve the value through using batch reading.
* This executes a single query to read the target for all of the objects and stores the
* result of the batch query in the original query to allow the other objects to share the results.
*/
protected Object batchedValueFromRow(AbstractRecord row, ObjectLevelReadQuery query, CacheKey parentCacheKey) {
ReadQuery batchQuery = (ReadQuery)query.getProperty(this);
if (batchQuery == null) {
if (query.hasBatchReadAttributes()) {
Map<DatabaseMapping, ReadQuery> queries = query.getBatchFetchPolicy().getMappingQueries();
if (queries != null) {
batchQuery = queries.get(this);
}
}
if (batchQuery == null) {
batchQuery = prepareNestedBatchQuery(query);
batchQuery.setIsExecutionClone(true);
} else {
batchQuery = (ReadQuery)batchQuery.clone();
batchQuery.setIsExecutionClone(true);
}
query.setProperty(this, batchQuery);
}
return this.indirectionPolicy.valueFromBatchQuery(batchQuery, row, query, parentCacheKey);
}
/**
* INTERNAL:
* Clone the attribute from the clone and assign it to the backup.
*/
@Override
public void buildBackupClone(Object clone, Object backup, UnitOfWorkImpl unitOfWork) {
Object attributeValue = getAttributeValueFromObject(clone);
Object clonedAttributeValue = this.indirectionPolicy.backupCloneAttribute(attributeValue, clone, backup, unitOfWork);
setAttributeValueInObject(backup, clonedAttributeValue);
}
/**
* INTERNAL:
* Used during building the backup shallow copy to copy the
* target object without re-registering it.
*/
@Override
public abstract Object buildBackupCloneForPartObject(Object attributeValue, Object clone, Object backup, UnitOfWorkImpl unitOfWork);
/**
* INTERNAL:
* Clone the attribute from the original and assign it to the clone.
*/
@Override
public void buildClone(Object original, CacheKey cacheKey, Object clone, Integer refreshCascade, AbstractSession cloningSession) {
Object attributeValue = null;
if (!this.isCacheable && (cacheKey != null && !cacheKey.isIsolated())){
ReadObjectQuery query = new ReadObjectQuery(descriptor.getJavaClass());
query.setSession(cloningSession);
attributeValue = valueFromRow(cacheKey.getProtectedForeignKeys(), null, query, cacheKey, cloningSession, true, null);
}else{
attributeValue = getAttributeValueFromObject(original);
}
attributeValue = this.indirectionPolicy.cloneAttribute(attributeValue, original, cacheKey, clone, refreshCascade, cloningSession, false); // building clone from an original not a row.
//GFBug#404 - fix moved to ObjectBuildingQuery.registerIndividualResult
setAttributeValueInObject(clone, attributeValue);
}
/**
* INTERNAL:
* A combination of readFromRowIntoObject and buildClone.
* <p>
* buildClone assumes the attribute value exists on the original and can
* simply be copied.
* <p>
* readFromRowIntoObject assumes that one is building an original.
* <p>
* Both of the above assumptions are false in this method, and actually
* attempts to do both at the same time.
* <p>
* Extract value from the row and set the attribute to this value in the
* working copy clone.
* In order to bypass the shared cache when in transaction a UnitOfWork must
* be able to populate working copies directly from the row.
*/
@Override
public void buildCloneFromRow(AbstractRecord databaseRow, JoinedAttributeManager joinManager, Object clone, CacheKey sharedCacheKey, ObjectBuildingQuery sourceQuery, UnitOfWorkImpl unitOfWork, AbstractSession executionSession) {
Boolean[] wasCacheUsed = new Boolean[]{Boolean.FALSE};
Object attributeValue = valueFromRow(databaseRow, joinManager, sourceQuery, sharedCacheKey, executionSession, true, wasCacheUsed);
Object clonedAttributeValue = this.indirectionPolicy.cloneAttribute(attributeValue, null, sharedCacheKey,clone, null, unitOfWork, !wasCacheUsed[0]);// building clone directly from row.
if (executionSession.isUnitOfWork() && sourceQuery.shouldRefreshIdentityMapResult()){
// check whether the attribute is fully build before calling getAttributeValueFromObject because that
// call may fully build the attribute
boolean wasAttributeValueFullyBuilt = isAttributeValueFullyBuilt(clone);
Object oldAttribute = this.getAttributeValueFromObject(clone);
setAttributeValueInObject(clone, clonedAttributeValue); // set this first to prevent infinite recursion
if (wasAttributeValueFullyBuilt && this.indirectionPolicy.objectIsInstantiatedOrChanged(oldAttribute)){
this.indirectionPolicy.instantiateObject(clone, clonedAttributeValue);
}
}else{
setAttributeValueInObject(clone, clonedAttributeValue);
}
if((joinManager != null && joinManager.isAttributeJoined(this.descriptor, this)) || (isExtendingPessimisticLockScope(sourceQuery) && extendPessimisticLockScope == ExtendPessimisticLockScope.TARGET_QUERY) || databaseRow.hasSopObject()) {
// need to instantiate to extended the lock beyond the source object table(s).
this.indirectionPolicy.instantiateObject(clone, clonedAttributeValue);
}
}
/**
* INTERNAL:
* Require for cloning, the part must be cloned.
*/
@Override
public abstract Object buildCloneForPartObject(Object attributeValue, Object original, CacheKey cacheKey, Object clone, AbstractSession cloningSession, Integer refreshCascade, boolean isExisting, boolean isFromSharedCache);
/**
* INTERNAL:
* The mapping clones itself to create deep copy.
*/
@Override
public Object clone() {
ForeignReferenceMapping clone = (ForeignReferenceMapping)super.clone();
clone.setIndirectionPolicy((IndirectionPolicy)indirectionPolicy.clone());
clone.setSelectionQuery((ReadQuery)getSelectionQuery().clone());
return clone;
}
/**
* INTERNAL:
* This method will access the target relationship and create a list of information to rebuild the relationship.
* This method is used in combination with the CachedValueHolder to store references to PK's to be loaded from
* a cache instead of a query.
*/
public abstract Object[] buildReferencesPKList(Object entity, Object attribute, AbstractSession session);
/**
* INTERNAL: Compare the attributes belonging to this mapping for the
* objects.
*/
@Override
public boolean compareObjects(Object firstObject, Object secondObject, AbstractSession session) {
if (isPrivateOwned()) {
return compareObjectsWithPrivateOwned(firstObject, secondObject, session);
} else {
return compareObjectsWithoutPrivateOwned(firstObject, secondObject, session);
}
}
/**
* Compare two objects if their parts are not private owned
*/
protected abstract boolean compareObjectsWithoutPrivateOwned(Object first, Object second, AbstractSession session);
/**
* Compare two objects if their parts are private owned
*/
protected abstract boolean compareObjectsWithPrivateOwned(Object first, Object second, AbstractSession session);
/**
* INTERNAL:
* Convert all the class-name-based settings in this mapping to actual class-based
* settings. This method is used when converting a project that has been built
* with class names to a project with classes.
*/
@Override
public void convertClassNamesToClasses(ClassLoader classLoader){
super.convertClassNamesToClasses(classLoader);
// DirectCollection mappings don't require a reference class.
if (getReferenceClassName() != null) {
Class referenceClass = null;
try{
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
try {
referenceClass = AccessController.doPrivileged(new PrivilegedClassForName(getReferenceClassName(), true, classLoader));
} catch (PrivilegedActionException exception) {
throw ValidationException.classNotFoundWhileConvertingClassNames(getReferenceClassName(), exception.getException());
}
} else {
referenceClass = PrivilegedAccessHelper.getClassForName(getReferenceClassName(), true, classLoader);
}
} catch (ClassNotFoundException exc){
throw ValidationException.classNotFoundWhileConvertingClassNames(getReferenceClassName(), exc);
}
setReferenceClass(referenceClass);
}
if (getSelectionQuery() != null) {
getSelectionQuery().convertClassNamesToClasses(classLoader);
}
}
/**
* INTERNAL:
* Builder the unit of work value holder.
* Ignore the original object.
* @param buildDirectlyFromRow indicates that we are building the clone directly
* from a row as opposed to building the original from the row, putting it in
* the shared cache, and then cloning the original.
*/
@Override
public DatabaseValueHolder createCloneValueHolder(ValueHolderInterface attributeValue, Object original, Object clone, AbstractRecord row, AbstractSession cloningSession, boolean buildDirectlyFromRow) {
return cloningSession.createCloneQueryValueHolder(attributeValue, clone, row, this);
}
/**
* INTERNAL:
* Return true if the merge should be bypassed. This would be the case for several reasons, depending on
* the kind of merge taking place.
*/
protected boolean dontDoMerge(Object target, Object source, MergeManager mergeManager) {
if (!shouldMergeCascadeReference(mergeManager)) {
return true;
}
if (mergeManager.isForRefresh()) {
// For reverts we are more concerned about the target than the source.
if (!isAttributeValueInstantiated(target)) {
return true;
}
} else {
if (mergeManager.shouldRefreshRemoteObject() && shouldMergeCascadeParts(mergeManager) && usesIndirection()) {
return true;
} else {
if (!isAttributeValueInstantiated(source)) {
return true;
}
}
}
return false;
}
/**
* PUBLIC:
* Indicates whether the referenced object should always be batch read on read all queries.
* Batch reading will read all of the related objects in a single query when accessed from an originating read all.
* This should only be used if it is know that the related objects are always required with the source object, or indirection is not used.
*/
public void dontUseBatchReading() {
setUsesBatchReading(false);
}
/**
* PUBLIC:
* Indirection means that a ValueHolder will be put in-between the attribute and the real object.
* This allows for the reading of the target from the database to be delayed until accessed.
* This defaults to true and is strongly suggested as it give a huge performance gain.
*/
public void dontUseIndirection() {
setIndirectionPolicy(new NoIndirectionPolicy());
}
/**
* INTERNAL:
* Called if shouldExtendPessimisticLockScopeInTargetQuery() is true.
* Adds locking clause to the target query to extend pessimistic lock scope.
*/
protected void extendPessimisticLockScopeInTargetQuery(ObjectLevelReadQuery targetQuery, ObjectBuildingQuery sourceQuery) {
targetQuery.setLockMode(sourceQuery.getLockMode());
}
/**
* INTERNAL:
* Called if shouldExtendPessimisticLockScopeInSourceQuery is true.
* Adds fields to be locked to the where clause of the source query.
* Note that the sourceQuery must be ObjectLevelReadQuery so that it has ExpressionBuilder.
*
* This method must be implemented in subclasses that allow
* setting shouldExtendPessimisticLockScopeInSourceQuery to true.
*/
public void extendPessimisticLockScopeInSourceQuery(ObjectLevelReadQuery sourceQuery) {
}
/**
* INTERNAL:
* Extract the value from the batch optimized query, this should be supported by most query types.
*/
public Object extractResultFromBatchQuery(ReadQuery batchQuery, CacheKey parentCacheKey, AbstractRecord sourceRow, AbstractSession session, ObjectLevelReadQuery originalQuery) throws QueryException {
Map<Object, Object> batchedObjects;
Object result;
Object sourceKey = extractBatchKeyFromRow(sourceRow, session);
if (sourceKey == null) {
// If the foreign key was null, then just return null.
return null;
}
Object cachedObject = checkCacheForBatchKey(sourceRow, sourceKey, null, batchQuery, originalQuery, session);
if (cachedObject != null) {
// If the object is already in the cache, then just return it.
return cachedObject;
}
// Ensure the query is only executed once.
synchronized (batchQuery) {
// Check if query was already executed.
batchedObjects = batchQuery.getBatchObjects();
BatchFetchPolicy originalPolicy = originalQuery.getBatchFetchPolicy();
if (batchedObjects == null) {
batchedObjects = new Hashtable<>();
batchQuery.setBatchObjects(batchedObjects);
} else {
result = batchedObjects.get(sourceKey);
if (result == Helper.NULL_VALUE) {
return null;
// If IN may not have that batch yet, or it may have been null.
} else if ((result != null) || (!originalPolicy.isIN())) {
return result;
}
}
// In case of IN the batch including this row may not have been executed yet.
AbstractRecord translationRow = originalQuery.getTranslationRow();
if (translationRow == null) {
translationRow = new DatabaseRecord();
}
// Execute query and index resulting object sets by key.
if (originalPolicy.isIN()) {
// Need to extract all foreign key values from all parent rows for IN parameter.
List<AbstractRecord> parentRows = originalPolicy.getDataResults(this);
// Execute queries by batch if too many rows.
int rowsSize = parentRows.size();
int size = Math.min(rowsSize, originalPolicy.getSize());
if (size == 0) {
return null;
}
int startIndex = 0;
if (size != rowsSize) {
// If only fetching a page, need to make sure the row we want is in the page.
startIndex = parentRows.indexOf(sourceRow);
}
if (startIndex == -1) {
return null;
}
List foreignKeyValues = new ArrayList(size);
Set foreignKeys = new HashSet(size);
int index = 0;
int offset = startIndex;
for (int count = 0; count < size; count++) {
if (index >= rowsSize) {
// Processed all rows, done.
break;
} else if ((offset + index) >= rowsSize) {
// If passed the end, go back to start.
offset = index * -1;
}
AbstractRecord row = parentRows.get(offset + index);
// Handle duplicate rows in the ComplexQueryResult being replaced with null, as a
// result of duplicate filtering being true for constructing the ComplexQueryResult
if (row != null) {
Object foreignKey = extractBatchKeyFromRow(row, session);
if (foreignKey == null) {
// Ignore null foreign keys.
count--;
} else {
cachedObject = checkCacheForBatchKey(row, foreignKey, batchedObjects, batchQuery, originalQuery, session);
if (cachedObject != null) {
// Avoid fetching things a cache hit occurs for.
count--;
} else {
// Ensure the same id is not selected twice.
if (foreignKeys.contains(foreignKey)) {
count--;
} else {
Object[] key = ((CacheId)foreignKey).getPrimaryKey();
Object foreignKeyValue = key[0];
// Support composite keys using nested IN.
if (key.length > 1) {
foreignKeyValue = Arrays.asList(key);
}
foreignKeyValues.add(foreignKeyValue);
foreignKeys.add(foreignKey);
}
}
}
}
index++;
}
// Need to compute remaining rows, this is tricky because a page in the middle could have been processed.
List<AbstractRecord> remainingParentRows;
if (startIndex == 0) {
// Tail
remainingParentRows = new ArrayList<>(parentRows.subList(index, rowsSize));
} else if (startIndex == offset) {
// Head and tail.
remainingParentRows = new ArrayList<>(parentRows.subList(0, startIndex));
remainingParentRows.addAll(parentRows.subList(startIndex + index, rowsSize));
} else {
// Middle
remainingParentRows = new ArrayList<>(parentRows.subList(offset + index, startIndex));
}
originalPolicy.setDataResults(this, remainingParentRows);
translationRow = translationRow.clone();
translationRow.put(QUERY_BATCH_PARAMETER, foreignKeyValues);
// Register each id as null, in case it has no relationship.
for (Object foreignKey : foreignKeys) {
batchedObjects.put(foreignKey, Helper.NULL_VALUE);
}
} else if (batchQuery.isReadAllQuery() && ((ReadAllQuery)batchQuery).getBatchFetchPolicy().isIN()) {
throw QueryException.originalQueryMustUseBatchIN(this, originalQuery);
}
executeBatchQuery(batchQuery, parentCacheKey, batchedObjects, session, translationRow);
batchQuery.setSession(null);
}
result = batchedObjects.get(sourceKey);
if (result == Helper.NULL_VALUE) {
return null;
} else {
return result;
}
}
/**
* INTERNAL:
* Extract the batch key value from the source row.
* Used for batch reading, most following same order and fields as in the mapping.
* The method should be overridden by classes that support batch reading.
*/
protected Object extractBatchKeyFromRow(AbstractRecord targetRow, AbstractSession session) {
throw QueryException.batchReadingNotSupported(this, null);
}
/**
* INTERNAL:
* This method is used to store the FK fields that can be cached that correspond to noncacheable mappings
* the FK field values will be used to re-issue the query when cloning the shared cache entity
*/
@Override
public abstract void collectQueryParameters(Set<DatabaseField> cacheFields);
/**
* INTERNAL:
* Check if the target object is in the cache if possible based on the source row.
* If in the cache, add the object to the batch results.
* Return null if not possible or not in the cache.
*/
protected Object checkCacheForBatchKey(AbstractRecord sourceRow, Object foreignKey, Map batchObjects, ReadQuery batchQuery, ObjectLevelReadQuery originalQuery, AbstractSession session) {
return null;
}
/**
* INTERNAL:
* Prepare and execute the batch query and store the
* results for each source object in a map keyed by the
* mappings source keys of the source objects.
*/
protected void executeBatchQuery(DatabaseQuery query, CacheKey parentCacheKey, Map referenceObjectsByKey, AbstractSession session, AbstractRecord row) {
throw QueryException.batchReadingNotSupported(this, query);
}
/**
* INTERNAL:
* Clone and prepare the JoinedAttributeManager nested JoinedAttributeManager.
* This is used for nested joining as the JoinedAttributeManager passed to the joined build object.
*/
public ObjectLevelReadQuery prepareNestedJoins(JoinedAttributeManager joinManager, ObjectBuildingQuery baseQuery, AbstractSession session) {
// A nested query must be built to pass to the descriptor that looks like the real query execution would.
ObjectLevelReadQuery nestedQuery = (ObjectLevelReadQuery)((ObjectLevelReadQuery)getSelectionQuery()).deepClone();
nestedQuery.setSession(session);
nestedQuery.setShouldUseSerializedObjectPolicy(baseQuery.shouldUseSerializedObjectPolicy());
// Must cascade for nested partial/join attributes, the expressions must be filter to only the nested ones.
if (baseQuery.hasPartialAttributeExpressions()) {
nestedQuery.setPartialAttributeExpressions(extractNestedExpressions(((ObjectLevelReadQuery)baseQuery).getPartialAttributeExpressions(), nestedQuery.getExpressionBuilder()));
// bug 5501751: USING GETALLOWINGNULL() WITH ADDPARTIALATTRIBUTE() BROKEN IN 10.1.3
// The query against Employee with
// query.addPartialAttribute(builder.getAllowingNull("address"));
// in case there's no address returns null instead of Address object.
// Note that in case
// query.addPartialAttribute(builder.getAllowingNull("address").get("city"));
// in case there's no address an empty Address object (all atributes are nulls) is returned.
if(nestedQuery.getPartialAttributeExpressions().isEmpty()) {
if(hasRootExpressionThatShouldUseOuterJoin(((ObjectLevelReadQuery)baseQuery).getPartialAttributeExpressions())) {
nestedQuery.setShouldBuildNullForNullPk(true);
}
}
} else {
if(nestedQuery.getDescriptor().hasFetchGroupManager()) {
FetchGroup sourceFG = baseQuery.getExecutionFetchGroup();
if (sourceFG != null) {
FetchGroup targetFetchGroup = sourceFG.getGroup(getAttributeName());
if (targetFetchGroup != null) {
nestedQuery.setFetchGroup(targetFetchGroup);
nestedQuery.prepareFetchGroup();
}
}
}
List nestedJoins = extractNestedNonAggregateExpressions(joinManager.getJoinedAttributeExpressions(), nestedQuery.getExpressionBuilder(), false);
if (nestedJoins.size() > 0) {
// Recompute the joined indexes based on the nested join expressions.
nestedQuery.getJoinedAttributeManager().clear();
nestedQuery.getJoinedAttributeManager().setJoinedAttributeExpressions_(nestedJoins);
// the next line sets isToManyJoinQuery flag
nestedQuery.getJoinedAttributeManager().prepareJoinExpressions(session);
nestedQuery.getJoinedAttributeManager().computeJoiningMappingQueries(session);
nestedQuery.getJoinedAttributeManager().computeJoiningMappingIndexes(true, session, 0);
} else if (nestedQuery.hasJoining()) {
// Clear any mapping level joins.
nestedQuery.setJoinedAttributeManager(null);
}
// Configure nested locking clause.
if (baseQuery.isLockQuery()) {
if (((ObjectLevelReadQuery)baseQuery).getLockingClause().isForUpdateOfClause()) {
ForUpdateOfClause clause = (ForUpdateOfClause)((ObjectLevelReadQuery)baseQuery).getLockingClause().clone();
clause.setLockedExpressions(extractNestedNonAggregateExpressions(clause.getLockedExpressions(), nestedQuery.getExpressionBuilder(), true));
nestedQuery.setLockingClause(clause);
} else {
nestedQuery.setLockingClause(((ObjectLevelReadQuery)baseQuery).getLockingClause());
}
}
}
nestedQuery.setShouldMaintainCache(baseQuery.shouldMaintainCache());
nestedQuery.setShouldRefreshIdentityMapResult(baseQuery.shouldRefreshIdentityMapResult());
// Bug 385700 - Populate session & query class if not initialized by
// ObjectLevelReadQuery.computeBatchReadMappingQueries() in case batch query
// has been using inheritance and child descriptors can have different mappings.
// Code below will add nested batch IN support to joining, not currently enabled as JOIN and EXISTS batch types not supported yet.
/*if (baseQuery.isObjectLevelReadQuery() && ((ObjectLevelReadQuery)baseQuery).hasBatchReadAttributes()) {
ObjectLevelReadQuery baseObjectQuery = (ObjectLevelReadQuery) baseQuery;
for (Expression expression : baseObjectQuery.getBatchReadAttributeExpressions()) {
ObjectExpression batchReadExpression = (ObjectExpression) expression;
// Batch Read Attribute Expressions may not have initialized.
ExpressionBuilder expressionBuilder = batchReadExpression.getBuilder();
if (expressionBuilder.getQueryClass() == null) {
expressionBuilder.setQueryClass(baseQuery.getReferenceClass());
}
if (expressionBuilder.getSession() == null) {
expressionBuilder.setSession(baseQuery.getSession().getRootSession(null));
}
}
// Computed nested batch attribute expressions, and add them to batch query.
List<Expression> nestedExpressions = extractNestedExpressions(baseObjectQuery.getBatchReadAttributeExpressions(), nestedQuery.getExpressionBuilder());
nestedQuery.getBatchReadAttributeExpressions().addAll(nestedExpressions);
nestedQuery.setBatchFetchType(baseObjectQuery.getBatchFetchPolicy().getType());
nestedQuery.setBatchFetchSize(baseObjectQuery.getBatchFetchPolicy().getSize());
}*/
// For flashback: Must still propagate all properties, as the
// attributes of this joined attribute may be read later too.
if (baseQuery.isObjectLevelReadQuery() && ((ObjectLevelReadQuery)baseQuery).hasAsOfClause()) {
nestedQuery.setAsOfClause(((ObjectLevelReadQuery)baseQuery).getAsOfClause());
}
nestedQuery.setCascadePolicy(baseQuery.getCascadePolicy());
if (nestedQuery.hasJoining()) {
nestedQuery.getJoinedAttributeManager().computeJoiningMappingQueries(session);
}
nestedQuery.setSession(null);
nestedQuery.setRequiresDeferredLocks(baseQuery.requiresDeferredLocks());
return nestedQuery;
}
/**
* INTERNAL:
* Allow the mapping the do any further batch preparation.
*/
protected void postPrepareNestedBatchQuery(ReadQuery batchQuery, ObjectLevelReadQuery query) {
// Do nothing.
}
/**
* INTERNAL:
* Return the selection criteria used to IN batch fetching.
*/
protected Expression buildBatchCriteria(ExpressionBuilder builder, ObjectLevelReadQuery query) {
throw QueryException.batchReadingNotSupported(this, null);
}
/**
* INTERNAL:
* Clone and prepare the selection query as a nested batch read query.
* This is used for nested batch reading.
*/
public ReadQuery prepareNestedBatchQuery(ObjectLevelReadQuery query) {
// For CR#2646-S.M. In case of inheritance the descriptor to use may not be that
// of the source query (the base class descriptor), but that of the subclass, if the
// attribute is only of the subclass. Thus in this case use the descriptor from the mapping.
// Also: for Bug 5478648 - Do not switch the descriptor if the query's descriptor is an aggregate
ClassDescriptor descriptorToUse = query.getDescriptor();
if ((descriptorToUse != this.descriptor) && (!descriptorToUse.getMappings().contains(this)) && (!this.descriptor.isDescriptorTypeAggregate())) {
descriptorToUse = this.descriptor;
}
ExpressionBuilder builder = new ExpressionBuilder(this.referenceClass);
builder.setQueryClassAndDescriptor(this.referenceClass, getReferenceDescriptor());
ReadAllQuery batchQuery = new ReadAllQuery(this.referenceClass, builder);
batchQuery.setName(getAttributeName());
batchQuery.setDescriptor(getReferenceDescriptor());
batchQuery.setSession(query.getSession());
batchQuery.setShouldUseSerializedObjectPolicy(query.shouldUseSerializedObjectPolicy());
//bug 3965568
// we should not wrap the results as this is an internal query
batchQuery.setShouldUseWrapperPolicy(false);
if (query.shouldCascadeAllParts() || (query.shouldCascadePrivateParts() && isPrivateOwned()) || (query.shouldCascadeByMapping() && this.cascadeRefresh)) {
batchQuery.setShouldRefreshIdentityMapResult(query.shouldRefreshIdentityMapResult());
batchQuery.setCascadePolicy(query.getCascadePolicy());
batchQuery.setShouldMaintainCache(query.shouldMaintainCache());
if (query.hasAsOfClause()) {
batchQuery.setAsOfClause(query.getAsOfClause());
}
//bug 3802197 - cascade binding and prepare settings
batchQuery.setShouldBindAllParameters(query.getShouldBindAllParameters());
batchQuery.setShouldPrepare(query.shouldPrepare());
}
batchQuery.setShouldOuterJoinSubclasses(query.shouldOuterJoinSubclasses());
//CR #4365
batchQuery.setQueryId(query.getQueryId());
Expression batchSelectionCriteria = null;
// Build the batch query, either using joining, or an exist sub-select.
BatchFetchType batchType = query.getBatchFetchPolicy().getType();
if (this.batchFetchType != null) {
batchType = this.batchFetchType;
}
if (batchType == BatchFetchType.EXISTS) {
// Using a EXISTS sub-select (WHERE EXIST (<original-query> AND <mapping-join> AND <mapping-join>)
ExpressionBuilder subBuilder = new ExpressionBuilder(descriptorToUse.getJavaClass());
subBuilder.setQueryClassAndDescriptor(descriptorToUse.getJavaClass(), descriptorToUse);
ReportQuery subQuery = new ReportQuery(descriptorToUse.getJavaClass(), subBuilder);
subQuery.setDescriptor(descriptorToUse);
subQuery.setShouldRetrieveFirstPrimaryKey(true);
Expression subCriteria = subBuilder.twist(getSelectionCriteria(), builder);
if (query.getSelectionCriteria() != null) {
// For bug 2612567, any query can have batch attributes, so the
// original selection criteria can be quite complex, with multiple
// builders (i.e. for parallel selects).
// Now uses cloneUsing(newBase) instead of rebuildOn(newBase).
subCriteria = query.getSelectionCriteria().cloneUsing(subBuilder).and(subCriteria);
}
// Check for history and set asOf.
if (descriptorToUse.getHistoryPolicy() != null) {
if (query.getSession().getAsOfClause() != null) {
subBuilder.asOf(query.getSession().getAsOfClause());
} else if (batchQuery.getAsOfClause() == null) {
subBuilder.asOf(AsOfClause.NO_CLAUSE);
} else {
subBuilder.asOf(batchQuery.getAsOfClause());
}
}
subQuery.setSelectionCriteria(subCriteria);
batchSelectionCriteria = builder.exists(subQuery);
} else if (batchType == BatchFetchType.IN) {
// Using a IN with foreign key values (WHERE FK IN :QUERY_BATCH_PARAMETER)
batchSelectionCriteria = buildBatchCriteria(builder, query);
} else {
// Using a join, (WHERE <orginal-query-criteria> AND <mapping-join>)
// Join the query where clause with the mapping's,
// this will cause a join that should bring in all of the target objects.
Expression backRef = builder.getManualQueryKey(getAttributeName() + "-back-ref", descriptorToUse);
batchSelectionCriteria = backRef.twist(getSelectionCriteria(), builder);
if (query.getSelectionCriteria() != null) {
// For bug 2612567, any query can have batch attributes, so the
// original selection criteria can be quite complex, with multiple
// builders (i.e. for parallel selects).
// Now uses cloneUsing(newBase) instead of rebuildOn(newBase).
batchSelectionCriteria = batchSelectionCriteria.and(query.getSelectionCriteria().cloneUsing(backRef));
}
// Since a manual query key expression does not really get normalized,
// it must get its additional expressions added in here. Probably best
// to somehow keep all this code inside QueryKeyExpression.normalize.
if (descriptorToUse.getQueryManager().getAdditionalJoinExpression() != null) {
batchSelectionCriteria = batchSelectionCriteria.and(descriptorToUse.getQueryManager().getAdditionalJoinExpression().rebuildOn(backRef));
}
// Check for history and add history expression.
if (descriptorToUse.getHistoryPolicy() != null) {
if (query.getSession().getAsOfClause() != null) {
backRef.asOf(query.getSession().getAsOfClause());
} else if (batchQuery.getAsOfClause() == null) {
backRef.asOf(AsOfClause.NO_CLAUSE);
} else {
backRef.asOf(batchQuery.getAsOfClause());
}
batchSelectionCriteria = batchSelectionCriteria.and(descriptorToUse.getHistoryPolicy().additionalHistoryExpression(backRef, backRef));
}
}
batchQuery.setSelectionCriteria(batchSelectionCriteria);
if (query.isDistinctComputed()) {
// Only recompute if it has not already been set by the user
batchQuery.setDistinctState(query.getDistinctState());
}
// Add batch reading attributes contained in the mapping's query.
ReadQuery mappingQuery = this.selectionQuery;
if (mappingQuery.isReadAllQuery()) {
// CR#3238 clone these vectors so they will not grow with each call to the query. -TW
batchQuery.setOrderByExpressions(new ArrayList<Expression>(((ReadAllQuery)mappingQuery).getOrderByExpressions()));
if (((ReadAllQuery)mappingQuery).hasBatchReadAttributes()) {
for (Expression expression : ((ReadAllQuery)mappingQuery).getBatchReadAttributeExpressions()) {
batchQuery.addBatchReadAttribute(expression);
}
}
}
// Bug 385700 - Populate session & query class if not initialized by
// ObjectLevelReadQuery.computeBatchReadMappingQueries() in case batch query
// has been using inheritance and child descriptors can have different mappings.
if (query.hasBatchReadAttributes()) {
for (Expression expression : query.getBatchReadAttributeExpressions()) {
ObjectExpression batchReadExpression = (ObjectExpression) expression;
// Batch Read Attribute Expressions may not have initialized.
ExpressionBuilder expressionBuilder = batchReadExpression.getBuilder();
if (expressionBuilder.getQueryClass() == null) {
expressionBuilder.setQueryClass(query.getReferenceClass());
}
if (expressionBuilder.getSession() == null) {
expressionBuilder.setSession(query.getSession().getRootSession(null));
}
}
// Computed nested batch attribute expressions, and add them to batch query.
List<Expression> nestedExpressions = extractNestedExpressions(query.getBatchReadAttributeExpressions(), batchQuery.getExpressionBuilder());
batchQuery.getBatchReadAttributeExpressions().addAll(nestedExpressions);
}
batchQuery.setBatchFetchType(batchType);
batchQuery.setBatchFetchSize(query.getBatchFetchPolicy().getSize());
// Allow subclasses to further prepare.
postPrepareNestedBatchQuery(batchQuery, query);
// Set nested fetch group.
if (batchQuery.getDescriptor().hasFetchGroupManager()) {
FetchGroup sourceFetchGroup = query.getExecutionFetchGroup();
if (sourceFetchGroup != null) {
FetchGroup targetFetchGroup = sourceFetchGroup.getGroup(getAttributeName());
if (targetFetchGroup != null) {
((ObjectLevelReadQuery)batchQuery).setFetchGroup(targetFetchGroup);
}
}
}
if (batchQuery.shouldPrepare()) {
batchQuery.checkPrepare(query.getSession(), query.getTranslationRow());
}
batchQuery.setSession(null);
return batchQuery;
}
/**
* INTERNAL:
* An object has been serialized from the server to the client.
* Replace the transient attributes of the remote value holders
* with client-side objects.
*/
@Override
public void fixObjectReferences(Object object, Map objectDescriptors, Map processedObjects, ObjectLevelReadQuery query, DistributedSession session) {
this.indirectionPolicy.fixObjectReferences(object, objectDescriptors, processedObjects, query, session);
}
/**
* INTERNAL:
* Return the value of an attribute which this mapping represents for an object.
*/
@Override
public Object getAttributeValueFromObject(Object object) throws DescriptorException {
Object attributeValue = super.getAttributeValueFromObject(object);
Object indirectionValue = this.indirectionPolicy.validateAttributeOfInstantiatedObject(attributeValue);
// PERF: Allow the indirection policy to initialize null attribute values,
// this allows the indirection objects to not be initialized in the constructor.
if (indirectionValue != attributeValue) {
setAttributeValueInObject(object, indirectionValue);
attributeValue = indirectionValue;
}
return attributeValue;
}
/**
* INTERNAL:
* Returns the attribute value from the reference object.
* If the attribute is using indirection the value of the value-holder is returned.
* If the value holder is not instantiated then it is instantiated.
*/
public Object getAttributeValueWithClonedValueHolders(Object object) {
Object attributeValue = getAttributeValueFromObject(object);
if (attributeValue instanceof DatabaseValueHolder){
return ((DatabaseValueHolder)attributeValue).clone();
} else if (attributeValue instanceof ValueHolder){
return ((ValueHolder)attributeValue).clone();
}
return attributeValue;
}
/**
* INTERNAL:
* Return source key fields for translation by an AggregateObjectMapping
* By default, return an empty NonSynchronizedVector
*/
public Collection getFieldsForTranslationInAggregate() {
return new NonSynchronizedVector(0);
}
/**
* INTERNAL:
* Should be overridden by subclass that allows setting
* extendPessimisticLockScope to DEDICATED_QUERY.
*/
protected ReadQuery getExtendPessimisticLockScopeDedicatedQuery(AbstractSession session, short lockMode) {
return null;
}
/**
* INTERNAL:
* Return the mapping's indirection policy.
*/
public IndirectionPolicy getIndirectionPolicy() {
return indirectionPolicy;
}
/**
* INTERNAL:
* Return whether the specified object is instantiated.
*/
@Override
public boolean isAttributeValueFromObjectInstantiated(Object object) {
return this.indirectionPolicy.objectIsInstantiated(getAttributeValueFromObject(object));
}
/**
* INTERNAL:
* Returns the join criteria stored in the mapping selection query. This criteria
* is used to read reference objects across the tables from the database.
*/
public Expression getJoinCriteria(ObjectExpression context, Expression base) {
Expression selectionCriteria = getSelectionCriteria();
return context.getBaseExpression().twist(selectionCriteria, base);
}
/**
* INTERNAL:
* return the object on the client corresponding to the specified object.
* ForeignReferenceMappings have to worry about
* maintaining object identity.
*/
@Override
public Object getObjectCorrespondingTo(Object object, DistributedSession session, Map objectDescriptors, Map processedObjects, ObjectLevelReadQuery query) {
return session.getObjectCorrespondingTo(object, objectDescriptors, processedObjects, query);
}
/**
* INTERNAL:
* Returns the attribute value from the reference object.
* If the attribute is using indirection the value of the value-holder is returned.
* If the value holder is not instantiated then it is instantiated.
*/
@Override
public Object getRealAttributeValueFromAttribute(Object attributeValue, Object object, AbstractSession session) {
return this.indirectionPolicy.getRealAttributeValueFromObject(object, attributeValue);
}
/**
* Return if this mapping is lazy.
* For relationship mappings this should normally be the same value as indirection,
* however for eager relationships this can be used with indirection to allow
* indirection locking and change tracking, but still always force instantiation.
*/
@Override
public boolean isLazy() {
if (isLazy == null) {
// False by default for mappings without indirection.
isLazy = usesIndirection();
}
return isLazy;
}
/**
* INTERNAL:
* Return whether this mapping should be traversed when we are locking.
*/
@Override
public boolean isLockableMapping(){
return !(this.usesIndirection()) && !referenceDescriptor.getCachePolicy().isIsolated();
}
/**
* INTERNAL:
* Trigger the instantiation of the attribute if lazy.
*/
@Override
public void instantiateAttribute(Object object, AbstractSession session) {
this.indirectionPolicy.instantiateObject(object, getAttributeValueFromObject(object));
}
/**
* PUBLIC:
* Returns the reference class.
*/
public Class getReferenceClass() {
return referenceClass;
}
/**
* INTERNAL:
* Returns the reference class name.
*/
public String getReferenceClassName() {
if ((referenceClassName == null) && (referenceClass != null)) {
referenceClassName = referenceClass.getName();
}
return referenceClassName;
}
/**
* INTERNAL:
* Return the referenceDescriptor. This is a descriptor which is associated with
* the reference class.
*/
@Override
public ClassDescriptor getReferenceDescriptor() {
if (referenceDescriptor == null) {
if (getTempSession() == null) {
return null;
} else {
referenceDescriptor = getTempSession().getDescriptor(getReferenceClass());
}
}
return referenceDescriptor;
}
/**
* INTERNAL:
* Return the relationshipPartner mapping for this bi-directional mapping. If the relationshipPartner is null then
* this is a uni-directional mapping.
*/
@Override
public DatabaseMapping getRelationshipPartner() {
if ((this.relationshipPartner == null) && (this.relationshipPartnerAttributeName != null)) {
setRelationshipPartner(getReferenceDescriptor().getObjectBuilder().getMappingForAttributeName(getRelationshipPartnerAttributeName()));
}
return this.relationshipPartner;
}
/**
* PUBLIC:
* Use this method retrieve the relationship partner attribute name of this bidirectional Mapping.
*/
public String getRelationshipPartnerAttributeName() {
return this.relationshipPartnerAttributeName;
}
/**
* INTERNAL:
* Returns the selection criteria stored in the mapping selection query. This criteria
* is used to read reference objects from the database. It will return null before
* initialization. To obtain the selection criteria before initialization (e.g., in a
* customizer) you can use the buildSelectionCriteria() method defined by some subclasses.
*
* @see org.eclipse.persistence.mappings.OneToOneMapping#buildSelectionCriteria()
* @see org.eclipse.persistence.mappings.OneToManyMapping#buildSelectionCriteria()
*/
public Expression getSelectionCriteria() {
return getSelectionQuery().getSelectionCriteria();
}
/**
* INTERNAL:
* Returns the read query associated with the mapping.
*/
public ReadQuery getSelectionQuery() {
return selectionQuery;
}
protected AbstractSession getTempSession() {
return tempInitSession;
}
/**
* INTERNAL:
* Extract and return the appropriate value from the
* specified remote value holder.
*/
@Override
public Object getValueFromRemoteValueHolder(RemoteValueHolder remoteValueHolder) {
return this.indirectionPolicy.getValueFromRemoteValueHolder(remoteValueHolder);
}
/**
* INTERNAL:
* Indicates whether the selection query is TopLink generated or defined by
* the user.
*/
public boolean hasCustomSelectionQuery() {
return hasCustomSelectionQuery;
}
/**
* INTERNAL:
* Indicates whether the mapping (or at least one of its nested mappings, at any nested depth)
* references an entity.
* To return true the mapping (or nested mapping) should be ForeignReferenceMapping with non-null and non-aggregate reference descriptor.
*/
@Override
public boolean hasNestedIdentityReference() {
return true;
}
/**
* INTERNAL:
* Initialize the state of mapping.
*/
@Override
public void preInitialize(AbstractSession session) throws DescriptorException {
super.preInitialize(session);
// If weaving was used the mapping must be configured to use the weaved get/set methods.
if ((this.indirectionPolicy instanceof BasicIndirectionPolicy) && ClassConstants.PersistenceWeavedLazy_Class.isAssignableFrom(getDescriptor().getJavaClass())) {
Class attributeType = getAttributeAccessor().getAttributeClass();
// Check that not already weaved or coded.
if (!(ClassConstants.ValueHolderInterface_Class.isAssignableFrom(attributeType))) {
if (!indirectionPolicy.isWeavedObjectBasicIndirectionPolicy()){
if(getAttributeAccessor().isMethodAttributeAccessor()) {
useWeavedIndirection(getGetMethodName(), getSetMethodName(), true);
} else if(getAttributeAccessor().isInstanceVariableAttributeAccessor()) {
useWeavedIndirection(Helper.getWeavedGetMethodName(getAttributeName()), Helper.getWeavedSetMethodName(getAttributeName()), false);
}
}
setGetMethodName(Helper.getWeavedValueHolderGetMethodName(getAttributeName()));
setSetMethodName(Helper.getWeavedValueHolderSetMethodName(getAttributeName()));
// Must re-initialize the attribute accessor.
super.preInitialize(session);
}
}
if (getPartitioningPolicyName() != null) {
PartitioningPolicy policy = session.getProject().getPartitioningPolicy(getPartitioningPolicyName());
if (policy == null) {
session.getIntegrityChecker().handleError(DescriptorException.missingPartitioningPolicy(getPartitioningPolicyName(), null, this));
}
setPartitioningPolicy(policy);
}
if (this.isCascadeOnDeleteSetOnDatabase && !session.getPlatform().supportsDeleteOnCascade()) {
this.isCascadeOnDeleteSetOnDatabase = false;
}
}
/**
* INTERNAL:
* Initialize the state of mapping.
*/
@Override
public void initialize(AbstractSession session) throws DescriptorException {
super.initialize(session);
//474752 : InitializeReferenceDescriptor before
//addMappingsPostCalculateChanges
initializeReferenceDescriptor(session);
if (this.isPrivateOwned && (this.descriptor != null)) {
this.descriptor.addMappingsPostCalculateChanges(this);
}
initializeSelectionQuery(session);
this.indirectionPolicy.initialize();
if ((this.referenceDescriptor != null) && this.referenceDescriptor.getCachePolicy().isIsolated()) {
this.isCacheable = false;
}
}
/**
* Initialize and set the descriptor for the referenced class in this mapping.
*/
protected void initializeReferenceDescriptor(AbstractSession session) throws DescriptorException {
if (getReferenceClass() == null) {
throw DescriptorException.referenceClassNotSpecified(this);
}
ClassDescriptor refDescriptor = session.getDescriptor(getReferenceClass());
if (refDescriptor == null) {
throw DescriptorException.descriptorIsMissing(getReferenceClass().getName(), this);
}
if (refDescriptor.isAggregateDescriptor() && (!isAggregateCollectionMapping())) {
throw DescriptorException.referenceDescriptorCannotBeAggregate(this);
}
// can not be isolated if it is null. Seems that only aggregates do not set
// the owning descriptor on the mapping.
setReferenceDescriptor(refDescriptor);
}
/**
* INTERNAL:
* The method validateAttributeOfInstantiatedObject(Object attributeValue) fixes the value of the attributeValue
* in cases where it is null and indirection requires that it contain some specific data structure. Return whether this will happen.
* This method is used to help determine if indirection has been triggered
* @param object
* @return
* @see org.eclipse.persistence.internal.indirection.IndirectionPolicy#validateAttributeOfInstantiatedObject(Object)
*/
public boolean isAttributeValueFullyBuilt(Object object){
Object attributeValue = super.getAttributeValueFromObject(object);
return this.indirectionPolicy.isAttributeValueFullyBuilt(attributeValue);
}
/**
* A subclass should implement this method if it wants non default behavior.
*/
protected void initializeSelectionQuery(AbstractSession session) throws DescriptorException {
if (((ObjectLevelReadQuery)getSelectionQuery()).getReferenceClass() == null) {
throw DescriptorException.referenceClassNotSpecified(this);
}
getSelectionQuery().setName(getAttributeName());
getSelectionQuery().setDescriptor(getReferenceDescriptor());
getSelectionQuery().setSourceMapping(this);
if (getSelectionQuery().getPartitioningPolicy() == null) {
getSelectionQuery().setPartitioningPolicy(getPartitioningPolicy());
}
}
/**
* INTERNAL:
* The referenced object is checked if it is instantiated or not
*/
public boolean isAttributeValueInstantiated(Object object) {
return this.indirectionPolicy.objectIsInstantiated(getAttributeValueFromObject(object));
}
/**
* PUBLIC:
* Check cascading value for the detach operation.
*/
public boolean isCascadeDetach() {
return this.cascadeDetach;
}
/**
* PUBLIC:
* Check cascading value for the CREATE operation.
*/
public boolean isCascadePersist() {
return this.cascadePersist;
}
/**
* PUBLIC:
* Check cascading value for the MERGE operation.
*/
public boolean isCascadeMerge() {
return this.cascadeMerge;
}
/**
* PUBLIC:
* Check cascading value for the REFRESH operation.
*/
public boolean isCascadeRefresh() {
return this.cascadeRefresh;
}
/**
* PUBLIC:
* Check cascading value for the REMOVE operation.
*/
public boolean isCascadeRemove() {
return this.cascadeRemove;
}
/**
* INTERNAL:
* Return if the mapping has any ownership or other dependency over its target object(s).
*/
@Override
public boolean hasDependency() {
return isPrivateOwned() || isCascadeRemove();
}
/**
* INTERNAL:
*/
@Override
public boolean isForeignReferenceMapping() {
return true;
}
/**
* INTERNAL:
* Return if this mapping supports joining.
*/
@Override
public boolean isJoiningSupported() {
return false;
}
/**
* PUBLIC:
* Return true if referenced objects are privately owned else false.
*/
@Override
public boolean isPrivateOwned() {
return isPrivateOwned;
}
/**
* INTERNAL:
* Iterate on the iterator's current object's attribute defined by this mapping.
* The iterator's settings for cascading and value holders determine how the
* iteration continues from here.
*/
@Override
public void iterate(DescriptorIterator iterator) {
Object attributeValue = this.getAttributeValueFromObject(iterator.getVisitedParent());
this.indirectionPolicy.iterateOnAttributeValue(iterator, attributeValue);
}
/**
* INTERNAL:
* Iterate on the attribute value.
* The value holder has already been processed.
*/
@Override
public abstract void iterateOnRealAttributeValue(DescriptorIterator iterator, Object realAttributeValue);
/**
* Force instantiation of the load group.
*/
@Override
public void load(final Object object, AttributeItem item, final AbstractSession session, final boolean fromFetchGroup) {
instantiateAttribute(object, session);
if (item.getGroup() != null && (!fromFetchGroup || session.isUnitOfWork())) {
// if fromFetchGroup then the above instantiate already loaded the elements unless this is in UOW
// in which case the clones must be loaded as well.
Object value = getRealAttributeValueFromObject(object, session);
session.load(value, item.getGroup(), getReferenceDescriptor(), fromFetchGroup);
}
}
/**
* INTERNAL:
* Replace the client value holder with the server value holder,
* after copying some of the settings from the client value holder.
*/
public void mergeRemoteValueHolder(Object clientSideDomainObject, Object serverSideDomainObject, MergeManager mergeManager) {
this.indirectionPolicy.mergeRemoteValueHolder(clientSideDomainObject, serverSideDomainObject, mergeManager);
}
/**
* PUBLIC:
* Sets the reference object to be a private owned.
* The default behavior is non private owned, or independent.
* @see #setIsPrivateOwned(boolean)
*/
public void privateOwnedRelationship() {
setIsPrivateOwned(true);
}
/**
* INTERNAL:
* Extract value from the row and set the attribute to this value in the object.
* return value as this value will have been converted to the appropriate type for
* the object.
*/
@Override
public Object readFromRowIntoObject(AbstractRecord databaseRow, JoinedAttributeManager joinManager, Object targetObject, CacheKey parentCacheKey, ObjectBuildingQuery sourceQuery, AbstractSession executionSession, boolean isTargetProtected) throws DatabaseException {
Boolean[] wasCacheUsed = new Boolean[]{Boolean.FALSE};
Object attributeValue = valueFromRow(databaseRow, joinManager, sourceQuery, parentCacheKey, executionSession, isTargetProtected, wasCacheUsed);
if (wasCacheUsed[0]){
//must clone here as certain mappings require the clone object to clone the attribute.
Integer refreshCascade = null;
if (sourceQuery != null && sourceQuery.isObjectBuildingQuery() && sourceQuery.shouldRefreshIdentityMapResult()) {
refreshCascade = sourceQuery.getCascadePolicy();
}
attributeValue = this.indirectionPolicy.cloneAttribute(attributeValue, parentCacheKey.getObject(), parentCacheKey, targetObject, refreshCascade, executionSession, false);
}
if (executionSession.isUnitOfWork() && sourceQuery.shouldRefreshIdentityMapResult() || databaseRow.hasSopObject()){
// check whether the attribute is fully build before calling getAttributeValueFromObject because that
// call may fully build the attribute
boolean wasAttributeValueFullyBuilt = isAttributeValueFullyBuilt(targetObject);
Object oldAttribute = this.getAttributeValueFromObject(targetObject);
setAttributeValueInObject(targetObject, attributeValue); // set this first to prevent infinite recursion
if (wasAttributeValueFullyBuilt && this.indirectionPolicy.objectIsInstantiatedOrChanged(oldAttribute)){
this.indirectionPolicy.instantiateObject(targetObject, attributeValue);
}
}else{
setAttributeValueInObject(targetObject, attributeValue);
}
if (parentCacheKey != null){
this.indirectionPolicy.setSourceObject(parentCacheKey.getObject(), attributeValue);
}
return attributeValue;
}
/**
* INTERNAL:
* Once descriptors are serialized to the remote session. All its mappings and reference descriptors are traversed. Usually
* mappings are initialized and serialized reference descriptors are replaced with local descriptors if they already exist on the
* remote session.
*/
@Override
public void remoteInitialization(DistributedSession session) {
super.remoteInitialization(session);
setTempSession(session);
}
/**
* INTERNAL:
* replace the value holders in the specified reference object(s)
*/
@Override
public Map replaceValueHoldersIn(Object object, RemoteSessionController controller) {
return controller.replaceValueHoldersIn(object);
}
/**
* Returns true if this mappings associated weaved field requires a
* transient setting to avoid metadata processing.
*/
public boolean requiresTransientWeavedFields() {
return requiresTransientWeavedFields;
}
/**
* PUBLIC:
* Sets the cascading for all JPA operations.
*/
public void setCascadeAll(boolean value) {
setCascadePersist(value);
setCascadeMerge(value);
setCascadeRefresh(value);
setCascadeRemove(value);
setCascadeDetach(value);
}
/**
* PUBLIC:
* Sets the cascading for the JPA detach operation.
*/
public void setCascadeDetach(boolean value) {
this.cascadeDetach = value;
}
/**
* PUBLIC:
* Sets the cascading for the JPA CREATE operation.
*/
public void setCascadePersist(boolean value) {
this.cascadePersist = value;
}
/**
* PUBLIC:
* Sets the cascading for the JPA MERGE operation.
*/
public void setCascadeMerge(boolean value) {
this.cascadeMerge = value;
}
/**
* PUBLIC:
* Sets the cascading for the JPA REFRESH operation.
*/
public void setCascadeRefresh(boolean value) {
this.cascadeRefresh = value;
}
/**
* PUBLIC:
* Sets the cascading for the JPA REMOVE operation.
*/
public void setCascadeRemove(boolean value) {
this.cascadeRemove = value;
}
/**
* PUBLIC:
* Relationship mappings creates a read query to read reference objects. If this default
* query needs to be customize then user can specify its own read query to do the reading
* of reference objects. One must instance of ReadQuery or subclasses of the ReadQuery.
*/
public void setCustomSelectionQuery(ReadQuery query) {
setSelectionQuery(query);
setHasCustomSelectionQuery(true);
}
protected void setHasCustomSelectionQuery(boolean bool) {
hasCustomSelectionQuery = bool;
}
/**
* INTERNAL:
* A way of forcing the selection criteria to be rebuilt.
*/
public void setForceInitializationOfSelectionCriteria(boolean bool) {
forceInitializationOfSelectionCriteria = bool;
}
/**
* ADVANCED:
* Set the indirection policy.
*/
public void setIndirectionPolicy(IndirectionPolicy indirectionPolicy) {
this.indirectionPolicy = indirectionPolicy;
indirectionPolicy.setMapping(this);
}
/**
* PUBLIC:
* Set if the relationship is privately owned.
* A privately owned relationship means the target object is a dependent part of the source
* object and is not referenced by any other object and cannot exist on its own.
* Private ownership causes many operations to be cascaded across the relationship,
* including, deletion, insertion, refreshing, locking (when cascaded).
* It also ensures that private objects removed from collections are deleted and object added are inserted.
*/
public void setIsPrivateOwned(boolean isPrivateOwned) {
if (this.descriptor != null && ! this.isMapKeyMapping()){ // initialized
if (isPrivateOwned && !this.isPrivateOwned){
this.descriptor.addMappingsPostCalculateChanges(this);
if (getDescriptor().hasInheritance()){
for (ClassDescriptor descriptor: getDescriptor().getInheritancePolicy().getAllChildDescriptors()) {
descriptor.addMappingsPostCalculateChanges(this);
}
}
}else if (!isPrivateOwned && this.isPrivateOwned){
this.descriptor.getMappingsPostCalculateChanges().remove(this);
if (getDescriptor().hasInheritance()){
for (ClassDescriptor descriptor: getDescriptor().getInheritancePolicy().getAllChildDescriptors()) {
descriptor.getMappingsPostCalculateChanges().remove(this);
}
}
}
}
this.isPrivateOwned = isPrivateOwned;
}
/**
* INTERNAL:
* Set the value of the attribute mapped by this mapping,
* placing it inside a value holder if necessary.
* If the value holder is not instantiated then it is instantiated.
*/
@Override
public void setRealAttributeValueInObject(Object object, Object value) throws DescriptorException {
this.indirectionPolicy.setRealAttributeValueInObject(object, value);
}
/**
* PUBLIC:
* Set the referenced class.
*/
public void setReferenceClass(Class referenceClass) {
this.referenceClass = referenceClass;
if (referenceClass != null) {
setReferenceClassName(referenceClass.getName());
// Make sure the reference class of the selectionQuery is set.
setSelectionQuery(getSelectionQuery());
}
}
/**
* INTERNAL:
* Used by MW.
*/
public void setReferenceClassName(String referenceClassName) {
this.referenceClassName = referenceClassName;
}
/**
* Set the referenceDescriptor. This is a descriptor which is associated with
* the reference class.
*/
protected void setReferenceDescriptor(ClassDescriptor aDescriptor) {
referenceDescriptor = aDescriptor;
}
/**
* INTERNAL:
* Sets the relationshipPartner mapping for this bi-directional mapping. If the relationshipPartner is null then
* this is a uni-directional mapping.
*/
public void setRelationshipPartner(DatabaseMapping mapping) {
this.relationshipPartner = mapping;
}
/**
* PUBLIC:
* Use this method to specify the relationship partner attribute name of a bidirectional Mapping.
* TopLink will use the attribute name to find the back pointer mapping to maintain referential integrity of
* the bi-directional mappings.
*/
public void setRelationshipPartnerAttributeName(String attributeName) {
this.relationshipPartnerAttributeName = attributeName;
}
/**
* Set this flag if this mappings associated weaved field requires a
* transient setting to avoid metadata processing.
*/
public void setRequiresTransientWeavedFields(boolean requiresTransientWeavedFields) {
this.requiresTransientWeavedFields = requiresTransientWeavedFields;
}
/**
* PUBLIC:
* Sets the selection criteria to be used as a where clause to read
* reference objects. This criteria is automatically generated by the
* TopLink if not explicitly specified by the user.
*/
public void setSelectionCriteria(Expression anExpression) {
getSelectionQuery().setSelectionCriteria(anExpression);
}
/**
* Sets the query
*/
protected void setSelectionQuery(ReadQuery aQuery) {
selectionQuery = aQuery;
// Make sure the reference class of the selectionQuery is set.
if ((selectionQuery != null) && selectionQuery.isObjectLevelReadQuery() && (selectionQuery.getReferenceClassName() == null)) {
((ObjectLevelReadQuery)selectionQuery).setReferenceClass(getReferenceClass());
}
}
/**
* PUBLIC:
* This is a property on the mapping which will allow custom SQL to be
* substituted for reading a reference object.
*/
public void setSelectionSQLString(String sqlString) {
getSelectionQuery().setSQLString(sqlString);
setCustomSelectionQuery(getSelectionQuery());
}
/**
* PUBLIC:
* This is a property on the mapping which will allow custom call to be
* substituted for reading a reference object.
*/
public void setSelectionCall(Call call) {
getSelectionQuery().setCall(call);
setCustomSelectionQuery(getSelectionQuery());
}
/**
* ADVANCED:
* Indicates whether pessimistic lock of ObjectLevelReadQuery with isPessimisticLockScopeExtended set to true
* should be applied through this mapping beyond the tables mapped to the source object.
*/
public void setShouldExtendPessimisticLockScope(boolean shouldExtend) {
extendPessimisticLockScope = shouldExtend ? ExtendPessimisticLockScope.TARGET_QUERY : ExtendPessimisticLockScope.NONE;
}
protected void setTempSession(AbstractSession session) {
this.tempInitSession = session;
}
/**
* PUBLIC:
* Indicates whether the referenced object should always be batch read on read all queries.
* Batch reading will read all of the related objects in a single query when accessed from an originating read all.
* This should only be used if it is know that the related objects are always required with the source object, or indirection is not used.
* @see #setBatchFetchType(BatchFetchType)
*/
public void setUsesBatchReading(boolean usesBatchReading) {
if (usesBatchReading) {
setBatchFetchType(BatchFetchType.JOIN);
} else {
setBatchFetchType(null);
}
}
/**
* PUBLIC:
* Indirection means that a ValueHolder will be put in-between the attribute and the real object.
* This allows for the reading of the target from the database to be delayed until accessed.
* This defaults to true and is strongly suggested as it give a huge performance gain.
* @see #useBasicIndirection()
* @see #dontUseIndirection()
*/
public void setUsesIndirection(boolean usesIndirection) {
if (usesIndirection) {
useBasicIndirection();
} else {
dontUseIndirection();
}
}
/**
* INTERNAL:
* Indicates whether pessimistic lock of ObjectLevelReadQuery with isPessimisticLockScopeExtended set to true
* should be applied through this mapping beyond the tables mapped to the source object.
*/
public boolean shouldExtendPessimisticLockScope() {
return extendPessimisticLockScope != ExtendPessimisticLockScope.NONE;
}
public boolean shouldExtendPessimisticLockScopeInSourceQuery() {
return extendPessimisticLockScope == ExtendPessimisticLockScope.SOURCE_QUERY;
}
public boolean shouldExtendPessimisticLockScopeInTargetQuery() {
return extendPessimisticLockScope == ExtendPessimisticLockScope.TARGET_QUERY;
}
public boolean shouldExtendPessimisticLockScopeInDedicatedQuery() {
return extendPessimisticLockScope == ExtendPessimisticLockScope.DEDICATED_QUERY;
}
/**
* INTERNAL:
*/
protected boolean shouldForceInitializationOfSelectionCriteria() {
return forceInitializationOfSelectionCriteria;
}
protected boolean shouldInitializeSelectionCriteria() {
if (shouldForceInitializationOfSelectionCriteria()) {
return true;
}
if (hasCustomSelectionQuery()) {
return false;
}
if (getSelectionCriteria() == null) {
return true;
}
return false;
}
/**
* INTERNAL:
* Returns true if the merge should cascade to the mappings reference's parts.
*/
public boolean shouldMergeCascadeParts(MergeManager mergeManager) {
return (mergeManager.shouldCascadeByMapping() && ((this.isCascadeMerge() && !mergeManager.isForRefresh()) || (this.isCascadeRefresh() && mergeManager.isForRefresh()) )) || mergeManager.shouldCascadeAllParts() || (mergeManager.shouldCascadePrivateParts() && isPrivateOwned());
}
/**
* INTERNAL:
* Returns true if the merge should cascade to the mappings reference's parts.
*/
public boolean shouldRefreshCascadeParts(MergeManager mergeManager) {
return (mergeManager.shouldCascadeByMapping() && this.isCascadeRefresh()) || mergeManager.shouldCascadeAllParts() || (mergeManager.shouldCascadePrivateParts() && isPrivateOwned());
}
/**
* Returns true if the merge should cascade to the mappings reference.
*/
protected boolean shouldMergeCascadeReference(MergeManager mergeManager) {
if (mergeManager.shouldCascadeReferences()) {
return true;
}
// P2.0.1.3: Was merging references on non-privately owned parts
// Same logic in:
return shouldMergeCascadeParts(mergeManager);
}
/**
* Returns true if any process leading to object modification should also affect its parts
* Usually used by write, insert, update and delete.
*/
protected boolean shouldObjectModifyCascadeToParts(ObjectLevelModifyQuery query) {
if (this.isReadOnly) {
return false;
}
// Only cascade dependents writes in uow.
if (query.shouldCascadeOnlyDependentParts()) {
return hasConstraintDependency();
}
if (this.isPrivateOwned) {
return true;
}
return query.shouldCascadeAllParts();
}
/**
* PUBLIC:
* Indicates whether the referenced object should always be batch read on read all queries.
* Batch reading will read all of the related objects in a single query when accessed from an originating read all.
* This should only be used if it is know that the related objects are always required with the source object, or indirection is not used.
*/
public boolean shouldUseBatchReading() {
return this.batchFetchType != null;
}
/**
* PUBLIC:
* Indirection means that a ValueHolder will be put in-between the attribute and the real object.
* This allows for the reading of the target from the database to be delayed until accessed.
* This defaults to true and is strongly suggested as it give a huge performance gain.
*/
public void useBasicIndirection() {
setIndirectionPolicy(new BasicIndirectionPolicy());
}
/**
* PUBLIC:
* Indicates whether the referenced object should always be batch read on read all queries.
* Batch reading will read all of the related objects in a single query when accessed from an originating read all.
* This should only be used if it is know that the related objects are always required with the source object, or indirection is not used.
*/
public void useBatchReading() {
setBatchFetchType(BatchFetchType.JOIN);
}
/**
* INTERNAL:
* Configures the mapping to used weaved indirection.
* This requires that the toplink-agent be used to weave indirection into the class.
* This policy is only require for method access.
* @param getMethodName is the name of the original (or weaved in field access case) set method for the mapping.
* @param setMethodName is the name of the original (or weaved in field access case) set method for the mapping.
* @param hasUsedMethodAccess indicates whether method or field access was originally used.
*/
public void useWeavedIndirection(String getMethodName, String setMethodName, boolean hasUsedMethodAccess){
setIndirectionPolicy(new WeavedObjectBasicIndirectionPolicy(getMethodName, setMethodName, null, hasUsedMethodAccess));
}
/**
* PUBLIC:
* Indirection means that a IndirectContainer (wrapping a ValueHolder) will be put in-between the attribute and the real object.
* This allows for an application specific class to be used which wraps the value holder.
* The purpose of this is that the domain objects will not require to import the ValueHolderInterface class.
* Refer also to transparent indirection for a transparent solution to indirection.
*/
public void useContainerIndirection(Class containerClass) {
ContainerIndirectionPolicy policy = new ContainerIndirectionPolicy();
policy.setContainerClass(containerClass);
setIndirectionPolicy(policy);
}
/**
* PUBLIC:
* Indirection means that some sort of indirection object will be put in-between the attribute and the real object.
* This allows for the reading of the target from the database to be delayed until accessed.
* This defaults to true and is strongly suggested as it give a huge performance gain.
*/
public boolean usesIndirection() {
return this.indirectionPolicy.usesIndirection();
}
/**
* INTERNAL:
* Update a ChangeRecord to replace the ChangeSet for the old entity with the changeSet for the new Entity. This is
* used when an Entity is merged into itself and the Entity reference new or detached entities.
*/
public abstract void updateChangeRecordForSelfMerge(ChangeRecord changeRecord, Object source, Object target, UnitOfWorkChangeSet parentUOWChangeSet, UnitOfWorkImpl unitOfWork);
/**
* PUBLIC:
* Indicates whether the referenced object(s) should always be joined on read queries.
* Joining will join the two classes tables to read all of the data in a single query.
* This should only be used if it is know that the related objects are always required with the source object,
* or indirection is not used.
* A join-fetch can either use an INNER_JOIN or OUTER_JOIN,
* if the relationship may reference null or an empty collection an outer join should be used to avoid filtering the source objects from the queries.
* Join fetch can also be specified on the query, and it is normally more efficient to do so as some queries may not require the related objects.
* Typically batch reading is more efficient than join fetching and should be considered, especially for collection relationships.
* @see org.eclipse.persistence.queries.ObjectLevelReadQuery#addJoinedAttribute(String)
* @see org.eclipse.persistence.queries.ReadAllQuery#addBatchReadAttribute(String)
*/
public void setJoinFetch(int joinFetch) {
this.joinFetch = joinFetch;
}
/**
* PUBLIC:
* Return if this relationship should always be join fetched.
*/
public int getJoinFetch() {
return joinFetch;
}
/**
* INTERNAL: Called by JPA metadata processing to store the owning mapping
* for this mapping
*
* @param mappedBy
*/
public void setMappedBy(String mappedBy) {
this.mappedBy = mappedBy;
}
/**
* PUBLIC:
* Return if this relationship should always be join fetched.
*/
public boolean isJoinFetched() {
return getJoinFetch() != NONE;
}
/**
* PUBLIC:
* Return if this relationship should always be INNER join fetched.
*/
public boolean isInnerJoinFetched() {
return getJoinFetch() == INNER_JOIN;
}
/**
* PUBLIC:
* Return if this relationship should always be OUTER join fetched.
*/
public boolean isOuterJoinFetched() {
return getJoinFetch() == OUTER_JOIN;
}
/**
* PUBLIC:
* Specify this relationship to always be join fetched using an INNER join.
*/
public void useInnerJoinFetch() {
setJoinFetch(INNER_JOIN);
}
/**
* PUBLIC:
* Specify this relationship to always be join fetched using an OUTER join.
*/
public void useOuterJoinFetch() {
setJoinFetch(OUTER_JOIN);
}
/**
* ADVANCED:
* Return if delete cascading has been set on the database for the
* mapping's foreign key constraint.
*/
public boolean isCascadeOnDeleteSetOnDatabase() {
return isCascadeOnDeleteSetOnDatabase;
}
/**
* ADVANCED:
* Set if delete cascading has been set on the database for the
* mapping's foreign key constraint.
* The behavior is dependent on the mapping.
* <p>OneToOne (target foreign key) - deletes target object (private owned)
* <p>OneToMany, AggregateCollection - deletes target objects (private owned)
* <p>ManyToMany - deletes from join table (only)
* <p>DirectCollection - delete from direct table
*/
public void setIsCascadeOnDeleteSetOnDatabase(boolean isCascadeOnDeleteSetOnDatabase) {
this.isCascadeOnDeleteSetOnDatabase = isCascadeOnDeleteSetOnDatabase;
}
/**
* Used to signal that this mapping references a protected/isolated entity and requires
* special merge/object building behaviour.
*/
@Override
public void setIsCacheable(boolean cacheable) {
this.isCacheable = cacheable;
}
/**
* INTERNAL:
* To validate mappings declaration
*/
@Override
public void validateBeforeInitialization(AbstractSession session) throws DescriptorException {
super.validateBeforeInitialization(session);
// If a lazy mapping required weaving for lazy, and weaving did not occur,
// then the mapping must be reverted to no use indirection.
if ((this.indirectionPolicy instanceof WeavedObjectBasicIndirectionPolicy) && !ClassConstants.PersistenceWeavedLazy_Class.isAssignableFrom(getDescriptor().getJavaClass())) {
Object[] args = new Object[2];
args[0] = getAttributeName();
args[1] = getDescriptor().getJavaClass();
session.log(SessionLog.WARNING, SessionLog.METADATA, "metadata_warning_ignore_lazy", args);
setIndirectionPolicy(new NoIndirectionPolicy());
}
if (getAttributeAccessor() instanceof InstanceVariableAttributeAccessor) {
Class attributeType = ((InstanceVariableAttributeAccessor)getAttributeAccessor()).getAttributeType();
this.indirectionPolicy.validateDeclaredAttributeType(attributeType, session.getIntegrityChecker());
} else if (getAttributeAccessor().isMethodAttributeAccessor()) {
// 323148
Class returnType = ((MethodAttributeAccessor)getAttributeAccessor()).getGetMethodReturnType();
this.indirectionPolicy.validateGetMethodReturnType(returnType, session.getIntegrityChecker());
Class parameterType = ((MethodAttributeAccessor)getAttributeAccessor()).getSetMethodParameterType();
this.indirectionPolicy.validateSetMethodParameterType(parameterType, session.getIntegrityChecker());
}
}
/**
* This method is used to load a relationship from a list of PKs. This list
* may be available if the relationship has been cached.
*/
public abstract Object valueFromPKList(Object[] pks, AbstractRecord foreignKeys, AbstractSession session);
/**
* INTERNAL: Return the value of the reference attribute or a value holder.
* Check whether the mapping's attribute should be optimized through batch
* and joining.
*/
@Override
public Object valueFromRow(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery sourceQuery, CacheKey cacheKey, AbstractSession executionSession, boolean isTargetProtected, Boolean[] wasCacheUsed) throws DatabaseException {
if (this.descriptor.getCachePolicy().isProtectedIsolation()) {
if (this.isCacheable && isTargetProtected && cacheKey != null) {
//cachekey will be null when isolating to uow
//used cached collection
Object cached = cacheKey.getObject();
if (cached != null) {
if (wasCacheUsed != null){
wasCacheUsed[0] = Boolean.TRUE;
}
//this will just clone the indirection.
//the indirection object is responsible for cloning the value.
return getAttributeValueFromObject(cached);
}
} else if (!this.isCacheable && !isTargetProtected && cacheKey != null) {
return this.indirectionPolicy.buildIndirectObject(new ValueHolder(null));
}
}
if (row.hasSopObject()) {
// DirectCollection or AggregateCollection that doesn't reference entities: no need to build members back into cache - just return the whole collection from sopObject.
if (!hasNestedIdentityReference()) {
return getAttributeValueFromObject(row.getSopObject());
} else {
return valueFromRowInternal(row, null, sourceQuery, executionSession, true);
}
}
// PERF: Direct variable access.
if (shouldUseValueFromRowWithJoin(joinManager, sourceQuery)) {
return valueFromRowInternalWithJoin(row, joinManager, sourceQuery, cacheKey, executionSession, isTargetProtected);
}
// If the query uses batch reading, return a special value holder
// or retrieve the object from the query property.
if (sourceQuery.isObjectLevelReadQuery() && (((ObjectLevelReadQuery)sourceQuery).isAttributeBatchRead(this.descriptor, getAttributeName())
|| (sourceQuery.isReadAllQuery() && shouldUseBatchReading()))) {
return batchedValueFromRow(row, (ObjectLevelReadQuery)sourceQuery, cacheKey);
} else {
return valueFromRowInternal(row, joinManager, sourceQuery, executionSession, false);
}
}
/**
* INTERNAL:
* Indicates whether valueFromRow should call valueFromRowInternalWithJoin (true)
* or valueFromRowInternal (false)
*/
protected boolean shouldUseValueFromRowWithJoin(JoinedAttributeManager joinManager, ObjectBuildingQuery sourceQuery) {
return ((joinManager != null) && (joinManager.isAttributeJoined(this.descriptor, this))) || sourceQuery.hasPartialAttributeExpressions();
}
/**
* INTERNAL:
* If the query used joining or partial attributes, build the target object directly.
* If isJoiningSupported()==true then this method must be overridden.
*/
protected Object valueFromRowInternalWithJoin(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery sourceQuery, CacheKey parentCacheKey, AbstractSession executionSession, boolean isTargetProtected) throws DatabaseException {
throw ValidationException.mappingDoesNotOverrideValueFromRowInternalWithJoin(Helper.getShortClassName(this.getClass()));
}
/**
* INTERNAL:
* Return the value of the reference attribute or a value holder.
* Check whether the mapping's attribute should be optimized through batch and joining.
*/
protected Object valueFromRowInternal(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery sourceQuery, AbstractSession executionSession) throws DatabaseException {
return valueFromRowInternal(row, joinManager, sourceQuery, executionSession, false);
}
/**
* INTERNAL:
* Return the value of the reference attribute or a value holder.
* Check whether the mapping's attribute should be optimized through batch and joining.
* @param shouldUseSopObject indicates whether sopObject stored in the row should be used to extract the value (and fields/values stored in the row ignored).
*/
protected Object valueFromRowInternal(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery sourceQuery, AbstractSession executionSession, boolean shouldUseSopObject) throws DatabaseException {
// PERF: Direct variable access.
ReadQuery targetQuery = this.selectionQuery;
if (shouldUseSopObject) {
Object sopAttribute = getAttributeValueFromObject(row.getSopObject());
Object sopRealAttribute;
if (isCollectionMapping()) {
if (sopAttribute == null) {
return getContainerPolicy().containerInstance();
}
sopRealAttribute = getIndirectionPolicy().getRealAttributeValueFromObject(row.getSopObject(), sopAttribute);
if (getContainerPolicy().isEmpty(sopRealAttribute)) {
return sopAttribute;
}
} else {
if (sopAttribute == null) {
return this.indirectionPolicy.nullValueFromRow();
}
// As part of SOP object the indirection should be already triggered
sopRealAttribute = getIndirectionPolicy().getRealAttributeValueFromObject(row.getSopObject(), sopAttribute);
if (sopRealAttribute == null) {
return sopAttribute;
}
}
DatabaseRecord sopRow = new DatabaseRecord(0);
sopRow.setSopObject(sopRealAttribute);
row = sopRow;
}
// Bug 464088
if (executionSession.isHistoricalSession() && !targetQuery.isPrepared()) {
targetQuery = (ReadQuery) targetQuery.clone();
targetQuery.setIsExecutionClone(true);
}
// Copy nested fetch group from the source query
if (targetQuery.isObjectLevelReadQuery() && targetQuery.getDescriptor().hasFetchGroupManager()) {
FetchGroup sourceFG = sourceQuery.getExecutionFetchGroup(this.getDescriptor());
if (sourceFG != null) {
FetchGroup targetFetchGroup = sourceFG.getGroup(getAttributeName());
if(targetFetchGroup != null) {
// perf: bug#4751950, first prepare the query before cloning.
if (targetQuery.shouldPrepare()) {
targetQuery.checkPrepare(executionSession, row);
}
targetQuery = (ReadQuery) targetQuery.clone();
targetQuery.setIsExecutionClone(true);
((ObjectLevelReadQuery)targetQuery).setFetchGroup(targetFetchGroup);
}
}
}
// CR #4365, 3610825 - moved up from the block below, needs to be set with
// indirection off. Clone the query and set its id.
// All indirections are triggered in sopObject, therefore if sopObject is used then indirection on targetQuery to be triggered, too.
if (!this.indirectionPolicy.usesIndirection() || shouldUseSopObject) {
if (targetQuery == this.selectionQuery) {
// perf: bug#4751950, first prepare the query before cloning.
if (targetQuery.shouldPrepare()) {
targetQuery.checkPrepare(executionSession, row);
}
targetQuery = (ReadQuery) targetQuery.clone();
targetQuery.setIsExecutionClone(true);
}
targetQuery.setQueryId(sourceQuery.getQueryId());
if (sourceQuery.usesResultSetAccessOptimization()) {
targetQuery.setAccessors(sourceQuery.getAccessors());
}
if(targetQuery.isObjectLevelReadQuery()) {
((ObjectLevelReadQuery)targetQuery).setRequiresDeferredLocks(sourceQuery.requiresDeferredLocks());
}
}
// If the source query is cascading then the target query must use the same settings.
if (targetQuery.isObjectLevelReadQuery()) {
if (sourceQuery.shouldCascadeAllParts() || (this.isPrivateOwned && sourceQuery.shouldCascadePrivateParts()) || (this.cascadeRefresh && sourceQuery.shouldCascadeByMapping())) {
// If the target query has already been cloned (we're refreshing) avoid
// re-cloning the query again.
if (targetQuery == this.selectionQuery) {
// perf: bug#4751950, first prepare the query before cloning.
if (targetQuery.shouldPrepare()) {
targetQuery.checkPrepare(executionSession, row);
}
targetQuery = (ReadQuery) targetQuery.clone();
targetQuery.setIsExecutionClone(true);
}
((ObjectLevelReadQuery)targetQuery).setShouldRefreshIdentityMapResult(sourceQuery.shouldRefreshIdentityMapResult());
targetQuery.setCascadePolicy(sourceQuery.getCascadePolicy());
// For queries that have turned caching off, such as aggregate collection, leave it off.
if (targetQuery.shouldMaintainCache()) {
targetQuery.setShouldMaintainCache(sourceQuery.shouldMaintainCache());
}
// For flashback: Read attributes as of the same time if required.
if (sourceQuery.isObjectLevelReadQuery() && ((ObjectLevelReadQuery)sourceQuery).hasAsOfClause()) {
targetQuery.setSelectionCriteria((Expression)targetQuery.getSelectionCriteria().clone());
((ObjectLevelReadQuery)targetQuery).setAsOfClause(((ObjectLevelReadQuery)sourceQuery).getAsOfClause());
}
}
if (isExtendingPessimisticLockScope(sourceQuery)) {
if (this.extendPessimisticLockScope == ExtendPessimisticLockScope.TARGET_QUERY) {
if (targetQuery == this.selectionQuery) {
// perf: bug#4751950, first prepare the query before cloning.
if (targetQuery.shouldPrepare()) {
targetQuery.checkPrepare(executionSession, row);
}
targetQuery = (ReadQuery) targetQuery.clone();
targetQuery.setIsExecutionClone(true);
}
extendPessimisticLockScopeInTargetQuery((ObjectLevelReadQuery)targetQuery, sourceQuery);
} else if (this.extendPessimisticLockScope == ExtendPessimisticLockScope.DEDICATED_QUERY) {
ReadQuery dedicatedQuery = getExtendPessimisticLockScopeDedicatedQuery(executionSession, sourceQuery.getLockMode());
executionSession.executeQuery(dedicatedQuery, row);
}
}
}
targetQuery = prepareHistoricalQuery(targetQuery, sourceQuery, executionSession);
return this.indirectionPolicy.valueFromQuery(targetQuery, row, executionSession);
}
/**
* INTERNAL:
* Indicates whether the source query's pessimistic lock scope scope should be extended in the target query.
*/
protected boolean isExtendingPessimisticLockScope(ObjectBuildingQuery sourceQuery) {
// TODO: What if sourceQuery is NOT ObjectLevelReadQuery? Should we somehow handle this?
// Or alternatively define ObjectBuildingQuery.shouldExtendPessimisticLockScope() to always return false?
return sourceQuery.isLockQuery() && sourceQuery.isObjectLevelReadQuery() && ((ObjectLevelReadQuery)sourceQuery).shouldExtendPessimisticLockScope();
}
/**
* INTERNAL:
* Allow for the mapping to perform any historical query additions.
* Return the new target query.
*/
protected ReadQuery prepareHistoricalQuery(ReadQuery targetQuery, ObjectBuildingQuery sourceQuery, AbstractSession executionSession) {
return targetQuery;
}
/**
* INTERNAL:
* Return a sub-partition of the row starting at the index for the mapping.
*/
public AbstractRecord trimRowForJoin(AbstractRecord row, JoinedAttributeManager joinManager, AbstractSession executionSession) {
// The field for many objects may be in the row,
// so build the subpartion of the row through the computed values in the query,
// this also helps the field indexing match.
if ((joinManager != null) && (joinManager.getJoinedMappingIndexes_() != null)) {
Object value = joinManager.getJoinedMappingIndexes_().get(this);
if (value != null) {
return trimRowForJoin(row, value, executionSession);
}
}
return row;
}
/**
* INTERNAL:
* Return a sub-partition of the row starting at the index.
*/
public AbstractRecord trimRowForJoin(AbstractRecord row, Object value, AbstractSession executionSession) {
// CR #... the field for many objects may be in the row,
// so build the subpartion of the row through the computed values in the query,
// this also helps the field indexing match.
int fieldStartIndex;
if (value instanceof Integer) {
fieldStartIndex = ((Integer)value).intValue();
} else {
// must be Map of classes to Integers
Map map = (Map)value;
Class cls;
if (getDescriptor().hasInheritance() && getDescriptor().getInheritancePolicy().shouldReadSubclasses()) {
cls = getDescriptor().getInheritancePolicy().classFromRow(row, executionSession);
} else {
cls = getDescriptor().getJavaClass();
}
fieldStartIndex = ((Integer)map.get(cls)).intValue();
}
Vector trimedFields = new NonSynchronizedSubVector(row.getFields(), fieldStartIndex, row.size());
Vector trimedValues = new NonSynchronizedSubVector(row.getValues(), fieldStartIndex, row.size());
return new DatabaseRecord(trimedFields, trimedValues);
}
/**
* INTERNAL:
* Prepare the clone of the nested query for joining.
* The nested query clones are stored on the execution (clone) joinManager to avoid cloning per row.
*/
protected ObjectLevelReadQuery prepareNestedJoinQueryClone(AbstractRecord row, List dataResults, JoinedAttributeManager joinManager, ObjectBuildingQuery sourceQuery, AbstractSession executionSession) {
// A nested query must be built to pass to the descriptor that looks like the real query execution would,
// these should be cached on the query during prepare.
ObjectLevelReadQuery nestedQuery = null;
// This is also call for partial object reading.
if (joinManager == null) {
nestedQuery = prepareNestedJoins(null, sourceQuery, executionSession);
nestedQuery.setSession(executionSession);
nestedQuery.setPrefetchedCacheKeys(sourceQuery.getPrefetchedCacheKeys());
return nestedQuery;
}
// PERF: Also store the clone of the nested query on the execution query to avoid
// cloning per row.
if (joinManager.getJoinedMappingQueryClones() == null) {
joinManager.setJoinedMappingQueryClones(new HashMap(5));
}
nestedQuery = joinManager.getJoinedMappingQueryClones().get(this);
if (nestedQuery == null) {
if (joinManager.getJoinedMappingQueries_() != null) {
nestedQuery = joinManager.getJoinedMappingQueries_().get(this);
nestedQuery = (ObjectLevelReadQuery)nestedQuery.clone();
} else {
nestedQuery = prepareNestedJoins(joinManager, sourceQuery, executionSession);
}
nestedQuery.setSession(executionSession);
//CR #4365 - used to prevent infinite recursion on refresh object cascade all
nestedQuery.setQueryId(joinManager.getBaseQuery().getQueryId());
nestedQuery.setExecutionTime(joinManager.getBaseQuery().getExecutionTime());
joinManager.getJoinedMappingQueryClones().put(this, nestedQuery);
}
nestedQuery.setPrefetchedCacheKeys(sourceQuery.getPrefetchedCacheKeys());
// Must also set data results to the nested query if it uses to-many joining.
if (nestedQuery.hasJoining() && nestedQuery.getJoinedAttributeManager().isToManyJoin()) {
// The data results only of the child object are required, they must also be trimmed.
List nestedDataResults = dataResults;
if (nestedDataResults == null) {
// Extract the primary key of the source object, to filter only the joined rows for that object.
Object sourceKey = this.descriptor.getObjectBuilder().extractPrimaryKeyFromRow(row, executionSession);
nestedDataResults = joinManager.getDataResultsByPrimaryKey().get(sourceKey);
}
nestedDataResults = new ArrayList(nestedDataResults);
Object indexObject = joinManager.getJoinedMappingIndexes_().get(this);
// Trim results to start at nested row index.
for (int index = 0; index < nestedDataResults.size(); index++) {
AbstractRecord sourceRow = (AbstractRecord)nestedDataResults.get(index);
nestedDataResults.set(index, trimRowForJoin(sourceRow, indexObject, executionSession));
}
nestedQuery.getJoinedAttributeManager().setDataResults(nestedDataResults, executionSession);
}
// Must also set data results to the nested query if it uses nested IN batch fetching.
/*if (nestedQuery.hasBatchReadAttributes() && nestedQuery.getBatchFetchPolicy().isIN()) {
List sourceDataResults = ((ObjectLevelReadQuery)sourceQuery).getBatchFetchPolicy().getAllDataResults();
// The data results only of the child object are required, they must also be trimmed.
List nestedDataResults = new ArrayList(sourceDataResults.size());
Object indexObject = joinManager.getJoinedMappingIndexes_().get(this);
// Trim results to start at nested row index.
for (int index = 0; index < sourceDataResults.size(); index++) {
AbstractRecord sourceRow = (AbstractRecord)sourceDataResults.get(index);
if (sourceRow != null) {
nestedDataResults.add(trimRowForJoin(sourceRow, indexObject, executionSession));
}
}
nestedQuery.getBatchFetchPolicy().setDataResults(nestedDataResults);
}*/
nestedQuery.setRequiresDeferredLocks(sourceQuery.requiresDeferredLocks());
return nestedQuery;
}
/**
* PUBLIC:
* Return the type of batch fetching to use for all queries for this class if configured.
*/
public BatchFetchType getBatchFetchType() {
return batchFetchType;
}
/**
* PUBLIC:
* Set the type of batch fetching to use for all queries for this class.
*/
public void setBatchFetchType(BatchFetchType batchFetchType) {
this.batchFetchType = batchFetchType;
}
/**
* INTERNAL:
* Allow subclass to define a foreign key in the target's table.
*/
public void addTargetForeignKeyField(DatabaseField targetForeignKeyField, DatabaseField sourcePrimaryKeyField) {
throw new UnsupportedOperationException("addTargetForeignKeyField");
}
/**
* INTERNAL:
* Allow subclass to define a foreign key in the source's table.
*/
public void addForeignKeyField(DatabaseField sourceForeignKeyField, DatabaseField targetPrimaryKeyField) {
throw new UnsupportedOperationException("addForeignKeyField");
}
/**
* INTERNAL:
* Relationships order by their target primary key fields by default.
*/
@Override
public List<Expression> getOrderByNormalizedExpressions(Expression base) {
List<Expression> orderBys = new ArrayList(this.referenceDescriptor.getPrimaryKeyFields().size());
for (DatabaseField field : this.referenceDescriptor.getPrimaryKeyFields()) {
orderBys.add(base.getField(field));
}
return orderBys;
}
}
|