private boolean determineDeleteInBatchSupported(final Class<?> genericType) { final MutableBoolean deleteInBatchSupported = new MutableBoolean(true); Reflections.doWithFields(genericType, new FieldCallback() { @Override public void doWith(final Field field) { if (!deleteInBatchSupported.getValue()) { return; } else if (Reflections.getAnnotation(field, ElementCollection.class) != null) { //element collections are mapped as separate tables, thus the values would cause a foreign key constraint violation deleteInBatchSupported.setValue(false); } else if (Reflections.getAnnotation(field, Embedded.class) != null) { //check embedded types for the same constraints if (!determineDeleteInBatchSupported(field.getType())) { deleteInBatchSupported.setValue(false); } } } }); return deleteInBatchSupported.getValue(); }
@PrePersist public void setAuditCreatedBy(Object entity) throws Exception { if (entity.getClass().isAnnotationPresent(Entity.class)) { Field field = getSingleField(entity.getClass(), "auditable"); field.setAccessible(true); if (field.isAnnotationPresent(Embedded.class)) { Object auditable = field.get(entity); if (auditable == null) { field.set(entity, new AdminAuditable()); auditable = field.get(entity); } Field temporalCreatedField = auditable.getClass().getDeclaredField("dateCreated"); Field temporalUpdatedField = auditable.getClass().getDeclaredField("dateUpdated"); Field agentField = auditable.getClass().getDeclaredField("createdBy"); setAuditValueTemporal(temporalCreatedField, auditable); setAuditValueTemporal(temporalUpdatedField, auditable); setAuditValueAgent(agentField, auditable); } } }
@PreUpdate public void setAuditUpdatedBy(Object entity) throws Exception { if (entity.getClass().isAnnotationPresent(Entity.class)) { Field field = getSingleField(entity.getClass(), "auditable"); field.setAccessible(true); if (field.isAnnotationPresent(Embedded.class)) { Object auditable = field.get(entity); if (auditable == null) { field.set(entity, new AdminAuditable()); auditable = field.get(entity); } Field temporalField = auditable.getClass().getDeclaredField("dateUpdated"); Field agentField = auditable.getClass().getDeclaredField("updatedBy"); setAuditValueTemporal(temporalField, auditable); setAuditValueAgent(agentField, auditable); } } }
@PrePersist public void setAuditCreatedBy(Object entity) throws Exception { if (entity.getClass().isAnnotationPresent(Entity.class)) { Field field = getSingleField(entity.getClass(), "auditable"); field.setAccessible(true); if (field.isAnnotationPresent(Embedded.class)) { Object auditable = field.get(entity); if (auditable == null) { field.set(entity, new Auditable()); auditable = field.get(entity); } Field temporalField = auditable.getClass().getDeclaredField("dateCreated"); Field agentField = auditable.getClass().getDeclaredField("createdBy"); setAuditValueTemporal(temporalField, auditable); setAuditValueAgent(agentField, auditable); } } }
@PreUpdate public void setAuditUpdatedBy(Object entity) throws Exception { if (entity.getClass().isAnnotationPresent(Entity.class)) { Field field = getSingleField(entity.getClass(), "auditable"); field.setAccessible(true); if (field.isAnnotationPresent(Embedded.class)) { Object auditable = field.get(entity); if (auditable == null) { field.set(entity, new Auditable()); auditable = field.get(entity); } Field temporalField = auditable.getClass().getDeclaredField("dateUpdated"); Field agentField = auditable.getClass().getDeclaredField("updatedBy"); setAuditValueTemporal(temporalField, auditable); setAuditValueAgent(agentField, auditable); } } }
@PrePersist public void setAuditCreatedBy(Object entity) throws Exception { if (entity.getClass().isAnnotationPresent(Entity.class)) { Field field = getSingleField(entity.getClass(), getAuditableFieldName()); field.setAccessible(true); if (field.isAnnotationPresent(Embedded.class)) { Object auditable = field.get(entity); if (auditable == null) { field.set(entity, new AdminAuditable()); auditable = field.get(entity); } Field temporalCreatedField = auditable.getClass().getDeclaredField("dateCreated"); Field temporalUpdatedField = auditable.getClass().getDeclaredField("dateUpdated"); Field agentField = auditable.getClass().getDeclaredField("createdBy"); setAuditValueTemporal(temporalCreatedField, auditable); setAuditValueTemporal(temporalUpdatedField, auditable); setAuditValueAgent(agentField, auditable); } } }
@PreUpdate public void setAuditUpdatedBy(Object entity) throws Exception { if (entity.getClass().isAnnotationPresent(Entity.class)) { Field field = getSingleField(entity.getClass(), getAuditableFieldName()); field.setAccessible(true); if (field.isAnnotationPresent(Embedded.class)) { Object auditable = field.get(entity); if (auditable == null) { field.set(entity, new AdminAuditable()); auditable = field.get(entity); } Field temporalField = auditable.getClass().getDeclaredField("dateUpdated"); Field agentField = auditable.getClass().getDeclaredField("updatedBy"); setAuditValueTemporal(temporalField, auditable); setAuditValueAgent(agentField, auditable); } } }
/** * Builds the property for the given attribute. * * @param attribute * the attribute to inspect * @param columnMetadata * the attached (or overriden) column metadata * @param override * the AssociationOverride found for this attribute * @return the property that represents the attribute or {@code null} if not persistent */ <X> Property<X, ?> buildProperty(final AttributeAccessor attribute, final Column columnMetadata, final AssociationOverride override) { if (attribute.isPersistent()) { if (CollectionProperty.isCollectionProperty(attribute)) { return new CollectionProperty<>(this, attribute, override); } else if (MapProperty.isMapProperty(attribute)) { return new MapProperty<>(this, attribute, override); } else if (EntityProperty.isEntityProperty(attribute)) { return new EntityProperty<>(this.context, getTable(), attribute, override); } else if (attribute.isAnnotationPresent(Embedded.class)) { return new EmbeddedProperty<>(this, attribute); } else if (attribute.isAnnotationPresent(Version.class)) { return new VersionProperty<>(this.context, this.table, attribute, columnMetadata); } else { return new PrimitiveProperty<>(this.context, this.table, attribute, columnMetadata); } } return null; }
@SuppressWarnings("unchecked") private void resovleEmbed(List<Map> fields, List<Map> complexFields, Field field) { if (field.isAnnotationPresent(Embedded.class)) { for (Field f : field.getType().getDeclaredFields()) { if ((field.getName().equals("status")) || field.getName().equals("serialVersionUID")) { continue; } if (resovleGenerAnno(field)) { continue; } if (isSimpleType(f.getType())) { fields.add(parseField(f)); } } complexFields.remove(complexFields.size() - 1); } }
public static Field findField(final Class<?> clazz, final String columnName) { for (final Field field : clazz.getDeclaredFields()) { if (field.getAnnotation(Embedded.class) != null || field.getAnnotation(EmbeddedId.class) != null) { findField(field.getClass(), columnName); } else { if (columnName.equals(DbUtil.getColumnName(field))) { return field; } } } return null; }
private void setTimestamps(Field[] fields, Object entity) throws Exception { Calendar cal = null; for (Field field : fields) { Class<?> type = field.getType(); Temporal temporalAnnotation = field.getAnnotation(Temporal.class); if (temporalAnnotation != null) { if (field.isAnnotationPresent(Column.class)) { field.setAccessible(true); try { if (TemporalType.TIMESTAMP.equals(temporalAnnotation.value()) && (field.isAnnotationPresent(AutoPopulate.class))) { if (field.get(entity) == null || field.getAnnotation(AutoPopulate.class).autoUpdateValue()) { if (type.isAssignableFrom(Date.class)) { if (cal == null) { cal = SystemTime.asCalendar(); } field.set(entity, cal.getTime()); } else if (type.isAssignableFrom(Calendar.class)) { if (cal == null) { cal = SystemTime.asCalendar(); } field.set(entity, cal); } } } } finally { field.setAccessible(false); } } } else if (field.isAnnotationPresent(Embedded.class)) { field.setAccessible(true); try { // Call recursively setTimestamps(field.getType().getDeclaredFields(), field.get(entity)); } finally { field.setAccessible(false); } } } }
@Embedded @AttributeOverrides({ @AttributeOverride(name = "learner", column = @Column(name = "duration_learner")), @AttributeOverride(name = "eqOracle", column = @Column(name = "duration_eqOracle")) }) public DetailedStatistics getDuration() { return duration; }
@Embedded @AttributeOverrides({ @AttributeOverride(name = "learner", column = @Column(name = "mqs_learner")), @AttributeOverride(name = "eqOracle", column = @Column(name = "mqs_eqOracle")) }) public DetailedStatistics getMqsUsed() { return mqsUsed; }
@Embedded @AttributeOverrides({ @AttributeOverride(name = "learner", column = @Column(name = "symbolsUsed_learner")), @AttributeOverride(name = "eqOracle", column = @Column(name = "symbolsUsed_eqOracle")) }) public DetailedStatistics getSymbolsUsed() { return symbolsUsed; }
@Embedded @AttributeOverrides({ @AttributeOverride(name= RouteLocation.COLUMN_LAT, column = @Column(name = "sLat")), @AttributeOverride(name= RouteLocation.COLUMN_LNG, column = @Column(name = "sLng")) }) public RouteLocation getStartLocation() { return startLocation; }
@Embedded @AttributeOverrides({ @AttributeOverride(name= RouteLocation.COLUMN_LAT, column = @Column(name = "eLat")), @AttributeOverride(name= RouteLocation.COLUMN_LNG, column = @Column(name = "eLng")) }) public RouteLocation getDestinationLocation() { return destinationLocation; }
/** * Adds an extended property to the BeanMapping. * * @param props * @param name * @param type * @param columnMapping * @return */ public static <B> Property<B, Object> initExtendedProperty(Map<String, Property<B, Object>> props, String name, Class<B> type, String columnMapping) { PropertyDescriptor pd = BeanMappingReflectionUtils.getPropertyDescriptor(type, name); if (!isPropertyReadableAndWritable(pd)) { return null; } List<Annotation> ans = BeanMappingReflectionUtils.readAnnotations(type, pd.getName()); Column column = BeanMappingReflectionUtils.getAnnotation(ans, Column.class); ReflectionProperty<B, Object> prop = new ReflectionProperty<B, Object>(name, (Class<Object>) pd.getPropertyType(), inferColumn(columnMapping != null ? columnMapping : name, column), pd.getWriteMethod(), pd.getReadMethod()); if (column != null) { prop.readOnly(true); } if (BeanMappingReflectionUtils.getAnnotation(ans, Id.class) != null) { prop.setIdProperty(true); } if (useAdditionalConfiguration()) { prop.getConfiguration().setAnnotations(ans); if (Collection.class.isAssignableFrom(pd.getPropertyType())) { prop.getConfiguration().setCollectionTypeArguments( ((ParameterizedType) pd.getReadMethod().getGenericReturnType()).getActualTypeArguments()); } } if (BeanMappingReflectionUtils.getAnnotation(ans, Embedded.class) != null) { props.putAll(getCompositeProperties(prop, ans)); } else { props.put(prop.name(), prop); } return prop; }
public static Field findField(Class<?> clazz, String columnName) { for (Field field : clazz.getDeclaredFields()) { if (field.getAnnotation(Embedded.class) != null || field.getAnnotation(EmbeddedId.class) != null) { findField(field.getClass(), columnName); } else { if (columnName.equals(DbUtil.getColumnName(field))) { return field; } } } return null; }
@Embedded @AttributeOverrides( { @AttributeOverride(name = "breite", column = @Column(name = "GEO_BREITE")), @AttributeOverride(name = "laenge", column = @Column(name = "GEO_LAENGE")) }) public GeoKoordinaten getKoordinaten() { return koordinaten; }
@Embedded @AttributeOverrides({ @AttributeOverride(name = "latitude", column = @Column(name = "latitude", nullable = false, precision = 22, scale = 0)), @AttributeOverride(name = "longitude", column = @Column(name = "longitude", nullable = false, precision = 22, scale = 0)) }) public GeoPoint getPosition() { return position; }
/** * @return CarData of this FileEntry */ @Override @NotNull @Embedded public CarData getCarData() { return carData; }
private CtMethod generateFieldWriter( CtClass managedCtClass, CtField persistentField, AttributeTypeDescriptor typeDescriptor) { final FieldInfo fieldInfo = persistentField.getFieldInfo(); final String fieldName = fieldInfo.getName(); final String writerName = EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + fieldName; final CtMethod writer; try { if ( !enhancementContext.isLazyLoadable( persistentField ) ) { // not lazy-loadable... writer = CtNewMethod.setter( writerName, persistentField ); } else { final String methodBody = typeDescriptor.buildWriteInterceptionBodyFragment( fieldName ); writer = CtNewMethod.make( Modifier.PRIVATE, CtClass.voidType, writerName, new CtClass[] {persistentField.getType()}, null, "{" + methodBody + "}", managedCtClass ); } if ( enhancementContext.doDirtyCheckingInline( managedCtClass ) && !isComposite ) { writer.insertBefore( typeDescriptor.buildInLineDirtyCheckingBodyFragment( persistentField ) ); } if ( isComposite ) { StringBuilder builder = new StringBuilder(); builder.append( " if( " ) .append( EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME ) .append( " != null) " ) .append( EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME ) .append( ".callOwner(\"." ) .append( persistentField.getName() ) .append( "\");" ); writer.insertBefore( builder.toString() ); } //composite types if ( persistentField.getAnnotation( Embedded.class ) != null ) { //make sure to add the CompositeOwner interface if ( !doClassInheritCompositeOwner( managedCtClass ) ) { managedCtClass.addInterface( classPool.get( "org.hibernate.engine.spi.CompositeOwner" ) ); } //if a composite have a embedded field we need to implement the method as well if ( isComposite ) { createTrackChangeCompositeMethod( managedCtClass ); } writer.insertBefore( cleanupPreviousOwner( persistentField ) ); writer.insertAfter( compositeMethodBody( persistentField ) ); } managedCtClass.addMethod( writer ); return writer; } catch (Exception e) { throw new EnhancementException( String.format( "Could not enhance entity class [%s] to add field writer method [%s]", managedCtClass.getName(), writerName ), e ); } }
@Embedded public Jobs getJob() { return job; }
@Embedded public EnderecoEntrega getEnderecoEntrega() { return this.enderecoEntrega; }
@Test public void nullableConstraintsMustBeMutuallyConsistent() { final Stream<Field> failedFields = filterFieldsOfManagedJpaTypes(field -> { if (field.isAnnotationPresent(Version.class) || field.isAnnotationPresent(ElementCollection.class)) { return false; } final boolean isPrimitive = field.getType().isPrimitive(); final boolean notNull = field.isAnnotationPresent(NotNull.class); final boolean notEmpty = field.isAnnotationPresent(NotEmpty.class); final boolean notBlank = field.isAnnotationPresent(NotBlank.class); final boolean notNullOrEmpty = notNull || notEmpty || notBlank; final boolean embedded = field.isAnnotationPresent(Embedded.class); final Column column = field.getAnnotation(Column.class); final ManyToOne manyToOne = field.getAnnotation(ManyToOne.class); final OneToOne oneToOne = field.getAnnotation(OneToOne.class); final JoinColumn joinColumn = field.getAnnotation(JoinColumn.class); if (column != null) { if (manyToOne != null && column.nullable() != manyToOne.optional() || oneToOne != null && column.nullable() != oneToOne.optional() || joinColumn != null && column.nullable() != joinColumn.nullable() || column.nullable() && (notNullOrEmpty || isPrimitive) || !isPrimitive && column.insertable() && !column.nullable() && !notNullOrEmpty) { return true; } } if (joinColumn != null) { if (manyToOne != null && joinColumn.nullable() != manyToOne.optional() || oneToOne != null && joinColumn.nullable() != oneToOne.optional() || joinColumn.nullable() && notNull || joinColumn.insertable() && !joinColumn.nullable() && !notNull) { return true; } } if (manyToOne != null) { if (!isIdField(field) && manyToOne.optional() == notNull) { return true; } } if (oneToOne != null) { if (!isIdField(field) && oneToOne.optional() == notNull) { return true; } } if (notNullOrEmpty && !embedded) { if (column == null && joinColumn == null && manyToOne == null && oneToOne == null) { return true; } } return false; }); assertNoFields(failedFields, "Entity fields should have consistency with regard to:\n" + " (1) Presence of @NotNull, @NotBlank or @NotEmpty\n" + " (2) Values of @Column.nullable, @ManyToOne.optional, @OneToOne.optional and " + "@JoinColumn.nullable\n" + " (3) Whether the type of field is primitive or not\n" + " These fields fail: "); }