Author: epbernard
Date: 2009-07-22 16:40:50 -0400 (Wed, 22 Jul 2009)
New Revision: 17192
Modified:
core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java
core/trunk/annotations/src/main/java/org/hibernate/cfg/IndexColumn.java
core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java
core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/ListBinder.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/any/PropertyList.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/array/Contest.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/deepcollectionelements/A.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/deepcollectionelements/B.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/fetch/Person.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/Drawer.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/emops/Race.java
Log:
ANN-860 implement @OrderColumn
Modified: core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java
===================================================================
---
core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java 2009-07-22
13:45:15 UTC (rev 17191)
+++
core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java 2009-07-22
20:40:50 UTC (rev 17192)
@@ -76,6 +76,7 @@
import javax.persistence.MapKeyColumn;
import javax.persistence.MapKeyJoinColumns;
import javax.persistence.MapKeyJoinColumn;
+import javax.persistence.OrderColumn;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
@@ -1473,13 +1474,28 @@
ManyToMany manyToManyAnn = property.getAnnotation( ManyToMany.class );
ElementCollection elementCollectionAnn = property.getAnnotation(
ElementCollection.class );
CollectionOfElements collectionOfElementsAnn = property.getAnnotation(
CollectionOfElements.class ); //legacy hibernate
- org.hibernate.annotations.IndexColumn indexAnn = property.getAnnotation(
- org.hibernate.annotations.IndexColumn.class
- );
- IndexColumn indexColumn = IndexColumn.buildColumnFromAnnotation(
- indexAnn, propertyHolder, inferredData, mappings
- );
+ final IndexColumn indexColumn;
+
+ if ( property.isAnnotationPresent( OrderColumn.class ) ) {
+ indexColumn = IndexColumn.buildColumnFromAnnotation(
+ property.getAnnotation(OrderColumn.class),
+ propertyHolder,
+ inferredData,
+ entityBinder.getSecondaryTables(),
+ mappings
+ );
+ }
+ else {
+ //if @IndexColumn is not there, the generated IndexColumn is an implicit column and
not used.
+ //so we can leave the legacy processing as the default
+ indexColumn = IndexColumn.buildColumnFromAnnotation(
+ property.getAnnotation(org.hibernate.annotations.IndexColumn.class),
+ propertyHolder,
+ inferredData,
+ mappings
+ );
+ }
CollectionBinder collectionBinder = CollectionBinder.getCollectionBinder(
propertyHolder.getEntityName(),
property,
Modified: core/trunk/annotations/src/main/java/org/hibernate/cfg/IndexColumn.java
===================================================================
--- core/trunk/annotations/src/main/java/org/hibernate/cfg/IndexColumn.java 2009-07-22
13:45:15 UTC (rev 17191)
+++ core/trunk/annotations/src/main/java/org/hibernate/cfg/IndexColumn.java 2009-07-22
20:40:50 UTC (rev 17192)
@@ -25,6 +25,8 @@
import java.util.Map;
+import javax.persistence.OrderColumn;
+
import org.hibernate.mapping.Join;
/**
@@ -82,7 +84,36 @@
this.base = base;
}
+ //JPA 2 @OrderColumn processing
public static IndexColumn buildColumnFromAnnotation(
+ OrderColumn ann,
+ PropertyHolder propertyHolder,
+ PropertyData inferredData,
+ Map<String, Join> secondaryTables,
+ ExtendedMappings mappings
+ ) {
+ IndexColumn column;
+ if ( ann != null ) {
+ String sqlType = BinderHelper.isDefault( ann.columnDefinition() ) ? null :
ann.columnDefinition();
+ String name = BinderHelper.isDefault( ann.name() ) ? inferredData.getPropertyName() +
"_ORDER" : ann.name();
+ //TODO move it to a getter based system and remove the constructor
+ column = new IndexColumn(
+ false, sqlType, 0, 0, 0, name, ann.nullable(),
+ false, ann.insertable(), ann.updatable(), ann.table(),
+ secondaryTables, propertyHolder, mappings
+ );
+ }
+ else {
+ column = new IndexColumn(
+ true, null, 0, 0, 0, null, true,
+ false, true, true, null, null, propertyHolder, mappings
+ );
+ }
+ return column;
+ }
+
+ //legacy @IndexColumn processing
+ public static IndexColumn buildColumnFromAnnotation(
org.hibernate.annotations.IndexColumn ann,
PropertyHolder propertyHolder,
PropertyData inferredData,
Modified:
core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java
===================================================================
---
core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java 2009-07-22
13:45:15 UTC (rev 17191)
+++
core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java 2009-07-22
20:40:50 UTC (rev 17192)
@@ -268,7 +268,8 @@
else if ( java.util.List.class.equals( returnedClass ) ) {
if ( isIndexed ) {
if ( property.isAnnotationPresent( CollectionId.class ) ) {
- throw new AnnotationException( "List do not support @CollectionId and
@IndexColumn at the same time: "
+ throw new AnnotationException(
+ "List do not support @CollectionId and @OrderColumn (or @IndexColumn) at the
same time: "
+ StringHelper.qualify( entityName, property.getName() ) );
}
return new ListBinder();
Modified:
core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/ListBinder.java
===================================================================
---
core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/ListBinder.java 2009-07-22
13:45:15 UTC (rev 17191)
+++
core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/ListBinder.java 2009-07-22
20:40:50 UTC (rev 17192)
@@ -96,7 +96,7 @@
}
private void bindIndex(final ExtendedMappings mappings) {
- if ( indexColumn.isImplicit() == false ) {
+ if ( !indexColumn.isImplicit() ) {
PropertyHolder valueHolder = PropertyHolderBuilder.buildPropertyHolder(
this.collection,
StringHelper.qualify( this.collection.getRole(), "key" ),
@@ -130,7 +130,7 @@
else {
Collection coll = this.collection;
throw new AnnotationException(
- "List/array has to be annotated with an @IndexColumn: "
+ "List/array has to be annotated with an @OrderColumn (or @IndexColumn): "
+ coll.getRole()
);
}
Modified:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/any/PropertyList.java
===================================================================
---
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/any/PropertyList.java 2009-07-22
13:45:15 UTC (rev 17191)
+++
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/any/PropertyList.java 2009-07-22
20:40:50 UTC (rev 17192)
@@ -2,19 +2,19 @@
import java.util.ArrayList;
import java.util.List;
+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.OrderColumn;
import javax.persistence.Table;
-import javax.persistence.JoinTable;
-import javax.persistence.Column;
-import javax.persistence.JoinColumn;
-import org.hibernate.annotations.ManyToAny;
+import org.hibernate.annotations.Any;
import org.hibernate.annotations.Cascade;
-import org.hibernate.annotations.IndexColumn;
-import org.hibernate.annotations.Any;
import org.hibernate.annotations.CascadeType;
+import org.hibernate.annotations.ManyToAny;
@Entity
@Table( name = "property_list" )
@@ -41,7 +41,7 @@
joinColumns = @JoinColumn(name = "obj_id"),
inverseJoinColumns = @JoinColumn(name = "property_id")
)
- @IndexColumn(name = "prop_index")
+ @OrderColumn(name = "prop_index")
public List<T> getGeneralProperties() {
return generalProperties;
}
Modified:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/array/Contest.java
===================================================================
---
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/array/Contest.java 2009-07-22
13:45:15 UTC (rev 17191)
+++
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/array/Contest.java 2009-07-22
20:40:50 UTC (rev 17192)
@@ -8,6 +8,7 @@
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.ElementCollection;
+import javax.persistence.OrderColumn;
import org.hibernate.annotations.IndexColumn;
@@ -31,7 +32,7 @@
}
@OneToMany(cascade = CascadeType.ALL)
- @IndexColumn(name = "pos")
+ @OrderColumn(name = "pos")
public Competitor[] getResults() {
return results;
}
@@ -41,7 +42,7 @@
}
@ElementCollection
- @IndexColumn(name = "pos", base=1)
+ @IndexColumn(name = "pos", base=1) //legacy + base
public Month[] getHeldIn() {
return heldIn;
}
Modified:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java
===================================================================
---
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java 2009-07-22
13:45:15 UTC (rev 17191)
+++
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java 2009-07-22
20:40:50 UTC (rev 17192)
@@ -7,7 +7,9 @@
import java.util.Set;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
+import javax.persistence.CollectionTable;
import javax.persistence.Column;
+import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
@@ -15,12 +17,9 @@
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
-import javax.persistence.JoinTable;
-import javax.persistence.ElementCollection;
-import javax.persistence.CollectionTable;
+import javax.persistence.OrderColumn;
import org.hibernate.annotations.CollectionOfElements;
-import org.hibernate.annotations.IndexColumn;
/**
* @author Emmanuel Bernard
@@ -94,7 +93,7 @@
joinColumns = @JoinColumn(name = "BoyId")
)
@Column(name = "favoriteNumber", nullable = false)
- @IndexColumn(name = "nbr_index")
+ @OrderColumn(name = "nbr_index")
public int[] getFavoriteNumbers() {
return favoriteNumbers;
}
Modified:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/deepcollectionelements/A.java
===================================================================
---
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/deepcollectionelements/A.java 2009-07-22
13:45:15 UTC (rev 17191)
+++
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/deepcollectionelements/A.java 2009-07-22
20:40:50 UTC (rev 17192)
@@ -11,11 +11,10 @@
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
+import javax.persistence.OrderColumn;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
-import org.hibernate.annotations.IndexColumn;
-
@Entity
@Table( name = "A" )
public class A {
@@ -24,7 +23,7 @@
@SequenceGenerator( name = "aSequence", sequenceName = "seq_A" )
private int id;
@ElementCollection
- @IndexColumn( name = "ndx" )
+ @OrderColumn( name = "ndx" )
private List<B> listOfB;
public int getId() {
Modified:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/deepcollectionelements/B.java
===================================================================
---
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/deepcollectionelements/B.java 2009-07-22
13:45:15 UTC (rev 17191)
+++
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/deepcollectionelements/B.java 2009-07-22
20:40:50 UTC (rev 17192)
@@ -4,16 +4,15 @@
import java.util.List;
import javax.persistence.Embeddable;
import javax.persistence.OneToMany;
+import javax.persistence.OrderColumn;
-import org.hibernate.annotations.IndexColumn;
-
@Embeddable
public class B {
private String name;
//@CollectionOfElements
@OneToMany
- @IndexColumn( name = "ndx" )
+ @OrderColumn( name = "ndx" )
private List<C> listOfC;
public List<C> getListOfC() {
Modified:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/fetch/Person.java
===================================================================
---
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/fetch/Person.java 2009-07-22
13:45:15 UTC (rev 17191)
+++
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/fetch/Person.java 2009-07-22
20:40:50 UTC (rev 17192)
@@ -12,13 +12,13 @@
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
+import javax.persistence.OrderColumn;
import javax.persistence.Table;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
-import org.hibernate.annotations.IndexColumn;
/**
@@ -118,7 +118,7 @@
@OneToMany(cascade=CascadeType.ALL)
@LazyCollection(LazyCollectionOption.EXTRA)
@Fetch(FetchMode.SUBSELECT)
- @IndexColumn(name="orderedStayIndex")
+ @OrderColumn(name="orderedStayIndex")
public List<Stay> getOrderedStay() {
return orderedStay;
}
Modified:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/Drawer.java
===================================================================
---
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/Drawer.java 2009-07-22
13:45:15 UTC (rev 17191)
+++
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/Drawer.java 2009-07-22
20:40:50 UTC (rev 17192)
@@ -6,9 +6,8 @@
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
+import javax.persistence.OrderColumn;
-import org.hibernate.annotations.IndexColumn;
-
/**
* @author Emmanuel Bernard
*/
@@ -33,7 +32,7 @@
* @return
*/
@OneToMany
- @IndexColumn(name = "from_bottom_position")
+ @OrderColumn //default name test
public List<Dress> getDresses() {
return dresses;
}
Modified:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java
===================================================================
---
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java 2009-07-22
13:45:15 UTC (rev 17191)
+++
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/indexcoll/IndexedCollectionTest.java 2009-07-22
20:40:50 UTC (rev 17192)
@@ -3,17 +3,16 @@
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import java.util.Iterator;
+import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
-import org.hibernate.Hibernate;
-import org.hibernate.mapping.PersistentClass;
+import org.hibernate.dialect.HSQLDialect;
import org.hibernate.mapping.Collection;
import org.hibernate.mapping.Column;
-import org.hibernate.dialect.HSQLDialect;
import org.hibernate.test.annotations.RequiresDialect;
import org.hibernate.test.annotations.TestCase;
@@ -25,33 +24,36 @@
public class IndexedCollectionTest extends TestCase {
public void testJPA2DefaultMapColumns() throws Exception {
- isDefaultKeyColumnPresent( "gasesDef" );
- isDefaultKeyColumnPresent( "gasesPerKeyDef" );
- isNotDefaultKeyColumnPresent( "gasesDefLeg" );
+ isDefaultKeyColumnPresent( Atmosphere.class.getName(), "gasesDef",
"_KEY" );
+ isDefaultKeyColumnPresent( Atmosphere.class.getName(), "gasesPerKeyDef",
"_KEY" );
+ isNotDefaultKeyColumnPresent( Atmosphere.class.getName(), "gasesDefLeg",
"_KEY" );
}
- private void isDefaultKeyColumnPresent(String propertyName) {
- final Collection collection = getCfg().getCollectionMapping( Atmosphere.class.getName()
+ "." + propertyName );
- final Iterator columnIterator = collection.getCollectionTable().getColumnIterator();
- boolean hasDefault = false;
- while ( columnIterator.hasNext() ) {
- Column column = (Column) columnIterator.next();
- if ( (propertyName + "_KEY").equals( column.getName() ) ) hasDefault =
true;
- }
- assertTrue( "Could not find " + propertyName + "_KEY",
hasDefault);
+ public void testJPA2DefaultIndexColumns() throws Exception {
+ isDefaultKeyColumnPresent( Drawer.class.getName(), "dresses",
"_ORDER" );
}
- private void isNotDefaultKeyColumnPresent(String propertyName) {
- final Collection collection = getCfg().getCollectionMapping( Atmosphere.class.getName()
+ "." + propertyName );
+ private void isDefaultKeyColumnPresent(String collectionRole, String propertyName,
String suffix) {
+ assertTrue( "Could not find " + propertyName + suffix,
+ isDefaultColumnPresent(collectionRole, propertyName, suffix) );
+ }
+
+ private boolean isDefaultColumnPresent(String collectionRole, String propertyName,
String suffix) {
+ final Collection collection = getCfg().getCollectionMapping( collectionRole +
"." + propertyName );
final Iterator columnIterator = collection.getCollectionTable().getColumnIterator();
boolean hasDefault = false;
while ( columnIterator.hasNext() ) {
Column column = (Column) columnIterator.next();
- if ( (propertyName + "_KEY").equals( column.getName() ) ) hasDefault =
true;
+ if ( (propertyName + suffix).equals( column.getName() ) ) hasDefault = true;
}
- assertFalse( "Could not find " + propertyName + "_KEY",
hasDefault);
+ return hasDefault;
}
+ private void isNotDefaultKeyColumnPresent(String collectionRole, String propertyName,
String suffix) {
+ assertFalse( "Could not find " + propertyName + suffix,
+ isDefaultColumnPresent(collectionRole, propertyName, suffix) );
+ }
+
public void testFkList() throws Exception {
Wardrobe w = new Wardrobe();
Drawer d1 = new Drawer();
Modified: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/emops/Race.java
===================================================================
---
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/emops/Race.java 2009-07-22
13:45:15 UTC (rev 17191)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/emops/Race.java 2009-07-22
20:40:50 UTC (rev 17192)
@@ -1,24 +1,23 @@
//$Id$
package org.hibernate.ejb.test.emops;
-import java.util.List;
import java.util.ArrayList;
-import javax.persistence.Id;
-import javax.persistence.GeneratedValue;
-import javax.persistence.Entity;
-import javax.persistence.OneToMany;
+import java.util.List;
import javax.persistence.CascadeType;
+import javax.persistence.Entity;
import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import javax.persistence.OrderColumn;
-import org.hibernate.annotations.IndexColumn;
-
/**
* @author Emmanuel Bernard
*/
@Entity
public class Race {
@Id @GeneratedValue public Integer id;
- @IndexColumn( name="index_" ) @OneToMany(cascade = CascadeType.ALL, fetch =
FetchType.EAGER)
+ @OrderColumn( name="index_" ) @OneToMany(cascade = CascadeType.ALL, fetch =
FetchType.EAGER)
@org.hibernate.annotations.Cascade( { org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
public List<Competitor> competitors = new ArrayList<Competitor>();
public String name;