Author: brmeyer
Date: 2012-08-30 19:34:53 -0400 (Thu, 30 Aug 2012)
New Revision: 21066
Added:
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/hhh7557/
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/hhh7557/EntityMapTest.java
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/hhh7557/MapHolder.java
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/hhh7557/MapKey.java
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/hhh7557/MapValue.java
Modified:
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/collection/PersistentMap.java
Log:
JBPAPP-9792 HHH-7557 Map entries get deleted
Added:
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/hhh7557/EntityMapTest.java
===================================================================
---
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/hhh7557/EntityMapTest.java
(rev 0)
+++
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/hhh7557/EntityMapTest.java 2012-08-30
23:34:53 UTC (rev 21066)
@@ -0,0 +1,103 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2012, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.test.collection.map.hhh7557;
+
+import java.util.HashMap;
+import java.util.List;
+
+import junit.framework.Assert;
+
+import org.hibernate.Session;
+import org.hibernate.test.annotations.TestCase;
+
+/**
+ * @author Elizabeth Chatman
+ * @author Steve Ebersole
+ */
+public class EntityMapTest extends TestCase {
+
+ @Override
+ protected Class<?>[] getAnnotatedClasses() {
+ return new Class[] {MapValue.class, MapKey.class, MapHolder.class};
+ }
+
+ public void testInsertIntoMap() throws Exception {
+ {
+ // Session 1: Insert 3 values into the map
+ Session session = openSession();
+ session.beginTransaction();
+ MapHolder mapHolder = new MapHolder();
+ mapHolder.setMap( new HashMap<MapKey, MapValue>() );
+ addMapEntry( session, mapHolder, "A", "1" );
+ addMapEntry( session, mapHolder, "B", "2" );
+ addMapEntry( session, mapHolder, "C", "3" );
+ session.save( mapHolder );
+ // Verify there are 3 entries in the map
+ Assert.assertEquals( 3, mapHolder.getMap().size() );
+ session.getTransaction().commit();
+ session.close();
+ }
+
+ {
+ // Session 2: Add a 4th value to the map
+ Session session = openSession();
+ session.beginTransaction();
+ MapHolder mapHolder = getMapHolder( session );
+ System.out.println( "Got MapHolder; checking map size -----" );
+ Assert.assertEquals( 3, mapHolder.getMap().size() );
+ System.out.println( "Got MapHolder; checked map size -----" );
+ addMapEntry( session, mapHolder, "D", "4" );
+ // Verify there are 4 entries in the map
+ Assert.assertEquals( 4, mapHolder.getMap().size() );
+ session.getTransaction().commit();
+ session.close();
+ }
+
+ {
+ // Session 3: Count the entries in the map
+ Session session = openSession();
+ session.beginTransaction();
+ MapHolder mapHolder = getMapHolder( session );
+ // Fails here (expected:<4> but was:<1>)
+ Assert.assertEquals( 4, mapHolder.getMap().size() );
+ session.getTransaction().commit();
+ session.close();
+ }
+ }
+
+ private void addMapEntry(Session session, MapHolder mapHolder, String key, String value)
{
+ System.out.println( "Inserting (" + key + "," + value + ")
into map" );
+ MapValue entityValue = new MapValue( value );
+ session.save( entityValue );
+ MapKey entityKey = new MapKey( key, entityValue );
+ session.save( entityKey );
+ mapHolder.getMap().put( entityKey, entityValue );
+ }
+
+ private MapHolder getMapHolder(Session session) {
+ List mapHolders = session.createQuery( "select distinct mh from MapHolder mh"
).list();
+ Assert.assertEquals( 1, mapHolders.size() );
+ return (MapHolder) mapHolders.get( 0 );
+ }
+}
Added:
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/hhh7557/MapHolder.java
===================================================================
---
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/hhh7557/MapHolder.java
(rev 0)
+++
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/hhh7557/MapHolder.java 2012-08-30
23:34:53 UTC (rev 21066)
@@ -0,0 +1,80 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2012, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.test.collection.map.hhh7557;
+
+import java.util.Map;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinTable;
+import javax.persistence.ManyToMany;
+import javax.persistence.Table;
+
+/**
+ * @author Elizabeth Chatman
+ * @author Steve Ebersole
+ */
+@Entity
+@Table(name = "map_holder")
+public class MapHolder {
+ private Long id;
+ private Map<MapKey, MapValue> map;
+
+ @Id
+ @GeneratedValue
+ @Column(name = "id", unique = true, nullable = false)
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ @ManyToMany
+ @JoinTable(
+ name = "map_key_map_value",
+ joinColumns = @JoinColumn(name = "map_holder_id", nullable = false),
+ inverseJoinColumns = @JoinColumn(name = "map_value_id", nullable = false)
+ )
+// @MapKeyJoinColumn(name = "map_key_id", nullable = false)
+ public Map<MapKey, MapValue> getMap() {
+ return map;
+ }
+
+ public void setMap(Map<MapKey, MapValue> map) {
+ this.map = map;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder();
+ builder.append( "MapHolder [id=" ).append( id ).append( "]" );
+ return builder.toString();
+ }
+
+}
Added:
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/hhh7557/MapKey.java
===================================================================
---
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/hhh7557/MapKey.java
(rev 0)
+++
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/hhh7557/MapKey.java 2012-08-30
23:34:53 UTC (rev 21066)
@@ -0,0 +1,137 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2012, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.test.collection.map.hhh7557;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+import javax.persistence.Table;
+import javax.persistence.UniqueConstraint;
+
+/**
+ * @author Elizabeth Chatman
+ * @author Steve Ebersole
+ */
+@Entity
+@Table(name = "map_key", uniqueConstraints = {
+ @UniqueConstraint(columnNames = {"name", "default_map_value_id"})
+})
+public class MapKey {
+ private Long id;
+ private String name;
+ private MapValue defaultValue;
+
+ public MapKey() {
+ }
+
+ public MapKey(String name, MapValue defaultValue) {
+ this.name = name;
+ this.defaultValue = defaultValue;
+ }
+
+ @Id
+ @GeneratedValue
+ @Column(name = "id", unique = true, nullable = false)
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ @Column(name = "name", nullable = false)
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ @ManyToOne(fetch = FetchType.EAGER)
+ @JoinColumn(name = "default_map_value_id", nullable = false)
+ public MapValue getDefaultValue() {
+ return defaultValue;
+ }
+
+ public void setDefaultValue(MapValue defaultValue) {
+ this.defaultValue = defaultValue;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((getDefaultValue() == null) ? 0 :
getDefaultValue().hashCode());
+ result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if ( this == obj ) {
+ return true;
+ }
+ if ( obj == null ) {
+ return false;
+ }
+ if ( !(obj instanceof MapKey) ) {
+ return false;
+ }
+ MapKey other = (MapKey) obj;
+ if ( getDefaultValue() == null ) {
+ if ( other.getDefaultValue() != null ) {
+ return false;
+ }
+ }
+ else if ( !getDefaultValue().equals( other.getDefaultValue() ) ) {
+ return false;
+ }
+ if ( getName() == null ) {
+ if ( other.getName() != null ) {
+ return false;
+ }
+ }
+ else if ( !getName().equals( other.getName() ) ) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder();
+ builder.append( "MapKey [id=" ).append( getId() )
+ .append( ", name=" ).append( getName() )
+ .append( ", defaultValue=" ).append( getDefaultValue() )
+ .append( "]" );
+ return builder.toString();
+ }
+
+}
\ No newline at end of file
Added:
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/hhh7557/MapValue.java
===================================================================
---
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/hhh7557/MapValue.java
(rev 0)
+++
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/collection/map/hhh7557/MapValue.java 2012-08-30
23:34:53 UTC (rev 21066)
@@ -0,0 +1,110 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2012, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.test.collection.map.hhh7557;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+/**
+ * @author Elizabeth Chatman
+ * @author Steve Ebersole
+ */
+@Entity
+@Table(name = "map_value")
+public class MapValue {
+ private Long id;
+ private String name;
+
+ public MapValue() {
+ }
+
+ public MapValue(String name) {
+ this.name = name;
+ }
+
+ @Id
+ @GeneratedValue
+ @Column(name = "id", unique = true, nullable = false)
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ @Column(name = "name", unique = true, nullable = false)
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ int previousHashCode = -1;
+
+ @Override
+ public int hashCode() {
+
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
+ previousHashCode = result;
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if ( this == obj ) {
+ return true;
+ }
+ if ( obj == null ) {
+ return false;
+ }
+ if ( !(obj instanceof MapValue) ) {
+ return false;
+ }
+ MapValue other = (MapValue) obj;
+ if ( getName() == null ) {
+ if ( other.getName() != null ) {
+ return false;
+ }
+ }
+ else if ( !getName().equals( other.getName() ) ) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder();
+ builder.append( "MapValue [id=" ).append( getId() ).append( ",
name=" ).append( getName() ).append( "]" );
+ return builder.toString();
+ }
+}
Modified:
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/collection/PersistentMap.java
===================================================================
---
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/collection/PersistentMap.java 2012-08-30
16:55:39 UTC (rev 21065)
+++
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/collection/PersistentMap.java 2012-08-30
23:34:53 UTC (rev 21066)
@@ -275,14 +275,35 @@
return map.toString();
}
- public Object readFrom(ResultSet rs, CollectionPersister persister, CollectionAliases
descriptor, Object owner)
- throws HibernateException, SQLException {
- Object element = persister.readElement( rs, owner,
descriptor.getSuffixedElementAliases(), getSession() );
- Object index = persister.readIndex( rs, descriptor.getSuffixedIndexAliases(),
getSession() );
- if ( element!=null ) map.put(index, element);
+ private transient List<Object[]> loadingEntries;
+
+ public Object readFrom(
+ ResultSet rs,
+ CollectionPersister persister,
+ CollectionAliases descriptor,
+ Object owner) throws HibernateException, SQLException {
+ final Object element = persister.readElement( rs, owner,
descriptor.getSuffixedElementAliases(), getSession() );
+ if ( element != null ) {
+ final Object index = persister.readIndex( rs, descriptor.getSuffixedIndexAliases(),
getSession() );
+ if ( loadingEntries == null ) {
+ loadingEntries = new ArrayList<Object[]>();
+ }
+ loadingEntries.add( new Object[] { index, element } );
+ }
return element;
}
+ @Override
+ @SuppressWarnings("unchecked")
+ public boolean endRead() {
+ if ( loadingEntries != null ) {
+ for ( Object[] entry : loadingEntries ) {
+ map.put( entry[0], entry[1] );
+ }
+ }
+ return super.endRead();
+ }
+
public Iterator entries(CollectionPersister persister) {
return map.entrySet().iterator();
}