protected FieldInfo buildFieldInfo(Field field) { FieldInfo info = new FieldInfo(); info.setName(field.getName()); info.setGenericType(field.getGenericType()); ManyToMany manyToMany = field.getAnnotation(ManyToMany.class); if (manyToMany != null) { info.setManyToManyMappedBy(manyToMany.mappedBy()); info.setManyToManyTargetEntity(manyToMany.targetEntity().getName()); } OneToMany oneToMany = field.getAnnotation(OneToMany.class); if (oneToMany != null) { info.setOneToManyMappedBy(oneToMany.mappedBy()); info.setOneToManyTargetEntity(oneToMany.targetEntity().getName()); } return info; }
private static String getMappedByFromAnnotation(CtField persistentField) { OneToOne oto = PersistentAttributesHelper.getAnnotation( persistentField, OneToOne.class ); if ( oto != null ) { return oto.mappedBy(); } OneToMany otm = PersistentAttributesHelper.getAnnotation( persistentField, OneToMany.class ); if ( otm != null ) { return otm.mappedBy(); } // For @ManyToOne associations, mappedBy must come from the @OneToMany side of the association ManyToMany mtm = PersistentAttributesHelper.getAnnotation( persistentField, ManyToMany.class ); return mtm == null ? "" : mtm.mappedBy(); }
protected FieldInfo buildFieldInfo(Field field) { FieldInfo info = new FieldInfo(); info.setName(field.getName()); info.setGenericType(field.getGenericType()); ManyToMany manyToMany = field.getAnnotation(ManyToMany.class); if (manyToMany != null) { info.setManyToManyMappedBy(manyToMany.mappedBy()); info.setManyToManyTargetEntity(manyToMany.targetEntity().getName()); } OneToMany oneToMany = field.getAnnotation(OneToMany.class); if (oneToMany != null) { info.setOneToManyMappedBy(oneToMany.mappedBy()); info.setOneToManyTargetEntity(oneToMany.targetEntity().getName()); } MapKey mapKey = field.getAnnotation(MapKey.class); if (mapKey != null) { info.setMapKey(mapKey.name()); } return info; }
final Class<? extends Annotation> annotationClass = annotation.annotationType(); defaultToJoinTable = defaultToJoinTable && ( ( annotationClass == ManyToMany.class && StringHelper.isEmpty( ( (ManyToMany) annotation ).mappedBy() ) ) || ( annotationClass == OneToMany.class && StringHelper.isEmpty( ( (OneToMany) annotation ).mappedBy() ) ) || ( annotationClass == ElementCollection.class )
@Test public void testNoChildren() throws Exception { reader = getReader( Entity2.class, "field1", "many-to-many.orm1.xml" ); assertAnnotationPresent( ManyToMany.class ); assertAnnotationNotPresent( OrderBy.class ); assertAnnotationNotPresent( OrderColumn.class ); assertAnnotationNotPresent( MapKey.class ); assertAnnotationNotPresent( MapKeyClass.class ); assertAnnotationNotPresent( MapKeyTemporal.class ); assertAnnotationNotPresent( MapKeyEnumerated.class ); assertAnnotationNotPresent( MapKeyColumn.class ); assertAnnotationNotPresent( MapKeyJoinColumns.class ); assertAnnotationNotPresent( MapKeyJoinColumn.class ); assertAnnotationNotPresent( JoinTable.class ); assertAnnotationNotPresent( Access.class ); ManyToMany relAnno = reader.getAnnotation( ManyToMany.class ); assertEquals( 0, relAnno.cascade().length ); assertEquals( FetchType.LAZY, relAnno.fetch() ); assertEquals( "", relAnno.mappedBy() ); assertEquals( void.class, relAnno.targetEntity() ); }
@Test public void testAllAttributes() throws Exception { reader = getReader( Entity2.class, "field1", "many-to-many.orm21.xml" ); assertAnnotationPresent( ManyToMany.class ); assertAnnotationNotPresent( OrderBy.class ); assertAnnotationNotPresent( OrderColumn.class ); assertAnnotationNotPresent( MapKey.class ); assertAnnotationNotPresent( MapKeyClass.class ); assertAnnotationNotPresent( MapKeyTemporal.class ); assertAnnotationNotPresent( MapKeyEnumerated.class ); assertAnnotationNotPresent( MapKeyColumn.class ); assertAnnotationNotPresent( MapKeyJoinColumns.class ); assertAnnotationNotPresent( MapKeyJoinColumn.class ); assertAnnotationNotPresent( JoinTable.class ); assertAnnotationPresent( Access.class ); ManyToMany relAnno = reader.getAnnotation( ManyToMany.class ); assertEquals( 0, relAnno.cascade().length ); assertEquals( FetchType.EAGER, relAnno.fetch() ); assertEquals( "field2", relAnno.mappedBy() ); assertEquals( Entity3.class, relAnno.targetEntity() ); assertEquals( AccessType.PROPERTY, reader.getAnnotation( Access.class ) .value() ); }
Arrays.asList(m2mAnnotation.cascade()), Boolean.TRUE, m2mAnnotation.mappedBy(), Relation.ForeignKey.MANY_TO_MANY);
/** * INTERNAL: (Overridden in XMLManyToManyAccessor) */ public String getMappedBy() { return m_manyToMany.mappedBy(); }
@Override protected String getMappedBy(Field field) { return field.getAnnotation(ManyToMany.class).mappedBy(); } @Override
private void readToMany(ManyToMany propAnn, DeployBeanPropertyAssocMany<?> manyProp) { manyProp.setMappedBy(propAnn.mappedBy()); manyProp.setFetchType(propAnn.fetch()); setCascadeTypes(propAnn.cascade(), manyProp.getCascadeInfo()); Class<?> targetType = propAnn.targetEntity(); if (targetType.equals(void.class)) { // via reflection of generics type targetType = manyProp.getTargetType(); } else { manyProp.setTargetType(targetType); } // find the other many table (not intersection) BeanTable assoc = factory.getBeanTable(targetType); if (assoc == null) { throw new BeanNotRegisteredException(errorMsgMissingBeanTable(targetType, manyProp.getFullBeanName())); } manyProp.setManyToMany(); manyProp.setModifyListenMode(ModifyListenMode.ALL); manyProp.setBeanTable(assoc); manyProp.getTableJoin().setType(SqlJoinType.OUTER); }
mappedBy = manyToManyAnn.mappedBy(); collectionBinder.setTargetEntity( context.getBootstrapContext().getReflectionManager().toXClass( manyToManyAnn.targetEntity() )
public String getMappedBy() { if (element.isAnnotationPresent(OneToMany.class)) { return element.getAnnotation(OneToMany.class).mappedBy(); } else if (element.isAnnotationPresent(OneToOne.class)) { return element.getAnnotation(OneToMany.class).mappedBy(); } else if (element.isAnnotationPresent(ManyToMany.class)) { return element.getAnnotation(ManyToMany.class).mappedBy(); } return null; }
protected String getInverseField(Field field) { OneToMany oneToManyAnnotation = field.getAnnotation(OneToMany.class); if (oneToManyAnnotation != null) return isBlank(oneToManyAnnotation.mappedBy()) ? null : oneToManyAnnotation.mappedBy(); ManyToMany manyToManyAnnotation = field.getAnnotation(ManyToMany.class); if (manyToManyAnnotation != null) return isBlank(manyToManyAnnotation.mappedBy()) ? null : manyToManyAnnotation.mappedBy(); OneToOne oneToOneAnnotation = field.getAnnotation(OneToOne.class); if (oneToOneAnnotation != null) return isBlank(oneToOneAnnotation.mappedBy()) ? null : oneToOneAnnotation.mappedBy(); return null; }
/** * Determine whether an object denoted by the given property is merged into persistence context together with the * owning object. This is true if the property is ManyToMany, or if it is OneToMany with certain CascadeType * defined. */ public boolean isCascade(MetaProperty metaProperty) { Objects.requireNonNull(metaProperty, "metaProperty is null"); OneToMany oneToMany = metaProperty.getAnnotatedElement().getAnnotation(OneToMany.class); if (oneToMany != null) { final Collection<CascadeType> cascadeTypes = Arrays.asList(oneToMany.cascade()); if (cascadeTypes.contains(CascadeType.ALL) || cascadeTypes.contains(CascadeType.MERGE)) { return true; } } ManyToMany manyToMany = metaProperty.getAnnotatedElement().getAnnotation(ManyToMany.class); if (manyToMany != null && StringUtils.isBlank(manyToMany.mappedBy())) { return true; } return false; }
final Class<? extends Annotation> annotationClass = annotation.annotationType(); defaultToJoinTable = defaultToJoinTable && ( ( annotationClass == ManyToMany.class && StringHelper.isEmpty( ( (ManyToMany) annotation ).mappedBy() ) ) || ( annotationClass == OneToMany.class && StringHelper.isEmpty( ( (OneToMany) annotation ).mappedBy() ) ) || ( annotationClass == CollectionOfElements.class ) //legacy Hibernate
private void introspectManyToMany(ManyToMany manyToMany) { Class targetClass = manyToMany.targetEntity(); if (! void.class.equals(targetClass)) setTargetEntity(targetClass); setCascadeTypes(manyToMany.cascade()); setFetch(manyToMany.fetch()); _mappedBy = manyToMany.mappedBy(); }
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; }
private static String getMappedByFromAnnotation(CtField persistentField) { OneToOne oto = PersistentAttributesHelper.getAnnotation( persistentField, OneToOne.class ); if ( oto != null ) { return oto.mappedBy(); } OneToMany otm = PersistentAttributesHelper.getAnnotation( persistentField, OneToMany.class ); if ( otm != null ) { return otm.mappedBy(); } // For @ManyToOne associations, mappedBy must come from the @OneToMany side of the association ManyToMany mtm = PersistentAttributesHelper.getAnnotation( persistentField, ManyToMany.class ); return mtm == null ? "" : mtm.mappedBy(); }
mappedBy = manyToManyAnn.mappedBy(); collectionBinder.setTargetEntity( mappings.getReflectionManager().toXClass( manyToManyAnn.targetEntity() )