use of org.hibernate.mapping.IdGenerator in project hibernate-orm by hibernate.
the class AnnotationBinder method bindIdClass.
private static void bindIdClass(String generatorType, String generatorName, PropertyData inferredData, PropertyData baseInferredData, Ejb3Column[] columns, PropertyHolder propertyHolder, boolean isComposite, AccessType propertyAccessor, EntityBinder entityBinder, boolean isEmbedded, boolean isIdentifierMapper, MetadataBuildingContext buildingContext, Map<XClass, InheritanceState> inheritanceStatePerClass) {
/*
* Fill simple value and property since and Id is a property
*/
PersistentClass persistentClass = propertyHolder.getPersistentClass();
if (!(persistentClass instanceof RootClass)) {
throw new AnnotationException("Unable to define/override @Id(s) on a subclass: " + propertyHolder.getEntityName());
}
RootClass rootClass = (RootClass) persistentClass;
String persistentClassName = rootClass.getClassName();
SimpleValue id;
final String propertyName = inferredData.getPropertyName();
HashMap<String, IdGenerator> localGenerators = new HashMap<String, IdGenerator>();
if (isComposite) {
id = fillComponent(propertyHolder, inferredData, baseInferredData, propertyAccessor, false, entityBinder, isEmbedded, isIdentifierMapper, false, buildingContext, inheritanceStatePerClass);
Component componentId = (Component) id;
componentId.setKey(true);
if (rootClass.getIdentifier() != null) {
throw new AnnotationException(componentId.getComponentClassName() + " must not have @Id properties when used as an @EmbeddedId");
}
if (componentId.getPropertySpan() == 0) {
throw new AnnotationException(componentId.getComponentClassName() + " has no persistent id property");
}
//tuplizers
XProperty property = inferredData.getProperty();
setupComponentTuplizer(property, componentId);
} else {
for (Ejb3Column column : columns) {
//this is an id
column.forceNotNull();
}
SimpleValueBinder value = new SimpleValueBinder();
value.setPropertyName(propertyName);
value.setReturnedClassName(inferredData.getTypeName());
value.setColumns(columns);
value.setPersistentClassName(persistentClassName);
value.setBuildingContext(buildingContext);
value.setType(inferredData.getProperty(), inferredData.getClassOrElement(), persistentClassName, null);
value.setAccessType(propertyAccessor);
id = value.make();
}
rootClass.setIdentifier(id);
BinderHelper.makeIdGenerator(id, generatorType, generatorName, buildingContext, Collections.<String, IdentifierGeneratorDefinition>emptyMap());
if (isEmbedded) {
rootClass.setEmbeddedIdentifier(inferredData.getPropertyClass() == null);
} else {
PropertyBinder binder = new PropertyBinder();
binder.setName(propertyName);
binder.setValue(id);
binder.setAccessType(inferredData.getDefaultAccess());
binder.setProperty(inferredData.getProperty());
Property prop = binder.makeProperty();
rootClass.setIdentifierProperty(prop);
//if the id property is on a superclass, update the metamodel
final org.hibernate.mapping.MappedSuperclass superclass = BinderHelper.getMappedSuperclassOrNull(inferredData.getDeclaringClass(), inheritanceStatePerClass, buildingContext);
if (superclass != null) {
superclass.setDeclaredIdentifierProperty(prop);
} else {
//we know the property is on the actual entity
rootClass.setDeclaredIdentifierProperty(prop);
}
}
}
Aggregations