Search in sources :

Example 11 with ManyToMany

use of javax.persistence.ManyToMany in project hibernate-orm by hibernate.

the class Ejb3XmlManyToManyTest method testCascadeSomeWithDefaultPersist.

@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]);
}
Also used : ManyToMany(javax.persistence.ManyToMany) Test(org.junit.Test)

Example 12 with ManyToMany

use of javax.persistence.ManyToMany in project hibernate-orm by hibernate.

the class Ejb3XmlManyToManyTest method testAllAttributes.

@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());
}
Also used : ManyToMany(javax.persistence.ManyToMany) Test(org.junit.Test)

Example 13 with ManyToMany

use of javax.persistence.ManyToMany in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method overridesDefaultsInJoinTable.

private JoinTable overridesDefaultsInJoinTable(Annotation annotation, XMLContext.Default defaults) {
    //no element but might have some default or some annotation
    boolean defaultToJoinTable = !(isPhysicalAnnotationPresent(JoinColumn.class) || isPhysicalAnnotationPresent(JoinColumns.class));
    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));
    final Class<JoinTable> annotationType = JoinTable.class;
    if (defaultToJoinTable && (StringHelper.isNotEmpty(defaults.getCatalog()) || StringHelper.isNotEmpty(defaults.getSchema()))) {
        AnnotationDescriptor ad = new AnnotationDescriptor(annotationType);
        if (defaults.canUseJavaAnnotations()) {
            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());
                ad.setValue("inverseJoinColumns", table.inverseJoinColumns());
            }
        }
        if (StringHelper.isEmpty((String) ad.valueOf("schema")) && StringHelper.isNotEmpty(defaults.getSchema())) {
            ad.setValue("schema", defaults.getSchema());
        }
        if (StringHelper.isEmpty((String) ad.valueOf("catalog")) && StringHelper.isNotEmpty(defaults.getCatalog())) {
            ad.setValue("catalog", defaults.getCatalog());
        }
        return AnnotationFactory.create(ad);
    } else if (defaults.canUseJavaAnnotations()) {
        return getPhysicalAnnotation(annotationType);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) JoinColumn(javax.persistence.JoinColumn) MapKeyJoinColumns(javax.persistence.MapKeyJoinColumns) JoinColumns(javax.persistence.JoinColumns) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) ManyToMany(javax.persistence.ManyToMany) ElementCollection(javax.persistence.ElementCollection) OneToMany(javax.persistence.OneToMany) JoinTable(javax.persistence.JoinTable)

Example 14 with ManyToMany

use of javax.persistence.ManyToMany in project hibernate-orm by hibernate.

the class PersistentAttributesHelper method getMappedByFromAnnotation.

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();
}
Also used : OneToOne(javax.persistence.OneToOne) ManyToMany(javax.persistence.ManyToMany) OneToMany(javax.persistence.OneToMany)

Example 15 with ManyToMany

use of javax.persistence.ManyToMany in project eweb4j-framework by laiweiwei.

the class ManyToManyDAO method update.

/**
	 * 一对多级联更新
	 */
public void update(Long newFromRefVal) {
    if (this.fields == null || this.fields.size() == 0)
        return;
    // "update {table} set {fromRefCol} = {newFromRefVal} where {fromRefCol} = {fromRefVal}
    // ; update {relTable} set {from} = {newFromRefVal} where {from} = {fromRefVal}"
    String format = "update %s set %s = %s where %s = %s ;";
    for (Field f : fields) {
        Method tarGetter = ru.getGetter(f.getName());
        if (tarGetter == null)
            continue;
        ManyToMany ann = tarGetter.getAnnotation(ManyToMany.class);
        if (ann == null) {
            ann = f.getAnnotation(ManyToMany.class);
            if (ann == null)
                continue;
        }
        JoinTable join = tarGetter.getAnnotation(JoinTable.class);
        if (join == null) {
            join = f.getAnnotation(JoinTable.class);
            if (join == null)
                continue;
        }
        JoinColumn[] froms = join.joinColumns();
        if (froms == null || froms.length == 0)
            continue;
        // 第三方关系表
        String relTable = join.name();
        // 主类在第三方关系表中的字段名
        String from = froms[0].name();
        String fromRefCol = froms[0].referencedColumnName();
        if (fromRefCol == null || fromRefCol.trim().length() == 0)
            fromRefCol = ORMConfigBeanUtil.getIdColumn(t);
        String fromRefField = ORMConfigBeanUtil.getField(t.getClass(), fromRefCol);
        try {
            Method fromRefFieldGetter = ru.getGetter(fromRefField);
            if (fromRefFieldGetter == null)
                throw new DAOException("can not find the 'from ref field -> " + fromRefField + "' of " + t.getClass() + " 's getter method", null);
            Object _obj = fromRefFieldGetter.invoke(t);
            if (_obj == null)
                continue;
            String fromRefVal = String.valueOf(_obj);
            // "update {table} set {fromRefCol} = {newFromRefVal} where {fromRefCol} = {fromRefVal}
            // ; update {relTable} set {from} = {newFromRefVal} where {from} = {fromRefVal}"
            final String sql1 = String.format(format, table, fromRefCol, newFromRefVal, fromRefCol, fromRefVal);
            final String sql2 = String.format(format, relTable, from, newFromRefVal, from, fromRefVal);
            Transaction.execute(new Trans() {

                @Override
                public void run(Object... args) throws Exception {
                    DAOFactory.getUpdateDAO(dsName).updateBySQL(sql1);
                    DAOFactory.getUpdateDAO(dsName).updateBySQL(sql2);
                }
            });
        } catch (Exception e) {
            throw new DAOException("", e);
        }
    }
}
Also used : DAOException(org.eweb4j.orm.dao.DAOException) Field(java.lang.reflect.Field) JoinColumn(javax.persistence.JoinColumn) ManyToMany(javax.persistence.ManyToMany) Method(java.lang.reflect.Method) Trans(org.eweb4j.orm.jdbc.transaction.Trans) DAOException(org.eweb4j.orm.dao.DAOException) JoinTable(javax.persistence.JoinTable)

Aggregations

ManyToMany (javax.persistence.ManyToMany)20 OneToMany (javax.persistence.OneToMany)10 JoinColumn (javax.persistence.JoinColumn)9 Field (java.lang.reflect.Field)8 JoinTable (javax.persistence.JoinTable)8 Method (java.lang.reflect.Method)6 OneToOne (javax.persistence.OneToOne)6 ReflectUtil (org.eweb4j.util.ReflectUtil)6 ElementCollection (javax.persistence.ElementCollection)4 ManyToOne (javax.persistence.ManyToOne)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 DAOException (org.eweb4j.orm.dao.DAOException)3 File (java.io.File)2 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Column (javax.persistence.Column)2 FetchType (javax.persistence.FetchType)2 Id (javax.persistence.Id)2