private boolean isCascadeAll() { OneToMany onetomany = getAnnotation(OneToMany.class); if( onetomany != null ) { return hasCascadeAll(onetomany.cascade()); } OneToOne onetoone = getAnnotation(OneToOne.class); if( onetoone != null ) { return hasCascadeAll(onetoone.cascade()); } ManyToOne manyToOne = getAnnotation(ManyToOne.class); if( manyToOne != null ) { return hasCascadeAll(manyToOne.cascade()); } // CollectionOfElements is a 'default' cascade all if( getAnnotation(CollectionOfElements.class) != null ) { return true; } return false; }
private String getMappedBy(MetaAttribute attr) { ManyToMany manyManyAnnotation = attr.getAnnotation(ManyToMany.class); OneToMany oneManyAnnotation = attr.getAnnotation(OneToMany.class); OneToOne oneOneAnnotation = attr.getAnnotation(OneToOne.class); String mappedBy = null; if (manyManyAnnotation != null) { mappedBy = manyManyAnnotation.mappedBy(); } if (oneManyAnnotation != null) { mappedBy = oneManyAnnotation.mappedBy(); } if (oneOneAnnotation != null) { mappedBy = oneOneAnnotation.mappedBy(); } if (mappedBy != null && mappedBy.length() == 0) { mappedBy = null; } return mappedBy; }
@Override public Optional<ResourceFieldType> getFieldType(BeanAttributeInformation attributeDesc) { Optional<OneToOne> oneToOne = attributeDesc.getAnnotation(OneToOne.class); Optional<OneToMany> oneToMany = attributeDesc.getAnnotation(OneToMany.class); Optional<ManyToOne> manyToOne = attributeDesc.getAnnotation(ManyToOne.class); Optional<ManyToMany> manyToMany = attributeDesc.getAnnotation(ManyToMany.class); if (oneToOne.isPresent() || oneToMany.isPresent() || manyToOne.isPresent() || manyToMany.isPresent()) { return Optional.of(ResourceFieldType.RELATIONSHIP); } Optional<Id> id = attributeDesc.getAnnotation(Id.class); Optional<EmbeddedId> embeddedId = attributeDesc.getAnnotation(EmbeddedId.class); if (id.isPresent() || embeddedId.isPresent()) { return Optional.of(ResourceFieldType.ID); } return Optional.empty(); }
private static boolean hasAnnotationsOnIdClass(XClass idClass) { // if(idClass.getAnnotation(Embeddable.class) != null) // return true; List<XProperty> properties = idClass.getDeclaredProperties( XClass.ACCESS_FIELD ); for ( XProperty property : properties ) { if ( property.isAnnotationPresent( Column.class ) || property.isAnnotationPresent( OneToMany.class ) || property.isAnnotationPresent( ManyToOne.class ) || property.isAnnotationPresent( Id.class ) || property.isAnnotationPresent( GeneratedValue.class ) || property.isAnnotationPresent( OneToOne.class ) || property.isAnnotationPresent( ManyToMany.class ) ) { return true; } } List<XMethod> methods = idClass.getDeclaredMethods(); for ( XMethod method : methods ) { if ( method.isAnnotationPresent( Column.class ) || method.isAnnotationPresent( OneToMany.class ) || method.isAnnotationPresent( ManyToOne.class ) || method.isAnnotationPresent( Id.class ) || method.isAnnotationPresent( GeneratedValue.class ) || method.isAnnotationPresent( OneToOne.class ) || method.isAnnotationPresent( ManyToMany.class ) ) { return true; } } return false; }
/** * In JPA, @ManyToOne and @OneToOne associations are fetched eagerly by * default. That will, in most situations, cause performance problems. * Because of that, the default behavior shall be changed to lazy fetching * which this test enforces. On some rare occasions, the developer may want * to apply eager fetching and for these cases @EagerFetch annotation is * available for indicating that the intention is purposeful and should be * passed by this test. */ @Test public void nonInverseToOneAssociationsMustBeLazilyFetched() { final Stream<Field> failedFields = filterFieldsOfManagedJpaTypes(field -> { final OneToOne oneToOne = field.getAnnotation(OneToOne.class); final ManyToOne manyToOne = field.getAnnotation(ManyToOne.class); return !isIdField(field) && !field.isAnnotationPresent(EagerFetch.class) && (manyToOne != null && manyToOne.fetch() == FetchType.EAGER || oneToOne != null && "".equals(oneToOne.mappedBy()) && oneToOne.fetch() == FetchType.EAGER); }); assertNoFields(failedFields, "These entity fields should either have \"fetch = FetchType.LAZY\" parameter set on @ManyToOne/" + "@OneToOne annotation or alternatively be annotated with @EagerFetch: "); }
/** * * Append the entity attribute name to the JPQL query statement. * * @param jpqlStatement * @param jpaEntity * @param attribute * @throws java.lang.SecurityException * @throws NoSuchFieldException * @throws ClassNotFoundException */ private void appendEntityAttributeName(StringBuilder jpqlStatement, JpaEntity jpaEntity, EntityAttribute attribute, AtomicInteger aliasUnicityKey, AtomicInteger joinAdditionsOffset) throws NoSuchFieldException, java.lang.SecurityException, ClassNotFoundException { Class<?> entityClass = Class.forName(jpaEntity.getClassName()); Field field = entityClass.getDeclaredField(attribute.getAttributeName()); boolean isJointRelationship = field.getAnnotation(OneToOne.class) != null || field.getAnnotation(OneToMany.class) != null || field.getAnnotation(ManyToOne.class) != null || field.getAnnotation(ManyToMany.class) != null; if(isJointRelationship) { StringBuilder joinFragment = new StringBuilder(); Queue<String> objectTreePath = extractDotSeparatedPathFragments(attribute.getPossibleValueLabelAttributePath()); completeJoinFragment(joinFragment, entityClass, objectTreePath, aliasUnicityKey); jpqlStatement.insert(joinAdditionsOffset.get(), joinFragment); joinAdditionsOffset.set(joinAdditionsOffset.get() + joinFragment.length()); jpqlStatement.append(" x").append(aliasUnicityKey.get()).append('.').append(attribute.getPossibleValueLabelAttribute()); } else { jpqlStatement.append(" x.").append(attribute.getAttributeName()); } }
protected Class<?> getValueType(PopulateValueRequest populateValueRequest, Class<?> startingValueType) { Class<?> valueType = startingValueType; if (!StringUtils.isEmpty(populateValueRequest.getMetadata().getToOneTargetProperty())) { Field nestedField = FieldManager.getSingleField(valueType, populateValueRequest.getMetadata() .getToOneTargetProperty()); ManyToOne manyToOne = nestedField.getAnnotation(ManyToOne.class); if (manyToOne != null && !manyToOne.targetEntity().getName().equals(void.class.getName())) { valueType = manyToOne.targetEntity(); } else { OneToOne oneToOne = nestedField.getAnnotation(OneToOne.class); if (oneToOne != null && !oneToOne.targetEntity().getName().equals(void.class.getName())) { valueType = oneToOne.targetEntity(); } } } return valueType; }
private Annotation anyAnnotation(Field field) { Annotation annotation = null; annotation = field.getAnnotation(ManyToMany.class); if (annotation != null) { return annotation; } annotation = field.getAnnotation(OneToOne.class); if (annotation != null) { return annotation; } annotation = field.getAnnotation(OneToMany.class); if (annotation != null) { return annotation; } annotation = field.getAnnotation(ManyToOne.class); if (annotation != null) { return annotation; } return null; }
private Annotation mappedByAnything(String property) { Field field = findField(property); Annotation relationAnnotation = null; relationAnnotation = field.getAnnotation(ManyToMany.class); if (relationAnnotation != null) { return relationAnnotation; } relationAnnotation = field.getAnnotation(OneToOne.class); if (relationAnnotation != null) { return relationAnnotation; } relationAnnotation = field.getAnnotation(OneToMany.class); if (relationAnnotation != null) { return relationAnnotation; } relationAnnotation = field.getAnnotation(ManyToOne.class); if (relationAnnotation != null) { return relationAnnotation; } return null; }
private Class<?> targetEntity(Annotation relationAnnotation) { if (relationAnnotation instanceof ManyToMany) { return ManyToMany.class.cast(relationAnnotation).targetEntity(); } if (relationAnnotation instanceof OneToOne) { return OneToOne.class.cast(relationAnnotation).targetEntity(); } if (relationAnnotation instanceof OneToMany) { return OneToMany.class.cast(relationAnnotation).targetEntity(); } if (relationAnnotation instanceof ManyToOne) { return ManyToOne.class.cast(relationAnnotation).targetEntity(); } return null; }
/** * Metodo que agrega relaciones * * @param criteria * @param example * @return */ @Nonnull private Criteria configureExample(@Nonnull final Criteria criteria, final T example) { if (example == null) { return criteria; } try { for (final Field f : example.getClass().getDeclaredFields()) { f.setAccessible(true); if (f.getAnnotation(OneToOne.class) == null && f.getAnnotation(ManyToOne.class) == null || f.get(example) == null) { continue; } criteria.add(Restrictions.eq(f.getName(), f.get(example))); } } catch (final Exception e) { this.log.error("Error al agregar la relación", e); } return criteria; }
private void buildUniqueProperty(final SingularProperty<E, ?> property) { if (this.context.getMaxUniqueProperties() > 0) { final boolean unique; final Column column = property.getAttribute().getAnnotation(Column.class); if (column != null && column.unique()) { unique = true; } else { final OneToOne oneToOne = property.getAttribute().getAnnotation(OneToOne.class); if (oneToOne != null) { unique = StringUtils.isEmpty(oneToOne.mappedBy()); } else { final JoinColumn joinColumn = property.getAttribute().getAnnotation(JoinColumn.class); unique = joinColumn != null && joinColumn.unique(); } } if (unique) { final UniquePropertyQuality propertyQuality = UniquePropertyQuality.getMatchingQuality(property); if (propertyQuality != null && isBetterUniquePropertyQuality(propertyQuality)) { this.uniquePropertiesQuality = propertyQuality; this.uniqueProperties = Collections .<SingularProperty<E, ?>> singletonList((SingularProperty<E, ?>) property); } } } }
/** * Find if a OneToOne or OneToMany annotation exits and if it's the case, verify if the fetch is LAZY. * @param field the field * @return true if eager, false otherwise */ public static boolean isLazy(final Field field) { final OneToOne oneOne = field.getAnnotation(OneToOne.class); if (oneOne != null) { // By Default Eager return oneOne.fetch().equals(FetchType.LAZY); } final ManyToOne manyOne = field.getAnnotation(ManyToOne.class); if (manyOne != null) { // By Default Eager return manyOne.fetch().equals(FetchType.LAZY); } final OneToMany oneMany = field.getAnnotation(OneToMany.class); if (oneMany != null) { // By Default Lazy return oneMany.fetch().equals(FetchType.LAZY); } final ManyToMany manyMany = field.getAnnotation(ManyToMany.class); if (manyMany != null) { // By Default Lazy return manyMany.fetch().equals(FetchType.LAZY); } // Other case, no problem return true; }
/** * @param field * @return */ private static Class<?> getTargetEntityFromAnnotation(final Field field) { for (final Annotation annotation : field.getAnnotations()) { if (instanceOf(annotation, OneToMany.class)) { return ((OneToMany) annotation).targetEntity(); } else if (instanceOf(annotation, OneToOne.class)) { return ((OneToOne) annotation).targetEntity(); } } return null; }
/** * Get the property metadata * for the supplied property * @param property * @param builder */ @SuppressWarnings("unchecked") private void extractPropertyMetadata(PropertyDescriptor property, ResourceMetadataBuilder builder) { // Get the field for the property Method accessor = property.getReadMethod(); // Get the property name String name = property.getName(); // Is it an identity field? if (AnnotationHelper.fieldOrPropertyAnnotated(property, Id.class)) { builder.identityProperty(name, accessor); } // Is it a relation field (and therefore embedded)? else if (AnnotationHelper.fieldOrPropertyAnnotated(property, ManyToOne.class, OneToOne.class, OneToMany.class, ManyToMany.class)) { builder.embeddedResource(name, accessor); } // None of the above, must be @Basic or unadorned. else { builder.simpleProperty(name, accessor); } }
@OneToOne( fetch = FetchType.LAZY, orphanRemoval = true, cascade = { CascadeType.REMOVE }, optional = false ) @JoinColumn( name = "IP_ADDRESS_RELATION_ID", nullable = false, unique = true ) public VlanIpAddressRelation getIpAddressRelation() { return ipAddressRelation; }
@OneToOne( fetch = FetchType.LAZY, orphanRemoval = true, cascade = { CascadeType.REMOVE }, optional = false ) @JoinColumn( name = "IP_ADDRESS_RELATION_ID", nullable = false, unique = true ) public RealNetworkInterfaceConfigurationIpAddressRelation getIpAddressRelation() { return ipAddressRelation; }
private static boolean needsToCascade(Field field) { Class<?> fieldtype = field.getType(); if (!DomainObject.class.isAssignableFrom(fieldtype)) return false; Annotation ann; CascadeType[] cascades = null; ann = field.getAnnotation(OneToOne.class); if (ann != null) { cascades = ((OneToOne) ann).cascade(); } else { ann = field.getAnnotation(OneToMany.class); if (ann != null) { cascades = ((OneToMany) ann).cascade(); } else { ann = field.getAnnotation(ManyToOne.class); if (ann != null) { cascades = ((ManyToOne) ann).cascade(); } else { ann = field.getAnnotation(ManyToMany.class); if (ann != null) { cascades = ((ManyToMany) ann).cascade(); } } } } if (cascades == null) return false; for (CascadeType cas : cascades) { if ((cas == CascadeType.ALL) || (cas == CascadeType.MERGE) || (cas == CascadeType.PERSIST) || (cas == CascadeType.REMOVE)) { return true; } } return false; }
private static boolean discoverTypeWithoutReflection(XProperty p) { if ( p.isAnnotationPresent( OneToOne.class ) && !p.getAnnotation( OneToOne.class ) .targetEntity() .equals( void.class ) ) { return true; } else if ( p.isAnnotationPresent( OneToMany.class ) && !p.getAnnotation( OneToMany.class ) .targetEntity() .equals( void.class ) ) { return true; } else if ( p.isAnnotationPresent( ManyToOne.class ) && !p.getAnnotation( ManyToOne.class ) .targetEntity() .equals( void.class ) ) { return true; } else if ( p.isAnnotationPresent( ManyToMany.class ) && !p.getAnnotation( ManyToMany.class ) .targetEntity() .equals( void.class ) ) { return true; } else if ( p.isAnnotationPresent( org.hibernate.annotations.Any.class ) ) { return true; } else if ( p.isAnnotationPresent( ManyToAny.class ) ) { if ( !p.isCollection() && !p.isArray() ) { throw new AnnotationException( "@ManyToAny used on a non collection non array property: " + p.getName() ); } return true; } else if ( p.isAnnotationPresent( Type.class ) ) { return true; } else if ( p.isAnnotationPresent( Target.class ) ) { return true; } return false; }
Ejb3JoinColumn[] buildDefaultJoinColumnsForXToOne(XProperty property, PropertyData inferredData) { Ejb3JoinColumn[] joinColumns; JoinTable joinTableAnn = propertyHolder.getJoinTable( property ); if ( joinTableAnn != null ) { joinColumns = Ejb3JoinColumn.buildJoinColumns( joinTableAnn.inverseJoinColumns(), null, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), mappings ); if ( StringHelper.isEmpty( joinTableAnn.name() ) ) { throw new AnnotationException( "JoinTable.name() on a @ToOne association has to be explicit: " + BinderHelper.getPath( propertyHolder, inferredData ) ); } } else { OneToOne oneToOneAnn = property.getAnnotation( OneToOne.class ); String mappedBy = oneToOneAnn != null ? oneToOneAnn.mappedBy() : null; joinColumns = Ejb3JoinColumn.buildJoinColumns( null, mappedBy, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), mappings ); } return joinColumns; }
private static Class<?> getTargetEntityClass(XProperty property) { final ManyToOne mTo = property.getAnnotation( ManyToOne.class ); if (mTo != null) { return mTo.targetEntity(); } final OneToOne oTo = property.getAnnotation( OneToOne.class ); if (oTo != null) { return oTo.targetEntity(); } throw new AssertionFailure("Unexpected discovery of a targetEntity: " + property.getName() ); }
private static boolean isIdClassPkOfTheAssociatedEntity( InheritanceState.ElementsToProcess elementsToProcess, XClass compositeClass, PropertyData inferredData, PropertyData baseInferredData, AccessType propertyAccessor, Map<XClass, InheritanceState> inheritanceStatePerClass, Mappings mappings) { if ( elementsToProcess.getIdPropertyCount() == 1 ) { final PropertyData idPropertyOnBaseClass = getUniqueIdPropertyFromBaseClass( inferredData, baseInferredData, propertyAccessor, mappings ); final InheritanceState state = inheritanceStatePerClass.get( idPropertyOnBaseClass.getClassOrElement() ); if ( state == null ) { return false; //while it is likely a user error, let's consider it is something that might happen } final XClass associatedClassWithIdClass = state.getClassWithIdClass( true ); if ( associatedClassWithIdClass == null ) { //we cannot know for sure here unless we try and find the @EmbeddedId //Let's not do this thorough checking but do some extra validation final XProperty property = idPropertyOnBaseClass.getProperty(); return property.isAnnotationPresent( ManyToOne.class ) || property.isAnnotationPresent( OneToOne.class ); } else { final XClass idClass = mappings.getReflectionManager().toXClass( associatedClassWithIdClass.getAnnotation( IdClass.class ).value() ); return idClass.equals( compositeClass ); } } else { return false; } }
@OneToOne(cascade = CascadeType.PERSIST) @JoinColumns ({ @JoinColumn(name = "view" , referencedColumnName = "dataid" ), @JoinColumn(name = "creater" , referencedColumnName = "creater" ) }) public TopicView getView() { return view; }
/** * @return code */ @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) @JoinColumn(name = "idCode") public AccessCodeEntity getCode() { return this.code; }
/** * @return visitor */ @OneToOne @JoinColumn(name = "idVisitor") public VisitorEntity getVisitor() { return this.visitor; }
/** * To differentiate @OneToOne associations from @ManyToOne ones. */ @Test public void nonInverseOneToOneAssociationsMustHaveUniqueJoinColumn() { final Stream<Field> failedFields = filterFieldsOfManagedJpaTypes(field -> { final OneToOne oneToOne = field.getAnnotation(OneToOne.class); final JoinColumn joinCol = field.getAnnotation(JoinColumn.class); return !isIdField(field) && oneToOne != null && "".equals(oneToOne.mappedBy()) && (joinCol == null || !joinCol.unique()); }); assertNoFields(failedFields, "These entity fields should be annotated with @JoinColumn(unique = true): "); }
private boolean hasJpaAnnotations(MetaAttribute attribute) { List<Class<? extends Annotation>> annotationClasses = Arrays.asList(Id.class, EmbeddedId.class, Column.class, ManyToMany.class, ManyToOne.class, OneToMany.class, OneToOne.class, Version.class, ElementCollection.class); for (Class<? extends Annotation> annotationClass : annotationClasses) { if (attribute.getAnnotation(annotationClass) != null) { return true; } } return false; }
private void checkOneToOne(Method g) { OneToOne annotation = g.getAnnotation(OneToOne.class); if(null == annotation) return; if(annotation.optional() && annotation.fetch() == FetchType.LAZY) { problem(Severity.ERROR, "@OneOnOne that is optional with fetch=LAZY does eager fetching, causing big performance trouble"); m_badOneToOne++; } }
protected boolean isRequired(MetaProperty metaProperty) { if (metaProperty.isMandatory()) return true; ManyToOne many2One = metaProperty.getAnnotatedElement().getAnnotation(ManyToOne.class); if (many2One != null && !many2One.optional()) return true; OneToOne one2one = metaProperty.getAnnotatedElement().getAnnotation(OneToOne.class); return one2one != null && !one2one.optional(); }
/** * JAVADOC Method Level Comments * * @return JAVADOC. */ //@Transient @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name = "attachment") public Attachment getAttachment() { return attachment; }
protected void addAssertValidCheckIfRequired(final Annotation constraintAnnotation, final Collection<Check> checks, @SuppressWarnings("unused") /*parameter for potential use by subclasses*/final AccessibleObject fieldOrMethod) { if (containsCheckOfType(checks, AssertValidCheck.class)) return; if (constraintAnnotation instanceof OneToOne || constraintAnnotation instanceof OneToMany || constraintAnnotation instanceof ManyToOne || constraintAnnotation instanceof ManyToMany) checks.add(new AssertValidCheck()); }
@Cascade(org.hibernate.annotations.CascadeType.ALL) @OneToOne(fetch = FetchType.LAZY) // disable foreign key, to be able to remove runtime data @JoinColumn(name = "ENV_SCRIPT_ID", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)) public ScriptData getEnvScript() { return envScript; }
@Cascade(org.hibernate.annotations.CascadeType.ALL) @OneToOne(fetch = FetchType.LAZY) // disable foreign key, to be able to remove runtime data @JoinColumn(name = "SCRIPT_ID", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)) public ScriptData getScript() { return script; }
@Cascade(CascadeType.ALL) @OneToOne(fetch = FetchType.LAZY) // disable foreign key, to be able to remove runtime data @JoinColumn(name = "PRE_SCRIPT_ID", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)) public ScriptData getPreScript() { return preScript; }
@Cascade(CascadeType.ALL) @OneToOne(fetch = FetchType.LAZY) // disable foreign key, to be able to remove runtime data @JoinColumn(name = "POST_SCRIPT_ID", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)) public ScriptData getPostScript() { return postScript; }
@Cascade(CascadeType.ALL) @OneToOne(fetch = FetchType.LAZY) // disable foreign key, to be able to remove runtime data @JoinColumn(name = "CLEAN_SCRIPT_ID", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)) public ScriptData getCleanScript() { return cleanScript; }
@Cascade(CascadeType.ALL) @OneToOne(fetch = FetchType.LAZY) // disable foreign key, to be able to remove runtime data @JoinColumn(name = "FLOW_SCRIPT_ID", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)) public ScriptData getFlowScript() { return flowScript; }