@@ -1572,117 +1572,86 @@ public void serialize(ObjectOutputStream oos) throws IOException {
1572
1572
oos .writeBoolean ( defaultReadOnly );
1573
1573
oos .writeBoolean ( hasNonReadOnlyEntities );
1574
1574
1575
- if ( entitiesByKey == null ) {
1576
- oos .writeInt ( 0 );
1577
- }
1578
- else {
1579
- oos .writeInt ( entitiesByKey .size () );
1580
- if ( LOG .isTraceEnabled () ) {
1581
- LOG .trace ( "Starting serialization of [" + entitiesByKey .size () + "] entitiesByKey entries" );
1582
- }
1583
- for ( Map .Entry <EntityKey ,Object > entry : entitiesByKey .entrySet () ) {
1584
- entry .getKey ().serialize ( oos );
1585
- oos .writeObject ( entry .getValue () );
1586
- }
1587
- }
1588
-
1589
- if ( entitiesByUniqueKey == null ) {
1590
- oos .writeInt ( 0 );
1591
- }
1592
- else {
1593
- oos .writeInt ( entitiesByUniqueKey .size () );
1594
- if ( LOG .isTraceEnabled () ) {
1595
- LOG .trace ( "Starting serialization of [" + entitiesByUniqueKey .size () + "] entitiesByUniqueKey entries" );
1596
- }
1597
- for ( Map .Entry <EntityUniqueKey ,Object > entry : entitiesByUniqueKey .entrySet () ) {
1598
- entry .getKey ().serialize ( oos );
1599
- oos .writeObject ( entry .getValue () );
1600
- }
1601
- }
1602
-
1603
- if ( proxiesByKey == null ) {
1604
- oos .writeInt ( 0 );
1605
- }
1606
- else {
1607
- oos .writeInt ( proxiesByKey .size () );
1608
- if ( LOG .isTraceEnabled () ) {
1609
- LOG .trace ( "Starting serialization of [" + proxiesByKey .size () + "] proxiesByKey entries" );
1610
- }
1611
- for ( Map .Entry <EntityKey ,Object > entry : proxiesByKey .entrySet () ) {
1612
- entry .getKey ().serialize ( oos );
1613
- oos .writeObject ( entry .getValue () );
1614
- }
1615
- }
1616
-
1617
- if ( entitySnapshotsByKey == null ) {
1618
- oos .writeInt ( 0 );
1619
- }
1620
- else {
1621
- oos .writeInt ( entitySnapshotsByKey .size () );
1622
- if ( LOG .isTraceEnabled () ) {
1623
- LOG .trace ( "Starting serialization of [" + entitySnapshotsByKey .size () + "] entitySnapshotsByKey entries" );
1624
- }
1625
- for ( Map .Entry <EntityKey ,Object > entry : entitySnapshotsByKey .entrySet () ) {
1626
- entry .getKey ().serialize ( oos );
1627
- oos .writeObject ( entry .getValue () );
1628
- }
1629
- }
1575
+ final Serializer <Map .Entry <EntityKey , Object >> entityKeySerializer = (entry , stream ) -> {
1576
+ entry .getKey ().serialize ( stream );
1577
+ stream .writeObject ( entry .getValue () );
1578
+ };
1579
+
1580
+ writeMapToStream ( entitiesByKey , oos , "entitiesByKey" , entityKeySerializer );
1581
+ writeMapToStream (
1582
+ entitiesByUniqueKey ,
1583
+ oos , "entitiesByUniqueKey" , (entry , stream ) -> {
1584
+ entry .getKey ().serialize ( stream );
1585
+ stream .writeObject ( entry .getValue () );
1586
+ }
1587
+ );
1588
+ writeMapToStream ( proxiesByKey , oos , "proxiesByKey" , entityKeySerializer );
1589
+ writeMapToStream ( entitySnapshotsByKey , oos , "entitySnapshotsByKey" , entityKeySerializer );
1630
1590
1631
1591
entityEntryContext .serialize ( oos );
1592
+ writeMapToStream (
1593
+ collectionsByKey ,
1594
+ oos ,
1595
+ "collectionsByKey" ,
1596
+ (entry , stream ) -> {
1597
+ entry .getKey ().serialize ( stream );
1598
+ stream .writeObject ( entry .getValue () );
1599
+ }
1600
+ );
1601
+ writeMapToStream (
1602
+ collectionEntries ,
1603
+ oos ,
1604
+ "collectionEntries" ,
1605
+ (entry , stream ) -> {
1606
+ stream .writeObject ( entry .getKey () );
1607
+ entry .getValue ().serialize ( stream );
1608
+ }
1609
+ );
1610
+ writeMapToStream (
1611
+ arrayHolders ,
1612
+ oos ,
1613
+ "arrayHolders" ,
1614
+ (entry , stream ) -> {
1615
+ stream .writeObject ( entry .getKey () );
1616
+ stream .writeObject ( entry .getValue () );
1617
+ }
1618
+ );
1619
+ writeCollectionToStream ( nullifiableEntityKeys , oos , "nullifiableEntityKey" , EntityKey ::serialize );
1620
+ }
1632
1621
1633
- if ( collectionsByKey == null ) {
1634
- oos .writeInt ( 0 );
1635
- }
1636
- else {
1637
- oos .writeInt ( collectionsByKey .size () );
1638
- if ( LOG .isTraceEnabled () ) {
1639
- LOG .trace ( "Starting serialization of [" + collectionsByKey .size () + "] collectionsByKey entries" );
1640
- }
1641
- for ( Map .Entry <CollectionKey , PersistentCollection > entry : collectionsByKey .entrySet () ) {
1642
- entry .getKey ().serialize ( oos );
1643
- oos .writeObject ( entry .getValue () );
1644
- }
1645
- }
1622
+ private interface Serializer <E > {
1646
1623
1647
- if ( collectionEntries == null ) {
1648
- oos .writeInt ( 0 );
1649
- }
1650
- else {
1651
- oos .writeInt ( collectionEntries .size () );
1652
- if ( LOG .isTraceEnabled () ) {
1653
- LOG .trace ( "Starting serialization of [" + collectionEntries .size () + "] collectionEntries entries" );
1654
- }
1655
- for ( Map .Entry <PersistentCollection ,CollectionEntry > entry : collectionEntries .entrySet () ) {
1656
- oos .writeObject ( entry .getKey () );
1657
- entry .getValue ().serialize ( oos );
1658
- }
1659
- }
1624
+ void serialize (E element , ObjectOutputStream oos ) throws IOException ;
1625
+ }
1660
1626
1661
- if ( arrayHolders == null ) {
1627
+ private <K , V > void writeMapToStream (
1628
+ Map <K , V > map ,
1629
+ ObjectOutputStream oos ,
1630
+ String keysName ,
1631
+ Serializer <Entry <K , V >> serializer ) throws IOException {
1632
+ if ( map == null ) {
1662
1633
oos .writeInt ( 0 );
1663
1634
}
1664
1635
else {
1665
- oos .writeInt ( arrayHolders .size () );
1666
- if ( LOG .isTraceEnabled () ) {
1667
- LOG .trace ( "Starting serialization of [" + arrayHolders .size () + "] arrayHolders entries" );
1668
- }
1669
- for ( Map .Entry <Object ,PersistentCollection > entry : arrayHolders .entrySet () ) {
1670
- oos .writeObject ( entry .getKey () );
1671
- oos .writeObject ( entry .getValue () );
1672
- }
1636
+ writeCollectionToStream ( map .entrySet (), oos , keysName , serializer );
1673
1637
}
1638
+ }
1674
1639
1675
- if ( nullifiableEntityKeys == null ) {
1640
+ private <E > void writeCollectionToStream (
1641
+ Collection <E > collection ,
1642
+ ObjectOutputStream oos ,
1643
+ String keysName ,
1644
+ Serializer <E > serializer ) throws IOException {
1645
+ if ( collection == null ) {
1676
1646
oos .writeInt ( 0 );
1677
1647
}
1678
1648
else {
1679
- final int size = nullifiableEntityKeys .size ();
1649
+ oos . writeInt ( collection .size () );
1680
1650
if ( LOG .isTraceEnabled () ) {
1681
- LOG .trace ( "Starting serialization of [" + size + "] nullifiableEntityKey entries" );
1651
+ LOG .trace ( "Starting serialization of [" + collection . size () + "] " + keysName + " entries" );
1682
1652
}
1683
- oos .writeInt ( size );
1684
- for ( EntityKey entry : nullifiableEntityKeys ) {
1685
- entry .serialize ( oos );
1653
+ for ( E entry : collection ) {
1654
+ serializer .serialize ( entry , oos );
1686
1655
}
1687
1656
}
1688
1657
}
@@ -2142,9 +2111,9 @@ public Object[] removeLocalNaturalIdCrossReference(EntityPersister persister, Se
2142
2111
final Object [] naturalIdValues = getNaturalIdValues ( state , persister );
2143
2112
2144
2113
final Object [] localNaturalIdValues = getNaturalIdXrefDelegate ().removeNaturalIdCrossReference (
2145
- persister ,
2146
- id ,
2147
- naturalIdValues
2114
+ persister ,
2115
+ id ,
2116
+ naturalIdValues
2148
2117
);
2149
2118
2150
2119
return localNaturalIdValues != null ? localNaturalIdValues : naturalIdValues ;
0 commit comments