@Entity @Table(name = "vets") public class Vet extends Person { @ManyToMany(fetch = FetchType.EAGER) @JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"), inverseJoinColumns = @JoinColumn(name = "specialty_id")) private Set<Specialty> specialties;
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; }
@Test public void testCascadeSomeWithDefaultPersist() throws Exception { reader = getReader( Entity2.class, "field1", "many-to-many.orm19.xml" ); assertAnnotationPresent( ManyToMany.class ); ManyToMany relAnno = reader.getAnnotation( ManyToMany.class ); assertEquals( 4, relAnno.cascade().length ); assertEquals( CascadeType.REMOVE, relAnno.cascade()[0] ); assertEquals( CascadeType.REFRESH, relAnno.cascade()[1] ); assertEquals( CascadeType.DETACH, relAnno.cascade()[2] ); assertEquals( CascadeType.PERSIST, relAnno.cascade()[3] ); }
@Entity(name = "Project") public class Project { @Id private Long id; @ManyToMany private List<Employee> employees = new ArrayList<>(); //Getters and setters omitted for brevity } }
@Entity(name = "User") @Table(name = "`User`") @FilterDef(name = "activeUserFilter", defaultCondition = "active = true") @Filter(name = "activeUserFilter") private String name; private Boolean active; @ManyToMany @Fetch(FetchMode.JOIN) @Filter(name = "activeRoleFilter")
@ManyToMany @JoinTable(joinColumns = @JoinColumn(referencedColumnName = "address"), inverseJoinColumns = @JoinColumn(referencedColumnName = "name") ) public Set<Inhabitant> getHasInhabitants() { return hasInhabitants; }
@ManyToMany( cascade = CascadeType.ALL ) @JoinTable( name = "CHARACTER_ALIAS", indexes = @Index( columnList = "characters_id")) // @JoinTable( // name = "CHARACTER_ALIAS", // joinColumns = @JoinColumn(name="ALIAS_ID", referencedColumnName="ID"), // inverseJoinColumns = @JoinColumn(name="CHARACTER_ID", referencedColumnName="ID") // ) public List<Character> getCharacters() { return characters; }
@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() ); }
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 ) JoinTable table = getPhysicalAnnotation( annotationType ); if ( table != null ) { ad.setValue( "name", table.name() ); ad.setValue( "schema", table.schema() ); ad.setValue( "catalog", table.catalog() ); ad.setValue( "uniqueConstraints", table.uniqueConstraints() ); ad.setValue( "joinColumns", table.joinColumns() );
mappedBy = manyToManyAnn.mappedBy(); collectionBinder.setTargetEntity( context.getBootstrapContext().getReflectionManager().toXClass( manyToManyAnn.targetEntity() ) ); collectionBinder.setCascadeStrategy( getCascadeStrategy( manyToManyAnn.cascade(), hibernateCascade, false, false
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(); }
public static CtClass getTargetEntityClass(CtClass managedCtClass, CtField persistentField) throws NotFoundException { // get targetEntity defined in the annotation try { OneToOne oto = PersistentAttributesHelper.getAnnotation( persistentField, OneToOne.class ); OneToMany otm = PersistentAttributesHelper.getAnnotation( persistentField, OneToMany.class ); ManyToOne mto = PersistentAttributesHelper.getAnnotation( persistentField, ManyToOne.class ); ManyToMany mtm = PersistentAttributesHelper.getAnnotation( persistentField, ManyToMany.class ); Class<?> targetClass = null; if ( oto != null ) { targetClass = oto.targetEntity(); } if ( otm != null ) { targetClass = otm.targetEntity(); } if ( mto != null ) { targetClass = mto.targetEntity(); } if ( mtm != null ) { targetClass = mtm.targetEntity(); } if ( targetClass != null && targetClass != void.class ) { return managedCtClass.getClassPool().get( targetClass.getName() ); } } catch (NotFoundException ignore) { } // infer targetEntity from generic type signature String inferredTypeName = inferTypeName( managedCtClass, persistentField.getName() ); return inferredTypeName == null ? null : managedCtClass.getClassPool().get( inferredTypeName ); }
fetchType = manyToMany.fetch();
@Entity(name = "Project") public class Project { @Id private Long id; @ManyToMany private List<Employee> employees = new ArrayList<>(); //Getters and setters omitted for brevity } //end::fetching-strategies-domain-model-example[]
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}) @JoinTable( name = "StoreSupplier", joinColumns = @JoinColumn(name = "store"), inverseJoinColumns = @JoinColumn(name = "supplier") ) public Set<Supplier> getSuppliers() { return suppliers; }
@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() ); }
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 JoinTable table = getJavaAnnotation( annotationType ); if ( table != null ) { ad.setValue( "name", table.name() ); ad.setValue( "schema", table.schema() ); ad.setValue( "catalog", table.catalog() ); ad.setValue( "uniqueConstraints", table.uniqueConstraints() ); ad.setValue( "joinColumns", table.joinColumns() );
mappedBy = manyToManyAnn.mappedBy(); collectionBinder.setTargetEntity( mappings.getReflectionManager().toXClass( manyToManyAnn.targetEntity() ) ); collectionBinder.setCascadeStrategy( getCascadeStrategy( manyToManyAnn.cascade(), hibernateCascade, false, false
@Override protected String getMappedBy(Field field) { return field.getAnnotation(ManyToMany.class).mappedBy(); } @Override
.targetEntity() .equals( void.class ) ) { return true;