ANN-860 implement @OrderColumn

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17192 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2009-07-22 20:40:50 +00:00
parent e093ee46c4
commit eeb136fa71
13 changed files with 108 additions and 62 deletions

View File

@ -76,6 +76,7 @@ import javax.persistence.UniqueConstraint;
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 @@ public final class AnnotationBinder {
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,

View File

@ -25,6 +25,8 @@ package org.hibernate.cfg;
import java.util.Map;
import javax.persistence.OrderColumn;
import org.hibernate.mapping.Join;
/**
@ -82,6 +84,35 @@ public class IndexColumn
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,

View File

@ -268,7 +268,8 @@ public abstract class CollectionBinder {
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();

View File

@ -96,7 +96,7 @@ public class ListBinder extends CollectionBinder {
}
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 @@ public class ListBinder extends CollectionBinder {
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()
);
}

View File

@ -2,19 +2,19 @@ package org.hibernate.test.annotations.any;
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.Table;
import javax.persistence.JoinTable;
import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import org.hibernate.annotations.ManyToAny;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.IndexColumn;
import org.hibernate.annotations.Any;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.ManyToAny;
@Entity
@Table( name = "property_list" )
@ -41,7 +41,7 @@ public class PropertyList<T extends Property> {
joinColumns = @JoinColumn(name = "obj_id"),
inverseJoinColumns = @JoinColumn(name = "property_id")
)
@IndexColumn(name = "prop_index")
@OrderColumn(name = "prop_index")
public List<T> getGeneralProperties() {
return generalProperties;
}

View File

@ -8,6 +8,7 @@ import javax.persistence.GeneratedValue;
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 @@ public class Contest {
}
@OneToMany(cascade = CascadeType.ALL)
@IndexColumn(name = "pos")
@OrderColumn(name = "pos")
public Competitor[] getResults() {
return results;
}
@ -41,7 +42,7 @@ public class Contest {
}
@ElementCollection
@IndexColumn(name = "pos", base=1)
@IndexColumn(name = "pos", base=1) //legacy + base
public Month[] getHeldIn() {
return heldIn;
}

View File

@ -7,7 +7,9 @@ import java.util.Map;
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.FetchType;
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 @@ public class Boy {
joinColumns = @JoinColumn(name = "BoyId")
)
@Column(name = "favoriteNumber", nullable = false)
@IndexColumn(name = "nbr_index")
@OrderColumn(name = "nbr_index")
public int[] getFavoriteNumbers() {
return favoriteNumbers;
}

View File

@ -11,11 +11,10 @@ import javax.persistence.Entity;
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 @@ public class A {
@SequenceGenerator( name = "aSequence", sequenceName = "seq_A" )
private int id;
@ElementCollection
@IndexColumn( name = "ndx" )
@OrderColumn( name = "ndx" )
private List<B> listOfB;
public int getId() {

View File

@ -4,8 +4,7 @@ package org.hibernate.test.annotations.collectionelement.deepcollectionelements;
import java.util.List;
import javax.persistence.Embeddable;
import javax.persistence.OneToMany;
import org.hibernate.annotations.IndexColumn;
import javax.persistence.OrderColumn;
@Embeddable
public class B {
@ -13,7 +12,7 @@ public class B {
//@CollectionOfElements
@OneToMany
@IndexColumn( name = "ndx" )
@OrderColumn( name = "ndx" )
private List<C> listOfC;
public List<C> getListOfC() {

View File

@ -12,13 +12,13 @@ import javax.persistence.FetchType;
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 @@ public class Person implements Serializable {
@OneToMany(cascade=CascadeType.ALL)
@LazyCollection(LazyCollectionOption.EXTRA)
@Fetch(FetchMode.SUBSELECT)
@IndexColumn(name="orderedStayIndex")
@OrderColumn(name="orderedStayIndex")
public List<Stay> getOrderedStay() {
return orderedStay;
}

View File

@ -6,8 +6,7 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.hibernate.annotations.IndexColumn;
import javax.persistence.OrderColumn;
/**
* @author Emmanuel Bernard
@ -33,7 +32,7 @@ public class Drawer {
* @return
*/
@OneToMany
@IndexColumn(name = "from_bottom_position")
@OrderColumn //default name test
public List<Dress> getDresses() {
return dresses;
}

View File

@ -3,17 +3,16 @@ package org.hibernate.test.annotations.indexcoll;
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,31 +24,34 @@ import org.hibernate.test.annotations.TestCase;
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 );
public void testJPA2DefaultIndexColumns() throws Exception {
isDefaultKeyColumnPresent( Drawer.class.getName(), "dresses", "_ORDER" );
}
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;
}
assertTrue( "Could not find " + propertyName + "_KEY", hasDefault);
return hasDefault;
}
private void isNotDefaultKeyColumnPresent(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;
}
assertFalse( "Could not find " + propertyName + "_KEY", 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 {

View File

@ -1,16 +1,15 @@
//$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 org.hibernate.annotations.IndexColumn;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
/**
* @author Emmanuel Bernard
@ -18,7 +17,7 @@ import org.hibernate.annotations.IndexColumn;
@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;