1 package org.hibernate.cfg.annotations; 2 3 import java.util.Map ; 4 5 import org.hibernate.AnnotationException; 6 import org.hibernate.FetchMode; 7 import org.hibernate.MappingException; 8 import org.hibernate.annotations.OrderBy; 9 import org.hibernate.annotations.Sort; 10 import org.hibernate.cfg.Ejb3JoinColumn; 11 import org.hibernate.cfg.ExtendedMappings; 12 import org.hibernate.cfg.PropertyHolder; 13 import org.hibernate.cfg.PropertyHolderBuilder; 14 import org.hibernate.cfg.SecondPass; 15 import org.hibernate.cfg.Ejb3Column; 16 import org.hibernate.mapping.Collection; 17 import org.hibernate.mapping.IndexBackref; 18 import org.hibernate.mapping.IndexedCollection; 19 import org.hibernate.mapping.List; 20 import org.hibernate.mapping.OneToMany; 21 import org.hibernate.mapping.PersistentClass; 22 import org.hibernate.mapping.SimpleValue; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 32 public class ListBinder extends CollectionBinder { 33 private static Log log = LogFactory.getLog(ListBinder.class); 34 35 public ListBinder() {} 36 37 protected Collection createCollection(PersistentClass persistentClass) { 38 return new org.hibernate.mapping.List(persistentClass); 39 } 40 41 public void setSqlOrderBy(OrderBy orderByAnn) { 42 if (orderByAnn != null) log.warn("@OrderBy not allowed for a indexed collection, annotation ignored."); 43 } 44 45 public void setSort(Sort sortAnn) { 46 if (sortAnn != null) log.warn("@Sort not allowed for a indexed collection, annotation ignored."); 47 } 48 49 public SecondPass getSecondPass(final ExtendedMappings mappings, 50 final Ejb3JoinColumn[] keyColumns, 51 final Ejb3JoinColumn[] inverseColumns, 52 final String collType, 53 final FetchMode fetchMode, 54 final boolean unique) { 55 if (inverseColumns != null) { 56 return new SecondPass(mappings, ListBinder.this.collection) { 57 public void secondPass(Map persistentClasses, Map inheritedMetas) 58 throws MappingException { 59 bindManyToManySecondPass(ListBinder.this.collection, 60 persistentClasses, 61 keyColumns, 62 inverseColumns, 63 collType, 64 fetchMode, 65 unique, 66 cascadeDeleteEnabled, (ExtendedMappings) mappings); 67 bindIndex( mappings ); 68 } 69 }; 70 } 71 else { 72 return new SecondPass(mappings, collection) { 73 public void secondPass(Map persistentClasses, Map inheritedMetas) 74 throws MappingException { 75 bindCollectionSecondPass(ListBinder.this.collection, 76 persistentClasses, 77 keyColumns, 78 cascadeDeleteEnabled, hqlOrderBy, (ExtendedMappings) mappings); 79 bindIndex( mappings ); 80 } 81 }; 82 } 83 } 84 85 private void bindIndex(final ExtendedMappings mappings) { 86 if ( indexColumn.isImplicit() == false ) { 87 PropertyHolder valueHolder = PropertyHolderBuilder.buildPropertyHolder( 88 this.collection, 89 IndexedCollection.DEFAULT_INDEX_COLUMN_NAME 90 ); 91 List list = (List) this.collection; 92 if ( ! list.isOneToMany() ) indexColumn.forceNotNull(); 93 indexColumn.setPropertyHolder( valueHolder ); 94 SimpleValueBinder value = new SimpleValueBinder(); 95 value.setColumns(new Ejb3Column[] { indexColumn } ); 96 value.setExplicitType("integer"); 97 value.setMappings(mappings); 98 SimpleValue indexValue = value.make(); 99 indexColumn.linkWithValue( indexValue ); 100 list.setIndex(indexValue); 101 list.setBaseIndex( indexColumn.getBase() ); 102 if ( list.isOneToMany() && !list.getKey().isNullable() && !list.isInverse() ) { 103 String entityName = ( (OneToMany) list.getElement() ).getReferencedEntityName(); 104 PersistentClass referenced = mappings.getClass( entityName ); 105 IndexBackref ib = new IndexBackref(); 106 ib.setName( '_' + propertyName + "IndexBackref" ); 107 ib.setUpdateable( false ); 108 ib.setSelectable( false ); 109 ib.setCollectionRole( list.getRole() ); 110 ib.setEntityName( list.getOwner().getEntityName() ); 111 ib.setValue( list.getIndex() ); 112 referenced.addProperty( ib ); 113 } 114 } 115 else { 116 Collection coll = this.collection; 117 throw new AnnotationException( 118 "List/array has to be annotated with an @IndexColumn: " 119 + coll.getRole() 120 ); 121 } 122 } 123 } 124 | Popular Tags |