use of org.hibernate.annotations.CollectionId in project hibernate-orm by hibernate.
the class CollectionBinder method getCollectionBinder.
/**
* collection binder factory
*/
public static CollectionBinder getCollectionBinder(String entityName, XProperty property, boolean isIndexed, boolean isHibernateExtensionMapping, MetadataBuildingContext buildingContext) {
final CollectionBinder result;
if (property.isArray()) {
if (property.getElementClass().isPrimitive()) {
result = new PrimitiveArrayBinder();
} else {
result = new ArrayBinder();
}
} else if (property.isCollection()) {
//TODO consider using an XClass
Class returnedClass = property.getCollectionClass();
if (java.util.Set.class.equals(returnedClass)) {
if (property.isAnnotationPresent(CollectionId.class)) {
throw new AnnotationException("Set do not support @CollectionId: " + StringHelper.qualify(entityName, property.getName()));
}
result = new SetBinder(false);
} else if (java.util.SortedSet.class.equals(returnedClass)) {
if (property.isAnnotationPresent(CollectionId.class)) {
throw new AnnotationException("Set do not support @CollectionId: " + StringHelper.qualify(entityName, property.getName()));
}
result = new SetBinder(true);
} else if (java.util.Map.class.equals(returnedClass)) {
if (property.isAnnotationPresent(CollectionId.class)) {
throw new AnnotationException("Map do not support @CollectionId: " + StringHelper.qualify(entityName, property.getName()));
}
result = new MapBinder(false);
} else if (java.util.SortedMap.class.equals(returnedClass)) {
if (property.isAnnotationPresent(CollectionId.class)) {
throw new AnnotationException("Map do not support @CollectionId: " + StringHelper.qualify(entityName, property.getName()));
}
result = new MapBinder(true);
} else if (java.util.Collection.class.equals(returnedClass)) {
if (property.isAnnotationPresent(CollectionId.class)) {
result = new IdBagBinder();
} else {
result = new BagBinder();
}
} else if (java.util.List.class.equals(returnedClass)) {
if (isIndexed) {
if (property.isAnnotationPresent(CollectionId.class)) {
throw new AnnotationException("List do not support @CollectionId and @OrderColumn (or @IndexColumn) at the same time: " + StringHelper.qualify(entityName, property.getName()));
}
result = new ListBinder();
} else if (property.isAnnotationPresent(CollectionId.class)) {
result = new IdBagBinder();
} else {
result = new BagBinder();
}
} else {
throw new AnnotationException(returnedClass.getName() + " collection not yet supported: " + StringHelper.qualify(entityName, property.getName()));
}
} else {
throw new AnnotationException("Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: " + StringHelper.qualify(entityName, property.getName()));
}
result.setIsHibernateExtensionMapping(isHibernateExtensionMapping);
final CollectionType typeAnnotation = property.getAnnotation(CollectionType.class);
if (typeAnnotation != null) {
final String typeName = typeAnnotation.type();
// see if it names a type-def
final TypeDefinition typeDef = buildingContext.getMetadataCollector().getTypeDefinition(typeName);
if (typeDef != null) {
result.explicitType = typeDef.getTypeImplementorClass().getName();
result.explicitTypeParameters.putAll(typeDef.getParameters());
} else {
result.explicitType = typeName;
for (Parameter param : typeAnnotation.parameters()) {
result.explicitTypeParameters.setProperty(param.name(), param.value());
}
}
}
return result;
}
use of org.hibernate.annotations.CollectionId in project hibernate-orm by hibernate.
the class IdBagBinder method bindStarToManySecondPass.
@Override
protected boolean bindStarToManySecondPass(Map persistentClasses, XClass collType, Ejb3JoinColumn[] fkJoinColumns, Ejb3JoinColumn[] keyColumns, Ejb3JoinColumn[] inverseColumns, Ejb3Column[] elementColumns, boolean isEmbedded, XProperty property, boolean unique, TableBinder associationTableBinder, boolean ignoreNotFound, MetadataBuildingContext buildingContext) {
boolean result = super.bindStarToManySecondPass(persistentClasses, collType, fkJoinColumns, keyColumns, inverseColumns, elementColumns, isEmbedded, property, unique, associationTableBinder, ignoreNotFound, getBuildingContext());
CollectionId collectionIdAnn = property.getAnnotation(CollectionId.class);
if (collectionIdAnn != null) {
SimpleValueBinder simpleValue = new SimpleValueBinder();
PropertyData propertyData = new WrappedInferredData(new PropertyInferredData(null, property, //default access should not be useful
null, buildingContext.getBuildingOptions().getReflectionManager()), "id");
Ejb3Column[] idColumns = Ejb3Column.buildColumnFromAnnotation(collectionIdAnn.columns(), null, Nullability.FORCED_NOT_NULL, propertyHolder, propertyData, Collections.EMPTY_MAP, buildingContext);
//we need to make sure all id columns must be not-null.
for (Ejb3Column idColumn : idColumns) {
idColumn.setNullable(false);
}
Table table = collection.getCollectionTable();
simpleValue.setTable(table);
simpleValue.setColumns(idColumns);
Type typeAnn = collectionIdAnn.type();
if (typeAnn != null && !BinderHelper.isEmptyAnnotationValue(typeAnn.type())) {
simpleValue.setExplicitType(typeAnn);
} else {
throw new AnnotationException("@CollectionId is missing type: " + StringHelper.qualify(propertyHolder.getPath(), propertyName));
}
simpleValue.setBuildingContext(getBuildingContext());
SimpleValue id = simpleValue.make();
((IdentifierCollection) collection).setIdentifier(id);
String generator = collectionIdAnn.generator();
String generatorType;
if ("identity".equals(generator) || "assigned".equals(generator) || "sequence".equals(generator) || "native".equals(generator)) {
generatorType = generator;
generator = "";
} else {
generatorType = null;
}
BinderHelper.makeIdGenerator(id, generatorType, generator, getBuildingContext(), localGenerators);
}
return result;
}
Aggregations