use of org.hibernate.annotations.Parameter 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.Parameter in project hibernate-orm by hibernate.
the class AnnotationBinder method bindTypeDef.
private static void bindTypeDef(TypeDef defAnn, MetadataBuildingContext context) {
Properties params = new Properties();
for (Parameter param : defAnn.parameters()) {
params.setProperty(param.name(), param.value());
}
if (BinderHelper.isEmptyAnnotationValue(defAnn.name()) && defAnn.defaultForType().equals(void.class)) {
throw new AnnotationException("Either name or defaultForType (or both) attribute should be set in TypeDef having typeClass " + defAnn.typeClass().getName());
}
final String typeBindMessageF = "Binding type definition: %s";
if (!BinderHelper.isEmptyAnnotationValue(defAnn.name())) {
if (LOG.isDebugEnabled()) {
LOG.debugf(typeBindMessageF, defAnn.name());
}
context.getMetadataCollector().addTypeDefinition(new TypeDefinition(defAnn.name(), defAnn.typeClass(), null, params));
}
if (!defAnn.defaultForType().equals(void.class)) {
if (LOG.isDebugEnabled()) {
LOG.debugf(typeBindMessageF, defAnn.defaultForType().getName());
}
context.getMetadataCollector().addTypeDefinition(new TypeDefinition(defAnn.defaultForType().getName(), defAnn.typeClass(), new String[] { defAnn.defaultForType().getName() }, params));
}
}
use of org.hibernate.annotations.Parameter in project hibernate-orm by hibernate.
the class AnnotationBinder method buildIdGenerator.
private static IdentifierGeneratorDefinition buildIdGenerator(java.lang.annotation.Annotation generatorAnn, MetadataBuildingContext context) {
if (generatorAnn == null) {
return null;
}
IdentifierGeneratorDefinition.Builder definitionBuilder = new IdentifierGeneratorDefinition.Builder();
if (context.getMappingDefaults().getImplicitSchemaName() != null) {
definitionBuilder.addParam(PersistentIdentifierGenerator.SCHEMA, context.getMappingDefaults().getImplicitSchemaName());
}
if (context.getMappingDefaults().getImplicitCatalogName() != null) {
definitionBuilder.addParam(PersistentIdentifierGenerator.CATALOG, context.getMappingDefaults().getImplicitCatalogName());
}
if (generatorAnn instanceof TableGenerator) {
context.getBuildingOptions().getIdGenerationTypeInterpreter().interpretTableGenerator((TableGenerator) generatorAnn, definitionBuilder);
if (LOG.isTraceEnabled()) {
LOG.tracev("Add table generator with name: {0}", definitionBuilder.getName());
}
} else if (generatorAnn instanceof SequenceGenerator) {
context.getBuildingOptions().getIdGenerationTypeInterpreter().interpretSequenceGenerator((SequenceGenerator) generatorAnn, definitionBuilder);
if (LOG.isTraceEnabled()) {
LOG.tracev("Add sequence generator with name: {0}", definitionBuilder.getName());
}
} else if (generatorAnn instanceof GenericGenerator) {
GenericGenerator genGen = (GenericGenerator) generatorAnn;
definitionBuilder.setName(genGen.name());
definitionBuilder.setStrategy(genGen.strategy());
Parameter[] params = genGen.parameters();
for (Parameter parameter : params) {
definitionBuilder.addParam(parameter.name(), parameter.value());
}
if (LOG.isTraceEnabled()) {
LOG.tracev("Add generic generator with name: {0}", definitionBuilder.getName());
}
} else {
throw new AssertionFailure("Unknown Generator annotation: " + generatorAnn);
}
return definitionBuilder.build();
}
use of org.hibernate.annotations.Parameter in project hibernate-orm by hibernate.
the class SimpleValueBinder method setExplicitType.
// FIXME raise an assertion failure if setResolvedTypeMapping(String) and setResolvedTypeMapping(Type) are use at the same time
public void setExplicitType(Type typeAnn) {
if (typeAnn != null) {
explicitType = typeAnn.type();
typeParameters.clear();
for (Parameter param : typeAnn.parameters()) {
typeParameters.setProperty(param.name(), param.value());
}
}
}
use of org.hibernate.annotations.Parameter in project BroadleafCommerce by BroadleafCommerce.
the class SequenceGeneratorCorruptionDetection method patchSequenceGeneratorInconsistencies.
protected void patchSequenceGeneratorInconsistencies(EntityManager em, Session session) {
SessionFactory sessionFactory = session.getSessionFactory();
for (Object item : sessionFactory.getAllClassMetadata().values()) {
ClassMetadata metadata = (ClassMetadata) item;
String idProperty = metadata.getIdentifierPropertyName();
Class<?> mappedClass = metadata.getMappedClass();
Field idField;
try {
idField = mappedClass.getDeclaredField(idProperty);
} catch (NoSuchFieldException e) {
continue;
}
idField.setAccessible(true);
GenericGenerator genericAnnot = idField.getAnnotation(GenericGenerator.class);
TableGenerator tableAnnot = idField.getAnnotation(TableGenerator.class);
String segmentValue = null;
String tableName = null;
String segmentColumnName = null;
String valueColumnName = null;
if (genericAnnot != null && genericAnnot.strategy().equals(IdOverrideTableGenerator.class.getName())) {
// This is a BLC style ID generator
for (Parameter param : genericAnnot.parameters()) {
if (param.name().equals("segment_value")) {
segmentValue = param.value();
}
if (param.name().equals("table_name")) {
tableName = param.value();
}
if (param.name().equals("segment_column_name")) {
segmentColumnName = param.value();
}
if (param.name().equals("value_column_name")) {
valueColumnName = param.value();
}
}
// to redefine them. If they aren't defined in the annotation, glean them from the defaults
if (StringUtils.isBlank(tableName)) {
tableName = IdOverrideTableGenerator.DEFAULT_TABLE_NAME;
}
if (StringUtils.isBlank(segmentColumnName)) {
segmentColumnName = IdOverrideTableGenerator.DEFAULT_SEGMENT_COLUMN_NAME;
}
if (StringUtils.isBlank(valueColumnName)) {
valueColumnName = IdOverrideTableGenerator.DEFAULT_VALUE_COLUMN_NAME;
}
} else if (tableAnnot != null) {
// This is a traditional Hibernate generator
segmentValue = tableAnnot.pkColumnValue();
tableName = tableAnnot.table();
segmentColumnName = tableAnnot.pkColumnName();
valueColumnName = tableAnnot.valueColumnName();
}
if (!StringUtils.isEmpty(segmentValue) && !StringUtils.isEmpty(tableName) && !StringUtils.isEmpty(segmentColumnName) && !StringUtils.isEmpty(valueColumnName)) {
StringBuilder sb2 = new StringBuilder();
sb2.append("select ");
sb2.append(valueColumnName);
sb2.append(" from ");
if (!tableName.contains(".") && !StringUtils.isEmpty(defaultSchemaSequenceGenerator)) {
sb2.append(defaultSchemaSequenceGenerator);
sb2.append(".");
}
sb2.append(tableName);
sb2.append(" where ");
sb2.append(segmentColumnName);
sb2.append(" = '");
sb2.append(segmentValue);
sb2.append("'");
Long maxSequenceId = 0l;
boolean sequenceEntryExists = false;
List results2 = em.createNativeQuery(sb2.toString()).getResultList();
if (CollectionUtils.isNotEmpty(results2) && results2.get(0) != null) {
maxSequenceId = ((Number) results2.get(0)).longValue();
sequenceEntryExists = true;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Detecting id sequence state between " + mappedClass.getName() + " and " + segmentValue + " in " + tableName);
}
StringBuilder sb = new StringBuilder();
sb.append("select max(");
sb.append(idField.getName());
sb.append(") from ");
sb.append(mappedClass.getName());
sb.append(" entity");
List results;
BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
if (context == null) {
context = new BroadleafRequestContext();
BroadleafRequestContext.setBroadleafRequestContext(context);
}
try {
context.setInternalIgnoreFilters(true);
results = em.createQuery(sb.toString()).getResultList();
} finally {
context.setInternalIgnoreFilters(false);
}
if (CollectionUtils.isNotEmpty(results) && results.get(0) != null) {
LOG.debug(String.format("Checking for sequence corruption on entity %s", segmentValue));
Long maxEntityId = BLCNumberUtils.toLong(results.get(0));
if (maxEntityId > maxSequenceId) {
LOG.error(String.format("The sequence value for %s in %s was found as %d (or an entry did not exist) but the actual max sequence in" + " %s's table was found as %d", segmentValue, tableName, maxSequenceId, mappedClass.getName(), maxEntityId));
if (automaticallyCorrectInconsistencies) {
long newMaxId = maxEntityId + 10;
if (sequenceEntryExists) {
String log = String.format("Correcting sequences for entity %s. Updating the sequence value" + " to %d", mappedClass.getName(), newMaxId);
LOG.warn(log);
StringBuilder updateQuery = new StringBuilder();
updateQuery.append("update ");
if (!tableName.contains(".") && !StringUtils.isEmpty(defaultSchemaSequenceGenerator)) {
sb2.append(defaultSchemaSequenceGenerator);
sb2.append(".");
}
updateQuery.append(tableName);
updateQuery.append(" set ");
updateQuery.append(valueColumnName);
updateQuery.append(" = ");
updateQuery.append(String.valueOf(newMaxId));
updateQuery.append(" where ");
updateQuery.append(segmentColumnName);
updateQuery.append(" = '");
updateQuery.append(segmentValue);
updateQuery.append("'");
int response = em.createNativeQuery(updateQuery.toString()).executeUpdate();
if (response <= 0) {
throw new RuntimeException("Unable to update " + tableName + " with the sequence generator id for " + segmentValue);
}
} else {
String log = String.format("Correcting sequences for entity %s. Did not find an entry in" + " %s, inserting the new sequence value as %d", mappedClass.getName(), tableName, newMaxId);
LOG.warn(log);
StringBuilder insertQuery = new StringBuilder();
insertQuery.append("insert into ");
if (!tableName.contains(".") && !StringUtils.isEmpty(defaultSchemaSequenceGenerator)) {
sb2.append(defaultSchemaSequenceGenerator);
sb2.append(".");
}
insertQuery.append(tableName);
insertQuery.append(" (" + segmentColumnName + "," + valueColumnName + ")");
insertQuery.append("values ('" + segmentValue + "','" + String.valueOf(newMaxId) + "')");
int response = em.createNativeQuery(insertQuery.toString()).executeUpdate();
if (response <= 0) {
throw new RuntimeException("Unable to update " + tableName + " with the sequence generator id for " + segmentValue);
}
}
} else {
String reason = "A data inconsistency has been detected between the " + tableName + " table and one or more entity tables for which it manages current max primary key values.\n" + "The inconsistency was detected between the managed class (" + mappedClass.getName() + ") and the identifier (" + segmentValue + ") in " + tableName + ". Broadleaf\n" + "has stopped startup of the application in order to allow you to resolve the issue and avoid possible data corruption. If you wish to disable this detection, you may\n" + "set the 'detect.sequence.generator.inconsistencies' property to false in your application's common.properties or common-shared.properties. If you would like for this component\n" + "to autocorrect these problems by setting the sequence generator value to a value greater than the max entity id, then set the 'auto.correct.sequence.generator.inconsistencies'\n" + "property to true in your application's common.properties or common-shared.properties. If you would like to provide a default schema to be used to qualify table names used in the\n" + "queries for this detection, set the 'default.schema.sequence.generator' property in your application's common.properties or common-shared.properties. Also, if you are upgrading\n" + "from 1.6 or below, please refer to http://docs.broadleafcommerce.org/current/1.6-to-2.0-Migration.html for important information regarding migrating your SEQUENCE_GENERATOR table.";
LOG.error("Broadleaf Commerce failed to start", new RuntimeException(reason));
System.exit(1);
}
}
}
}
}
}
Aggregations