use of org.hibernate.MappingException in project hibernate-orm by hibernate.
the class QueryHintDefinition method getLockMode.
public LockMode getLockMode(String query) {
String hitName = QueryHints.NATIVE_LOCKMODE;
String value = (String) hintsMap.get(hitName);
if (value == null) {
return null;
}
try {
return LockMode.fromExternalForm(value);
} catch (MappingException e) {
throw new AnnotationException("Unknown LockMode in hint: " + query + ":" + hitName, e);
}
}
use of org.hibernate.MappingException in project hibernate-orm by hibernate.
the class SimpleValueBinder method fillSimpleValue.
public void fillSimpleValue() {
LOG.debugf("Starting fillSimpleValue for %s", propertyName);
if (attributeConverterDescriptor != null) {
if (!BinderHelper.isEmptyAnnotationValue(explicitType)) {
throw new AnnotationException(String.format("AttributeConverter and explicit Type cannot be applied to same attribute [%s.%s];" + "remove @Type or specify @Convert(disableConversion = true)", persistentClassName, propertyName));
}
LOG.debugf("Applying JPA AttributeConverter [%s] to [%s:%s]", attributeConverterDescriptor, persistentClassName, propertyName);
simpleValue.setJpaAttributeConverterDescriptor(attributeConverterDescriptor);
} else {
String type;
TypeDefinition typeDef;
if (!BinderHelper.isEmptyAnnotationValue(explicitType)) {
type = explicitType;
typeDef = buildingContext.getMetadataCollector().getTypeDefinition(type);
} else {
// try implicit type
TypeDefinition implicitTypeDef = buildingContext.getMetadataCollector().getTypeDefinition(returnedClassName);
if (implicitTypeDef != null) {
typeDef = implicitTypeDef;
type = returnedClassName;
} else {
typeDef = buildingContext.getMetadataCollector().getTypeDefinition(defaultType);
type = defaultType;
}
}
if (typeDef != null) {
type = typeDef.getTypeImplementorClass().getName();
simpleValue.setTypeParameters(typeDef.getParametersAsProperties());
}
if (typeParameters != null && typeParameters.size() != 0) {
//explicit type params takes precedence over type def params
simpleValue.setTypeParameters(typeParameters);
}
simpleValue.setTypeName(type);
}
if (persistentClassName != null || attributeConverterDescriptor != null) {
try {
simpleValue.setTypeUsingReflection(persistentClassName, propertyName);
} catch (Exception e) {
throw new MappingException(String.format(Locale.ROOT, "Unable to determine basic type mapping via reflection [%s -> %s]", persistentClassName, propertyName), e);
}
}
if (!simpleValue.isTypeSpecified() && isVersion()) {
simpleValue.setTypeName("integer");
}
// HHH-5205
if (timeStampVersionType != null) {
simpleValue.setTypeName(timeStampVersionType);
}
if (simpleValue.getTypeName() != null && simpleValue.getTypeName().length() > 0 && simpleValue.getMetadata().getTypeResolver().basic(simpleValue.getTypeName()) == null) {
try {
Class typeClass = buildingContext.getClassLoaderAccess().classForName(simpleValue.getTypeName());
if (typeClass != null && DynamicParameterizedType.class.isAssignableFrom(typeClass)) {
Properties parameters = simpleValue.getTypeParameters();
if (parameters == null) {
parameters = new Properties();
}
parameters.put(DynamicParameterizedType.IS_DYNAMIC, Boolean.toString(true));
parameters.put(DynamicParameterizedType.RETURNED_CLASS, returnedClassName);
parameters.put(DynamicParameterizedType.IS_PRIMARY_KEY, Boolean.toString(key));
parameters.put(DynamicParameterizedType.ENTITY, persistentClassName);
parameters.put(DynamicParameterizedType.XPROPERTY, xproperty);
parameters.put(DynamicParameterizedType.PROPERTY, xproperty.getName());
parameters.put(DynamicParameterizedType.ACCESS_TYPE, accessType.getType());
simpleValue.setTypeParameters(parameters);
}
} catch (ClassLoadingException e) {
throw new MappingException("Could not determine type for: " + simpleValue.getTypeName(), e);
}
}
}
use of org.hibernate.MappingException in project hibernate-orm by hibernate.
the class OneToOneSecondPass method doSecondPass.
//TODO refactor this code, there is a lot of duplication in this method
public void doSecondPass(Map persistentClasses) throws MappingException {
org.hibernate.mapping.OneToOne value = new org.hibernate.mapping.OneToOne(buildingContext.getMetadataCollector(), propertyHolder.getTable(), propertyHolder.getPersistentClass());
final String propertyName = inferredData.getPropertyName();
value.setPropertyName(propertyName);
String referencedEntityName = ToOneBinder.getReferenceEntityName(inferredData, targetEntity, buildingContext);
value.setReferencedEntityName(referencedEntityName);
AnnotationBinder.defineFetchingStrategy(value, inferredData.getProperty());
//value.setFetchMode( fetchMode );
value.setCascadeDeleteEnabled(cascadeOnDelete);
if (!optional)
value.setConstrained(true);
value.setForeignKeyType(value.isConstrained() ? ForeignKeyDirection.FROM_PARENT : ForeignKeyDirection.TO_PARENT);
PropertyBinder binder = new PropertyBinder();
binder.setName(propertyName);
binder.setValue(value);
binder.setCascade(cascadeStrategy);
binder.setAccessType(inferredData.getDefaultAccess());
Property prop = binder.makeProperty();
if (ignoreNotFound) {
prop.setOptional(true);
}
if (BinderHelper.isEmptyAnnotationValue(mappedBy)) {
/*
* we need to check if the columns are in the right order
* if not, then we need to create a many to one and formula
* but actually, since entities linked by a one to one need
* to share the same composite id class, this cannot happen in hibernate
*/
boolean rightOrder = true;
if (rightOrder) {
String path = StringHelper.qualify(propertyHolder.getPath(), propertyName);
final ToOneFkSecondPass secondPass = new ToOneFkSecondPass(value, joinColumns, //cannot have nullabe and unique on certain DBs
!optional, propertyHolder.getEntityOwnerClassName(), path, buildingContext);
secondPass.doSecondPass(persistentClasses);
//no column associated since its a one to one
propertyHolder.addProperty(prop, inferredData.getDeclaringClass());
} else {
//this is a many to one with Formula
}
} else {
PersistentClass otherSide = (PersistentClass) persistentClasses.get(value.getReferencedEntityName());
Property otherSideProperty;
try {
if (otherSide == null) {
throw new MappingException("Unable to find entity: " + value.getReferencedEntityName());
}
otherSideProperty = BinderHelper.findPropertyByName(otherSide, mappedBy);
} catch (MappingException e) {
throw new AnnotationException("Unknown mappedBy in: " + StringHelper.qualify(ownerEntity, ownerProperty) + ", referenced property unknown: " + StringHelper.qualify(value.getReferencedEntityName(), mappedBy));
}
if (otherSideProperty == null) {
throw new AnnotationException("Unknown mappedBy in: " + StringHelper.qualify(ownerEntity, ownerProperty) + ", referenced property unknown: " + StringHelper.qualify(value.getReferencedEntityName(), mappedBy));
}
if (otherSideProperty.getValue() instanceof OneToOne) {
propertyHolder.addProperty(prop, inferredData.getDeclaringClass());
} else if (otherSideProperty.getValue() instanceof ManyToOne) {
Iterator it = otherSide.getJoinIterator();
Join otherSideJoin = null;
while (it.hasNext()) {
Join otherSideJoinValue = (Join) it.next();
if (otherSideJoinValue.containsProperty(otherSideProperty)) {
otherSideJoin = otherSideJoinValue;
break;
}
}
if (otherSideJoin != null) {
//@OneToOne @JoinTable
Join mappedByJoin = buildJoinFromMappedBySide((PersistentClass) persistentClasses.get(ownerEntity), otherSideProperty, otherSideJoin);
ManyToOne manyToOne = new ManyToOne(buildingContext.getMetadataCollector(), mappedByJoin.getTable());
//FIXME use ignore not found here
manyToOne.setIgnoreNotFound(ignoreNotFound);
manyToOne.setCascadeDeleteEnabled(value.isCascadeDeleteEnabled());
manyToOne.setFetchMode(value.getFetchMode());
manyToOne.setLazy(value.isLazy());
manyToOne.setReferencedEntityName(value.getReferencedEntityName());
manyToOne.setUnwrapProxy(value.isUnwrapProxy());
prop.setValue(manyToOne);
Iterator otherSideJoinKeyColumns = otherSideJoin.getKey().getColumnIterator();
while (otherSideJoinKeyColumns.hasNext()) {
Column column = (Column) otherSideJoinKeyColumns.next();
Column copy = new Column();
copy.setLength(column.getLength());
copy.setScale(column.getScale());
copy.setValue(manyToOne);
copy.setName(column.getQuotedName());
copy.setNullable(column.isNullable());
copy.setPrecision(column.getPrecision());
copy.setUnique(column.isUnique());
copy.setSqlType(column.getSqlType());
copy.setCheckConstraint(column.getCheckConstraint());
copy.setComment(column.getComment());
copy.setDefaultValue(column.getDefaultValue());
manyToOne.addColumn(copy);
}
mappedByJoin.addProperty(prop);
} else {
propertyHolder.addProperty(prop, inferredData.getDeclaringClass());
}
value.setReferencedPropertyName(mappedBy);
// HHH-6813
// Foo: @Id long id, @OneToOne(mappedBy="foo") Bar bar
// Bar: @Id @OneToOne Foo foo
boolean referencesDerivedId = false;
try {
referencesDerivedId = otherSide.getIdentifier() instanceof Component && ((Component) otherSide.getIdentifier()).getProperty(mappedBy) != null;
} catch (MappingException e) {
// ignore
}
boolean referenceToPrimaryKey = referencesDerivedId || mappedBy == null;
value.setReferenceToPrimaryKey(referenceToPrimaryKey);
// loop of attempts to resolve identifiers.
if (referencesDerivedId) {
((ManyToOne) otherSideProperty.getValue()).setReferenceToPrimaryKey(false);
}
String propertyRef = value.getReferencedPropertyName();
if (propertyRef != null) {
buildingContext.getMetadataCollector().addUniquePropertyReference(value.getReferencedEntityName(), propertyRef);
}
} else {
throw new AnnotationException("Referenced property not a (One|Many)ToOne: " + StringHelper.qualify(otherSide.getEntityName(), mappedBy) + " in mappedBy of " + StringHelper.qualify(ownerEntity, ownerProperty));
}
}
final ForeignKey fk = inferredData.getProperty().getAnnotation(ForeignKey.class);
if (fk != null && !BinderHelper.isEmptyAnnotationValue(fk.name())) {
value.setForeignKeyName(fk.name());
} else {
final javax.persistence.ForeignKey jpaFk = inferredData.getProperty().getAnnotation(javax.persistence.ForeignKey.class);
if (jpaFk != null) {
if (jpaFk.value() == ConstraintMode.NO_CONSTRAINT) {
value.setForeignKeyName("none");
} else {
value.setForeignKeyName(StringHelper.nullIfEmpty(jpaFk.name()));
value.setForeignKeyDefinition(StringHelper.nullIfEmpty(jpaFk.foreignKeyDefinition()));
}
}
}
}
use of org.hibernate.MappingException in project hibernate-orm by hibernate.
the class AbstractEmptinessExpression method getQueryableCollection.
protected QueryableCollection getQueryableCollection(String entityName, String propertyName, SessionFactoryImplementor factory) throws HibernateException {
final PropertyMapping ownerMapping = (PropertyMapping) factory.getEntityPersister(entityName);
final Type type = ownerMapping.toType(propertyName);
if (!type.isCollectionType()) {
throw new MappingException("Property path [" + entityName + "." + propertyName + "] does not reference a collection");
}
final String role = ((CollectionType) type).getRole();
try {
return (QueryableCollection) factory.getCollectionPersister(role);
} catch (ClassCastException cce) {
throw new QueryException("collection role is not queryable: " + role);
} catch (Exception e) {
throw new QueryException("collection role not found: " + role);
}
}
use of org.hibernate.MappingException in project hibernate-orm by hibernate.
the class AccessMappingTest method testExplicitPropertyAccessAnnotationsOnField.
@Test
public void testExplicitPropertyAccessAnnotationsOnField() throws Exception {
Configuration cfg = new Configuration();
cfg.addAnnotatedClass(Course4.class);
cfg.addAnnotatedClass(Student.class);
SessionFactory sf = null;
try {
sf = cfg.buildSessionFactory(serviceRegistry);
fail("@Id and @OneToMany are not placed consistently in test entities. SessionFactory creation should fail.");
} catch (MappingException e) {
// success
} finally {
if (sf != null) {
sf.close();
}
}
}
Aggregations