use of javax.persistence.Column in project hibernate-orm by hibernate.
the class AnnotationBinder method addProperty.
private static int addProperty(PropertyContainer propertyContainer, XProperty property, List<PropertyData> inFlightPropertyDataList, MetadataBuildingContext context) {
// and if so, skip it..
for (PropertyData propertyData : inFlightPropertyDataList) {
if (propertyData.getPropertyName().equals(property.getName())) {
// EARLY EXIT!!!
return 0;
}
}
final XClass declaringClass = propertyContainer.getDeclaringClass();
final XClass entity = propertyContainer.getEntityAtStake();
int idPropertyCounter = 0;
PropertyData propertyAnnotatedElement = new PropertyInferredData(declaringClass, property, propertyContainer.getClassLevelAccessType().getType(), context.getBootstrapContext().getReflectionManager());
/*
* put element annotated by @Id in front
* since it has to be parsed before any association by Hibernate
*/
final XAnnotatedElement element = propertyAnnotatedElement.getProperty();
if (element.isAnnotationPresent(Id.class) || element.isAnnotationPresent(EmbeddedId.class)) {
inFlightPropertyDataList.add(0, propertyAnnotatedElement);
/**
* The property must be put in hibernate.properties as it's a system wide property. Fixable?
* TODO support true/false/default on the property instead of present / not present
* TODO is @Column mandatory?
* TODO add method support
*/
if (context.getBuildingOptions().isSpecjProprietarySyntaxEnabled()) {
if (element.isAnnotationPresent(Id.class) && element.isAnnotationPresent(Column.class)) {
String columnName = element.getAnnotation(Column.class).name();
for (XProperty prop : declaringClass.getDeclaredProperties(AccessType.FIELD.getType())) {
if (!prop.isAnnotationPresent(MapsId.class)) {
/**
* The detection of a configured individual JoinColumn differs between Annotation
* and XML configuration processing.
*/
boolean isRequiredAnnotationPresent = false;
JoinColumns groupAnnotation = prop.getAnnotation(JoinColumns.class);
if ((prop.isAnnotationPresent(JoinColumn.class) && prop.getAnnotation(JoinColumn.class).name().equals(columnName))) {
isRequiredAnnotationPresent = true;
} else if (prop.isAnnotationPresent(JoinColumns.class)) {
for (JoinColumn columnAnnotation : groupAnnotation.value()) {
if (columnName.equals(columnAnnotation.name())) {
isRequiredAnnotationPresent = true;
break;
}
}
}
if (isRequiredAnnotationPresent) {
// create a PropertyData fpr the specJ property holding the mapping
PropertyData specJPropertyData = new PropertyInferredData(declaringClass, // same dec
prop, // the actual @XToOne property
propertyContainer.getClassLevelAccessType().getType(), // TODO we should get the right accessor but the same as id would do
context.getBootstrapContext().getReflectionManager());
context.getMetadataCollector().addPropertyAnnotatedWithMapsIdSpecj(entity, specJPropertyData, element.toString());
}
}
}
}
}
if (element.isAnnotationPresent(ManyToOne.class) || element.isAnnotationPresent(OneToOne.class)) {
context.getMetadataCollector().addToOneAndIdProperty(entity, propertyAnnotatedElement);
}
idPropertyCounter++;
} else {
inFlightPropertyDataList.add(propertyAnnotatedElement);
}
if (element.isAnnotationPresent(MapsId.class)) {
context.getMetadataCollector().addPropertyAnnotatedWithMapsId(entity, propertyAnnotatedElement);
}
return idPropertyCounter;
}
use of javax.persistence.Column in project hibernate-orm by hibernate.
the class AnnotationBinder method bindManyToOne.
private static void bindManyToOne(String cascadeStrategy, Ejb3JoinColumn[] columns, boolean optional, boolean ignoreNotFound, boolean cascadeOnDelete, XClass targetEntity, PropertyHolder propertyHolder, PropertyData inferredData, boolean unique, boolean isIdentifierMapper, boolean inSecondPass, PropertyBinder propertyBinder, MetadataBuildingContext context) {
// All FK columns should be in the same table
org.hibernate.mapping.ManyToOne value = new org.hibernate.mapping.ManyToOne(context, columns[0].getTable());
// This is a @OneToOne mapped to a physical o.h.mapping.ManyToOne
if (unique) {
value.markAsLogicalOneToOne();
}
value.setReferencedEntityName(ToOneBinder.getReferenceEntityName(inferredData, targetEntity, context));
final XProperty property = inferredData.getProperty();
defineFetchingStrategy(value, property);
// value.setFetchMode( fetchMode );
value.setIgnoreNotFound(ignoreNotFound);
value.setCascadeDeleteEnabled(cascadeOnDelete);
// value.setLazy( fetchMode != FetchMode.JOIN );
if (!optional) {
for (Ejb3JoinColumn column : columns) {
column.setNullable(false);
}
}
if (property.isAnnotationPresent(MapsId.class)) {
// read only
for (Ejb3JoinColumn column : columns) {
column.setInsertable(false);
column.setUpdatable(false);
}
}
final JoinColumn joinColumn = property.getAnnotation(JoinColumn.class);
final JoinColumns joinColumns = property.getAnnotation(JoinColumns.class);
// Make sure that JPA1 key-many-to-one columns are read only tooj
boolean hasSpecjManyToOne = false;
if (context.getBuildingOptions().isSpecjProprietarySyntaxEnabled()) {
String columnName = "";
for (XProperty prop : inferredData.getDeclaringClass().getDeclaredProperties(AccessType.FIELD.getType())) {
if (prop.isAnnotationPresent(Id.class) && prop.isAnnotationPresent(Column.class)) {
columnName = prop.getAnnotation(Column.class).name();
}
if (property.isAnnotationPresent(ManyToOne.class) && joinColumn != null && !BinderHelper.isEmptyAnnotationValue(joinColumn.name()) && joinColumn.name().equals(columnName) && !property.isAnnotationPresent(MapsId.class)) {
hasSpecjManyToOne = true;
for (Ejb3JoinColumn column : columns) {
column.setInsertable(false);
column.setUpdatable(false);
}
}
}
}
value.setTypeName(inferredData.getClassOrElementName());
final String propertyName = inferredData.getPropertyName();
value.setTypeUsingReflection(propertyHolder.getClassName(), propertyName);
if ((joinColumn != null && joinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT) || (joinColumns != null && joinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT)) {
// not ideal...
value.setForeignKeyName("none");
} else {
final ForeignKey fk = property.getAnnotation(ForeignKey.class);
if (fk != null && StringHelper.isNotEmpty(fk.name())) {
value.setForeignKeyName(fk.name());
} else {
final javax.persistence.ForeignKey fkOverride = propertyHolder.getOverriddenForeignKey(StringHelper.qualify(propertyHolder.getPath(), propertyName));
if (fkOverride != null && fkOverride.value() == ConstraintMode.NO_CONSTRAINT) {
value.setForeignKeyName("none");
} else if (fkOverride != null) {
value.setForeignKeyName(StringHelper.nullIfEmpty(fkOverride.name()));
value.setForeignKeyDefinition(StringHelper.nullIfEmpty(fkOverride.foreignKeyDefinition()));
} else if (joinColumns != null) {
value.setForeignKeyName(StringHelper.nullIfEmpty(joinColumns.foreignKey().name()));
value.setForeignKeyDefinition(StringHelper.nullIfEmpty(joinColumns.foreignKey().foreignKeyDefinition()));
} else if (joinColumn != null) {
value.setForeignKeyName(StringHelper.nullIfEmpty(joinColumn.foreignKey().name()));
value.setForeignKeyDefinition(StringHelper.nullIfEmpty(joinColumn.foreignKey().foreignKeyDefinition()));
}
}
}
String path = propertyHolder.getPath() + "." + propertyName;
FkSecondPass secondPass = new ToOneFkSecondPass(value, columns, // cannot have nullable and unique on certain DBs like Derby
!optional && unique, propertyHolder.getEntityOwnerClassName(), path, context);
if (inSecondPass) {
secondPass.doSecondPass(context.getMetadataCollector().getEntityBindingMap());
} else {
context.getMetadataCollector().addSecondPass(secondPass);
}
Ejb3Column.checkPropertyConsistency(columns, propertyHolder.getEntityName() + "." + propertyName);
// PropertyBinder binder = new PropertyBinder();
propertyBinder.setName(propertyName);
propertyBinder.setValue(value);
// binder.setCascade(cascadeStrategy);
if (isIdentifierMapper) {
propertyBinder.setInsertable(false);
propertyBinder.setUpdatable(false);
} else if (hasSpecjManyToOne) {
propertyBinder.setInsertable(false);
propertyBinder.setUpdatable(false);
} else {
propertyBinder.setInsertable(columns[0].isInsertable());
propertyBinder.setUpdatable(columns[0].isUpdatable());
}
propertyBinder.setColumns(columns);
propertyBinder.setAccessType(inferredData.getDefaultAccess());
propertyBinder.setCascade(cascadeStrategy);
propertyBinder.setProperty(property);
propertyBinder.setXToMany(true);
propertyBinder.makePropertyAndBind();
}
use of javax.persistence.Column in project hibernate-orm by hibernate.
the class ColumnsBuilder method extractMetadata.
public ColumnsBuilder extractMetadata() {
columns = null;
joinColumns = buildExplicitJoinColumns(property, inferredData);
if (property.isAnnotationPresent(Column.class) || property.isAnnotationPresent(Formula.class)) {
Column ann = property.getAnnotation(Column.class);
Formula formulaAnn = property.getAnnotation(Formula.class);
columns = Ejb3Column.buildColumnFromAnnotation(new Column[] { ann }, formulaAnn, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
} else if (property.isAnnotationPresent(Columns.class)) {
Columns anns = property.getAnnotation(Columns.class);
columns = Ejb3Column.buildColumnFromAnnotation(anns.columns(), null, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
}
// set default values if needed
if (joinColumns == null && (property.isAnnotationPresent(ManyToOne.class) || property.isAnnotationPresent(OneToOne.class))) {
joinColumns = buildDefaultJoinColumnsForXToOne(property, inferredData);
} else if (joinColumns == null && (property.isAnnotationPresent(OneToMany.class) || property.isAnnotationPresent(ElementCollection.class))) {
OneToMany oneToMany = property.getAnnotation(OneToMany.class);
String mappedBy = oneToMany != null ? oneToMany.mappedBy() : "";
joinColumns = Ejb3JoinColumn.buildJoinColumns(null, mappedBy, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), buildingContext);
} else if (joinColumns == null && property.isAnnotationPresent(org.hibernate.annotations.Any.class)) {
throw new AnnotationException("@Any requires an explicit @JoinColumn(s): " + BinderHelper.getPath(propertyHolder, inferredData));
}
if (columns == null && !property.isAnnotationPresent(ManyToMany.class)) {
// useful for collection of embedded elements
columns = Ejb3Column.buildColumnFromAnnotation(null, null, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
}
if (nullability == Nullability.FORCED_NOT_NULL) {
// force columns to not null
for (Ejb3Column col : columns) {
col.forceNotNull();
}
}
return this;
}
use of javax.persistence.Column in project cosmic by MissionCriticalCloud.
the class Filter method addOrderBy.
public void addOrderBy(Class<?> clazz, final String field, final boolean ascending) {
if (field == null) {
return;
}
final Field f;
final Pair<Class<?>, Field> pair = ReflectUtil.getAnyField(clazz, field);
assert (pair != null) : "Can't find field " + field + " in " + clazz.getName();
clazz = pair.first();
f = pair.second();
final Column column = f.getAnnotation(Column.class);
final String name = column != null ? column.name() : field;
final StringBuilder order = new StringBuilder();
if (column == null || column.table() == null || column.table().length() == 0) {
order.append(DbUtil.getTableName(clazz));
} else {
order.append(column.table());
}
order.append(".").append(name).append(ascending ? " ASC " : " DESC ");
if (_orderBy == null) {
_orderBy = order.insert(0, " ORDER BY ").toString();
} else {
_orderBy = order.insert(0, _orderBy + ", ").toString();
}
}
use of javax.persistence.Column in project cassandra-driver-mapping by valchkou.
the class EntityTypeParser method getColumnName.
/**
* by default the field name is the column name.
*
* @Column annotation will override defaults
*/
private static String getColumnName(Field f) {
String columnName = null;
Annotation columnA = f.getAnnotation(Column.class);
if (columnA instanceof Column) {
columnName = ((Column) columnA).name();
}
if (columnName == null || columnName.length() < 1) {
columnName = f.getName();
}
return columnName;
}
Aggregations