|
From: <hib...@li...> - 2006-05-01 19:07:59
|
Author: epbernard
Date: 2006-05-01 15:07:55 -0400 (Mon, 01 May 2006)
New Revision: 9844
Added:
trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/indexcoll/AlphabeticalDirectory.java
Modified:
trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/MapBinder.java
trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/indexcoll/AddressBook.java
trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/indexcoll/AddressEntry.java
trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java
Log:
ANN-281 support @javax.persistence.MapKey referencing an entity
Modified: trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/MapBinder.java
===================================================================
--- trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/MapBinder.java 2006-05-01 18:11:06 UTC (rev 9843)
+++ trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/MapBinder.java 2006-05-01 19:07:55 UTC (rev 9844)
@@ -237,16 +237,28 @@
newProperty.setPersistentClass( current.getPersistentClass() );
newProperty.setPropertyAccessorName( current.getPropertyAccessorName() );
newProperty.setSelectable( current.isSelectable() );
- newProperty.setValue( createFormulatedValue( current.getValue(), collection ) );
+ newProperty.setValue( createFormulatedValue( current.getValue(), collection) );
indexComponent.addProperty( newProperty );
}
return indexComponent;
}
- else {
+ else if ( value instanceof SimpleValue ) {
SimpleValue sourceValue = (SimpleValue) value;
- SimpleValue targetValue = new SimpleValue( collection.getCollectionTable() );
- targetValue.setTypeName( sourceValue.getTypeName() );
- targetValue.setTypeParameters( sourceValue.getTypeParameters() );
+ SimpleValue targetValue;
+ if( value instanceof ManyToOne ) {
+ ManyToOne sourceManyToOne = (ManyToOne) sourceValue;
+ ManyToOne targetManyToOne = new ManyToOne( collection.getCollectionTable() );
+ targetManyToOne.setFetchMode( FetchMode.DEFAULT );
+ targetManyToOne.setLazy( true );
+ //targetValue.setIgnoreNotFound( ); does not make sense for a map key
+ targetManyToOne.setReferencedEntityName( sourceManyToOne.getReferencedEntityName() );
+ targetValue = targetManyToOne;
+ }
+ else {
+ targetValue = new SimpleValue( collection.getCollectionTable() );
+ targetValue.setTypeName( sourceValue.getTypeName() );
+ targetValue.setTypeParameters( sourceValue.getTypeParameters() );
+ }
Iterator columns = sourceValue.getColumnIterator();
while ( columns.hasNext() ) {
Object current = columns.next();
@@ -266,5 +278,8 @@
}
return targetValue;
}
+ else {
+ throw new AssertionFailure( "Unknown type encounters for map key: " + value.getClass() );
+ }
}
}
Modified: trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/indexcoll/AddressBook.java
===================================================================
--- trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/indexcoll/AddressBook.java 2006-05-01 18:11:06 UTC (rev 9843)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/indexcoll/AddressBook.java 2006-05-01 19:07:55 UTC (rev 9844)
@@ -19,6 +19,7 @@
private String owner;
private Map<AddressEntryPk, AddressEntry> entries = new HashMap<AddressEntryPk, AddressEntry>();
private Map<String, AddressEntry> lastNameEntries = new HashMap<String, AddressEntry>();
+ private Map<AlphabeticalDirectory, AddressEntry> directoryEntries = new HashMap<AlphabeticalDirectory, AddressEntry>();
@Id
@GeneratedValue
@@ -58,4 +59,14 @@
this.lastNameEntries = lastNameEntries;
}
+ @MapKey(name = "directory")
+ @OneToMany(mappedBy = "book")
+ public Map<AlphabeticalDirectory, AddressEntry> getDirectoryEntries() {
+ return directoryEntries;
+ }
+
+ public void setDirectoryEntries(Map<AlphabeticalDirectory, AddressEntry> directoryEntries) {
+ this.directoryEntries = directoryEntries;
+ }
+
}
Modified: trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/indexcoll/AddressEntry.java
===================================================================
--- trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/indexcoll/AddressEntry.java 2006-05-01 18:11:06 UTC (rev 9843)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/indexcoll/AddressEntry.java 2006-05-01 19:07:55 UTC (rev 9844)
@@ -14,6 +14,7 @@
private String street;
private String city;
private AddressBook book;
+ private AlphabeticalDirectory directory;
public boolean equals(Object o) {
if ( this == o ) return true;
@@ -63,4 +64,13 @@
public void setBook(AddressBook book) {
this.book = book;
}
+
+ @ManyToOne
+ public AlphabeticalDirectory getDirectory() {
+ return directory;
+ }
+
+ public void setDirectory(AlphabeticalDirectory directory) {
+ this.directory = directory;
+ }
}
Added: trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/indexcoll/AlphabeticalDirectory.java
===================================================================
--- trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/indexcoll/AlphabeticalDirectory.java 2006-05-01 18:11:06 UTC (rev 9843)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/indexcoll/AlphabeticalDirectory.java 2006-05-01 19:07:55 UTC (rev 9844)
@@ -0,0 +1,31 @@
+//$Id: $
+package org.hibernate.test.annotations.indexcoll;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+public class AlphabeticalDirectory {
+ @Id @GeneratedValue private Integer id;
+ private String name;
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Modified: trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java
===================================================================
--- trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java 2006-05-01 18:11:06 UTC (rev 9843)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java 2006-05-01 19:07:55 UTC (rev 9844)
@@ -201,6 +201,50 @@
s.close();
}
+ public void testMapKeyToEntity() throws Exception {
+ Session s;
+ Transaction tx;
+ s = openSession();
+ tx = s.beginTransaction();
+ AlphabeticalDirectory m = new AlphabeticalDirectory();
+ m.setName("M");
+ AlphabeticalDirectory v = new AlphabeticalDirectory();
+ v.setName("V");
+ s.persist(m);
+ s.persist(v);
+
+ AddressBook book = new AddressBook();
+ book.setOwner( "Emmanuel" );
+ AddressEntryPk helene = new AddressEntryPk( "Helene", "Michau" );
+ AddressEntry heleneEntry = new AddressEntry();
+ heleneEntry.setBook( book );
+ heleneEntry.setCity( "Levallois" );
+ heleneEntry.setStreet( "Louis Blanc" );
+ heleneEntry.setPerson( helene );
+ heleneEntry.setDirectory( m );
+ AddressEntryPk primeMinister = new AddressEntryPk( "Dominique", "Villepin" );
+ AddressEntry primeMinisterEntry = new AddressEntry();
+ primeMinisterEntry.setBook( book );
+ primeMinisterEntry.setCity( "Paris" );
+ primeMinisterEntry.setStreet( "Hotel Matignon" );
+ primeMinisterEntry.setPerson( primeMinister );
+ primeMinisterEntry.setDirectory( v );
+ book.getEntries().put( helene, heleneEntry );
+ book.getEntries().put( primeMinister, primeMinisterEntry );
+ s.persist( book );
+ s.flush();
+ s.clear();
+
+ book = (AddressBook) s.get( AddressBook.class, book.getId() );
+ assertEquals( 2, book.getEntries().size() );
+ assertEquals( heleneEntry.getCity(), book.getEntries().get( helene ).getCity() );
+ assertEquals( "M", book.getEntries().get( helene ).getDirectory().getName() );
+
+ s.delete( book );
+ tx.rollback();
+ s.close();
+ }
+
public void testComponentSubPropertyMapKey() throws Exception {
Session s;
Transaction tx;
@@ -364,7 +408,8 @@
Painter.class,
Painting.class,
Atmosphere.class,
- Gas.class
+ Gas.class,
+ AlphabeticalDirectory.class
};
}
}
|