use of org.hibernate.mapping.Component in project hibernate-orm by hibernate.
the class PropertyBinder method bind.
private Property bind(Property prop) {
if (isId) {
final RootClass rootClass = (RootClass) holder.getPersistentClass();
//FIXME this pose a problem as the PK is the class instead of the associated class which is not really compliant with the spec
if (isXToMany || entityBinder.wrapIdsInEmbeddedComponents()) {
Component identifier = (Component) rootClass.getIdentifier();
if (identifier == null) {
identifier = AnnotationBinder.createComponent(holder, new PropertyPreloadedData(null, null, null), true, false, buildingContext);
rootClass.setIdentifier(identifier);
identifier.setNullValue("undefined");
rootClass.setEmbeddedIdentifier(true);
rootClass.setIdentifierMapper(identifier);
}
//FIXME is it good enough?
identifier.addProperty(prop);
} else {
rootClass.setIdentifier((KeyValue) getValue());
if (embedded) {
rootClass.setEmbeddedIdentifier(true);
} else {
rootClass.setIdentifierProperty(prop);
final org.hibernate.mapping.MappedSuperclass superclass = BinderHelper.getMappedSuperclassOrNull(declaringClass, inheritanceStatePerClass, buildingContext);
if (superclass != null) {
superclass.setDeclaredIdentifierProperty(prop);
} else {
//we know the property is on the actual entity
rootClass.setDeclaredIdentifierProperty(prop);
}
}
}
} else {
holder.addProperty(prop, columns, declaringClass);
}
return prop;
}
use of org.hibernate.mapping.Component in project hibernate-orm by hibernate.
the class CopyIdentifierComponentSecondPass method doSecondPass.
@SuppressWarnings({ "unchecked" })
public void doSecondPass(Map persistentClasses) throws MappingException {
PersistentClass referencedPersistentClass = (PersistentClass) persistentClasses.get(referencedEntityName);
// TODO better error names
if (referencedPersistentClass == null) {
throw new AnnotationException("Unknown entity name: " + referencedEntityName);
}
if (!(referencedPersistentClass.getIdentifier() instanceof Component)) {
throw new AssertionFailure("Unexpected identifier type on the referenced entity when mapping a @MapsId: " + referencedEntityName);
}
Component referencedComponent = (Component) referencedPersistentClass.getIdentifier();
Iterator<Property> properties = referencedComponent.getPropertyIterator();
//prepare column name structure
boolean isExplicitReference = true;
Map<String, Ejb3JoinColumn> columnByReferencedName = new HashMap<String, Ejb3JoinColumn>(joinColumns.length);
for (Ejb3JoinColumn joinColumn : joinColumns) {
final String referencedColumnName = joinColumn.getReferencedColumn();
if (referencedColumnName == null || BinderHelper.isEmptyAnnotationValue(referencedColumnName)) {
break;
}
//JPA 2 requires referencedColumnNames to be case insensitive
columnByReferencedName.put(referencedColumnName.toLowerCase(Locale.ROOT), joinColumn);
}
//try default column orientation
AtomicInteger index = new AtomicInteger(0);
if (columnByReferencedName.isEmpty()) {
isExplicitReference = false;
for (Ejb3JoinColumn joinColumn : joinColumns) {
columnByReferencedName.put("" + index.get(), joinColumn);
index.getAndIncrement();
}
index.set(0);
}
while (properties.hasNext()) {
Property referencedProperty = properties.next();
if (referencedProperty.isComposite()) {
Property property = createComponentProperty(referencedPersistentClass, isExplicitReference, columnByReferencedName, index, referencedProperty);
component.addProperty(property);
} else {
Property property = createSimpleProperty(referencedPersistentClass, isExplicitReference, columnByReferencedName, index, referencedProperty);
component.addProperty(property);
}
}
}
use of org.hibernate.mapping.Component in project hibernate-orm by hibernate.
the class CopyIdentifierComponentSecondPass method createComponentProperty.
private Property createComponentProperty(PersistentClass referencedPersistentClass, boolean isExplicitReference, Map<String, Ejb3JoinColumn> columnByReferencedName, AtomicInteger index, Property referencedProperty) {
Property property = new Property();
property.setName(referencedProperty.getName());
//FIXME set optional?
//property.setOptional( property.isOptional() );
property.setPersistentClass(component.getOwner());
property.setPropertyAccessorName(referencedProperty.getPropertyAccessorName());
Component value = new Component(buildingContext.getMetadataCollector(), component.getOwner());
property.setValue(value);
final Component referencedValue = (Component) referencedProperty.getValue();
value.setTypeName(referencedValue.getTypeName());
value.setTypeParameters(referencedValue.getTypeParameters());
value.setComponentClassName(referencedValue.getComponentClassName());
Iterator<Property> propertyIterator = referencedValue.getPropertyIterator();
while (propertyIterator.hasNext()) {
Property referencedComponentProperty = propertyIterator.next();
if (referencedComponentProperty.isComposite()) {
Property componentProperty = createComponentProperty(referencedValue.getOwner(), isExplicitReference, columnByReferencedName, index, referencedComponentProperty);
value.addProperty(componentProperty);
} else {
Property componentProperty = createSimpleProperty(referencedValue.getOwner(), isExplicitReference, columnByReferencedName, index, referencedComponentProperty);
value.addProperty(componentProperty);
}
}
return property;
}
use of org.hibernate.mapping.Component in project hibernate-orm by hibernate.
the class ClassPropertyHolder method addProperty.
public void addProperty(Property prop, XClass declaringClass) {
if (prop.getValue() instanceof Component) {
//TODO handle quote and non quote table comparison
String tableName = prop.getValue().getTable().getName();
if (getJoinsPerRealTableName().containsKey(tableName)) {
final Join join = getJoinsPerRealTableName().get(tableName);
addPropertyToJoin(prop, declaringClass, join);
} else {
addPropertyToPersistentClass(prop, declaringClass);
}
} else {
addPropertyToPersistentClass(prop, declaringClass);
}
}
use of org.hibernate.mapping.Component in project hibernate-orm by hibernate.
the class IndexOrUniqueKeySecondPass method doSecondPass.
@Override
public void doSecondPass(Map persistentClasses) throws MappingException {
if (columns != null) {
for (int i = 0; i < columns.length; i++) {
addConstraintToColumn(columns[i]);
}
}
if (column != null) {
this.table = column.getTable();
PersistentClass persistentClass = (PersistentClass) persistentClasses.get(column.getPropertyHolder().getEntityName());
Property property = persistentClass.getProperty(column.getPropertyName());
if (property.getValue() instanceof Component) {
Component component = (Component) property.getValue();
List<Column> columns = new ArrayList<>();
component.getColumnIterator().forEachRemaining(selectable -> {
if (selectable instanceof Column) {
columns.add((Column) selectable);
}
});
addConstraintToColumns(columns);
} else {
addConstraintToColumn(buildingContext.getMetadataCollector().getLogicalColumnName(table, column.getMappingColumn().getQuotedName()));
}
}
}
Aggregations