use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.
the class BinderHelper method makeIdGenerator.
/**
* apply an id generator to a SimpleValue
*/
public static void makeIdGenerator(SimpleValue id, String generatorType, String generatorName, MetadataBuildingContext buildingContext, Map<String, IdentifierGeneratorDefinition> localGenerators) {
Table table = id.getTable();
table.setIdentifierValue(id);
//generator settings
id.setIdentifierGeneratorStrategy(generatorType);
Properties params = new Properties();
//always settable
params.setProperty(PersistentIdentifierGenerator.TABLE, table.getName());
final String implicitCatalogName = buildingContext.getBuildingOptions().getMappingDefaults().getImplicitCatalogName();
if (implicitCatalogName != null) {
params.put(PersistentIdentifierGenerator.CATALOG, implicitCatalogName);
}
final String implicitSchemaName = buildingContext.getBuildingOptions().getMappingDefaults().getImplicitSchemaName();
if (implicitSchemaName != null) {
params.put(PersistentIdentifierGenerator.SCHEMA, implicitSchemaName);
}
if (id.getColumnSpan() == 1) {
params.setProperty(PersistentIdentifierGenerator.PK, ((org.hibernate.mapping.Column) id.getColumnIterator().next()).getName());
}
// YUCK! but cannot think of a clean way to do this given the string-config based scheme
params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, buildingContext.getObjectNameNormalizer());
if (!isEmptyAnnotationValue(generatorName)) {
//we have a named generator
IdentifierGeneratorDefinition gen = getIdentifierGenerator(generatorName, localGenerators, buildingContext);
if (gen == null) {
throw new AnnotationException("Unknown Id.generator: " + generatorName);
}
//This is quite vague in the spec but a generator could override the generate choice
String identifierGeneratorStrategy = gen.getStrategy();
//yuk! this is a hack not to override 'AUTO' even if generator is set
final boolean avoidOverriding = identifierGeneratorStrategy.equals("identity") || identifierGeneratorStrategy.equals("seqhilo") || identifierGeneratorStrategy.equals(MultipleHiLoPerTableGenerator.class.getName());
if (generatorType == null || !avoidOverriding) {
id.setIdentifierGeneratorStrategy(identifierGeneratorStrategy);
}
//checkIfMatchingGenerator(gen, generatorType, generatorName);
for (Object o : gen.getParameters().entrySet()) {
Map.Entry elt = (Map.Entry) o;
params.setProperty((String) elt.getKey(), (String) elt.getValue());
}
}
if ("assigned".equals(generatorType)) {
id.setNullValue("undefined");
}
id.setIdentifierGeneratorProperties(params);
}
use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.
the class ListBinder method bindIndex.
private void bindIndex(final MetadataBuildingContext buildingContext) {
if (!indexColumn.isImplicit()) {
PropertyHolder valueHolder = PropertyHolderBuilder.buildPropertyHolder(this.collection, StringHelper.qualify(this.collection.getRole(), "key"), null, null, propertyHolder, getBuildingContext());
List list = (List) this.collection;
if (!list.isOneToMany())
indexColumn.forceNotNull();
indexColumn.setPropertyHolder(valueHolder);
SimpleValueBinder value = new SimpleValueBinder();
value.setColumns(new Ejb3Column[] { indexColumn });
value.setExplicitType("integer");
value.setBuildingContext(getBuildingContext());
SimpleValue indexValue = value.make();
indexColumn.linkWithValue(indexValue);
list.setIndex(indexValue);
list.setBaseIndex(indexColumn.getBase());
if (list.isOneToMany() && !list.getKey().isNullable() && !list.isInverse()) {
String entityName = ((OneToMany) list.getElement()).getReferencedEntityName();
PersistentClass referenced = buildingContext.getMetadataCollector().getEntityBinding(entityName);
IndexBackref ib = new IndexBackref();
ib.setName('_' + propertyName + "IndexBackref");
ib.setUpdateable(false);
ib.setSelectable(false);
ib.setCollectionRole(list.getRole());
ib.setEntityName(list.getOwner().getEntityName());
ib.setValue(list.getIndex());
referenced.addProperty(ib);
}
} else {
Collection coll = this.collection;
throw new AnnotationException("List/array has to be annotated with an @OrderColumn (or @IndexColumn): " + coll.getRole());
}
}
use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.
the class QueryBinder method bindQuery.
public static void bindQuery(org.hibernate.annotations.NamedQuery queryAnn, MetadataBuildingContext context) {
if (queryAnn == null) {
return;
}
if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) {
throw new AnnotationException("A named query must have a name when used in class or package level");
}
FlushMode flushMode;
flushMode = getFlushMode(queryAnn.flushMode());
NamedQueryDefinition query = new NamedQueryDefinitionBuilder().setName(queryAnn.name()).setQuery(queryAnn.query()).setCacheable(queryAnn.cacheable()).setCacheRegion(BinderHelper.isEmptyAnnotationValue(queryAnn.cacheRegion()) ? null : queryAnn.cacheRegion()).setTimeout(queryAnn.timeout() < 0 ? null : queryAnn.timeout()).setFetchSize(queryAnn.fetchSize() < 0 ? null : queryAnn.fetchSize()).setFlushMode(flushMode).setCacheMode(getCacheMode(queryAnn.cacheMode())).setReadOnly(queryAnn.readOnly()).setComment(BinderHelper.isEmptyAnnotationValue(queryAnn.comment()) ? null : queryAnn.comment()).setParameterTypes(null).createNamedQueryDefinition();
context.getMetadataCollector().addNamedQuery(query);
if (LOG.isDebugEnabled()) {
LOG.debugf("Binding named query: %s => %s", query.getName(), query.getQueryString());
}
}
use of org.hibernate.AnnotationException 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.AnnotationException in project hibernate-orm by hibernate.
the class EntityBinder method setCache.
public void setCache(Cache cacheAnn) {
if (cacheAnn != null) {
cacheRegion = BinderHelper.isEmptyAnnotationValue(cacheAnn.region()) ? null : cacheAnn.region();
cacheConcurrentStrategy = getCacheConcurrencyStrategy(cacheAnn.usage());
if ("all".equalsIgnoreCase(cacheAnn.include())) {
cacheLazyProperty = true;
} else if ("non-lazy".equalsIgnoreCase(cacheAnn.include())) {
cacheLazyProperty = false;
} else {
throw new AnnotationException("Unknown lazy property annotations: " + cacheAnn.include());
}
} else {
cacheConcurrentStrategy = null;
cacheRegion = null;
cacheLazyProperty = true;
}
}
Aggregations