Search in sources :

Example 6 with ManyManyLinkField

use of org.nutz.dao.impl.entity.field.ManyManyLinkField in project nutz by nutzam.

the class AbstractJdbcExpert method dropRelation.

public void dropRelation(Dao dao, Entity<?> en) {
    final List<Sql> sqls = new ArrayList<Sql>(5);
    for (LinkField lf : en.visitManyMany(null, null, null)) {
        ManyManyLinkField mm = (ManyManyLinkField) lf;
        if (!dao.exists(mm.getRelationName()))
            continue;
        sqls.add(Sqls.create("DROP TABLE " + mm.getRelationName()));
    }
    dao.execute(sqls.toArray(new Sql[sqls.size()]));
}
Also used : ArrayList(java.util.ArrayList) ManyManyLinkField(org.nutz.dao.impl.entity.field.ManyManyLinkField) ManyManyLinkField(org.nutz.dao.impl.entity.field.ManyManyLinkField) LinkField(org.nutz.dao.entity.LinkField) Sql(org.nutz.dao.sql.Sql)

Example 7 with ManyManyLinkField

use of org.nutz.dao.impl.entity.field.ManyManyLinkField in project nutz by nutzam.

the class DoInsertRelationLinkVisitor method visit.

public void visit(final Object obj, LinkField lnk) {
    // 只有多对多的映射才被考虑
    if (lnk instanceof ManyManyLinkField) {
        // 获取两边映射主键的值
        final ManyManyLinkField mm = (ManyManyLinkField) lnk;
        Object value = lnk.getValue(obj);
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(Lang.length(value));
        Lang.each(value, new Each<Object>() {

            public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException {
                list.add(new RelationObjectMap(mm, obj, ele));
            }
        });
        if (list.isEmpty())
            return;
        Entity<Map<String, Object>> en = holder.makeEntity(mm.getRelationName(), list.get(0));
        Pojo pojo = opt.maker().makeInsert(en);
        pojo.setOperatingObject(list);
        for (Object p : list) pojo.addParamsBy(p);
        opt.add(pojo);
    }
}
Also used : Pojo(org.nutz.dao.sql.Pojo) ArrayList(java.util.ArrayList) ManyManyLinkField(org.nutz.dao.impl.entity.field.ManyManyLinkField) RelationObjectMap(org.nutz.dao.util.RelationObjectMap) ExitLoop(org.nutz.lang.ExitLoop) LoopException(org.nutz.lang.LoopException) RelationObjectMap(org.nutz.dao.util.RelationObjectMap) Map(java.util.Map)

Example 8 with ManyManyLinkField

use of org.nutz.dao.impl.entity.field.ManyManyLinkField in project nutz by nutzam.

the class DoUpdateRelationLinkVisitor method visit.

public void visit(Object obj, LinkField lnk) {
    if (lnk instanceof ManyManyLinkField) {
        ManyManyLinkField mm = (ManyManyLinkField) lnk;
        Entity<?> en = opt.makeEntity(mm.getRelationName(), map);
        Pojo pojo = opt.maker().makeUpdate(en, null);
        pojo.setOperatingObject(map);
        pojo.append(items);
        opt.add(pojo);
    }
}
Also used : Pojo(org.nutz.dao.sql.Pojo) ManyManyLinkField(org.nutz.dao.impl.entity.field.ManyManyLinkField)

Example 9 with ManyManyLinkField

use of org.nutz.dao.impl.entity.field.ManyManyLinkField in project nutz by nutzam.

the class DynamicEntityParsingTest method tank_many_many_link_test.

@Test
public void tank_many_many_link_test() {
    pojos.initPlatoon(1);
    TableName.set(1);
    Entity<?> en = dao.getEntity(Tank.class);
    ManyManyLinkField link = (ManyManyLinkField) en.getLinkFields("members").get(0);
    assertEquals("dao_d_m_soldier_tank_1", link.getRelationName());
    assertEquals("id", link.getLinkedPkNames()[0]);
    assertEquals("name", link.getLinkedPkNames()[1]);
    TableName.clear();
    pojos.dropPlatoon(1);
}
Also used : ManyManyLinkField(org.nutz.dao.impl.entity.field.ManyManyLinkField) Test(org.junit.Test)

Aggregations

ManyManyLinkField (org.nutz.dao.impl.entity.field.ManyManyLinkField)9 Pojo (org.nutz.dao.sql.Pojo)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)2 ExitLoop (org.nutz.lang.ExitLoop)2 LoopException (org.nutz.lang.LoopException)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 DaoException (org.nutz.dao.DaoException)1 EntityField (org.nutz.dao.entity.EntityField)1 LinkField (org.nutz.dao.entity.LinkField)1 MappingField (org.nutz.dao.entity.MappingField)1 Column (org.nutz.dao.entity.annotation.Column)1 Id (org.nutz.dao.entity.annotation.Id)1 ManyMany (org.nutz.dao.entity.annotation.ManyMany)1 Name (org.nutz.dao.entity.annotation.Name)1 One (org.nutz.dao.entity.annotation.One)1 ManyLinkField (org.nutz.dao.impl.entity.field.ManyLinkField)1 NutMappingField (org.nutz.dao.impl.entity.field.NutMappingField)1