0

我有类 CreatureType 和 Weapon(用来对付某些生物的最佳武器),我需要模拟武器对特定 CreatureType 具有各种效率的事实。所以我创建了具有效率属性的关联实体 CreatureTypeWeapon。

我是这样编码的(原理一样)http://uaihebert.com/?p=1674&page=22

我问这个问题的原因是我不知道如何为关联实体实现dao类。

我有 CreatureType 的 DAO 类,我有 Weapon 的 DAO 类,它实现了带有 CRUD 操作的接口。

但我不知何故错过了处理关联实体上的 CRUD 操作的方法。假设我有这个 DAO

public class CreatureTypeWeaponDAOImpl implements CreatureTypeWeaponDAO {

    @Override
    public void create(CreatureTypeWeapon creatureTypeWeapon) {
        // implementation
    }
    // ... other CRUD methods
}

我这样使用它:

// this is like a person
CreatureType creatureType = new CreatureType();
creatureType.setName("alien");
creatureType.setMaxHitPoints(200);

// this is like a dog
Weapon weapon = new Weapon();           
weapon.setName("ak-47");

// this is like PersonDog
CreatureTypeWeapon creatureTypeWeapon = new CreatureTypeWeapon();
creatureTypeWeapon.setWeapon(weapon);
creatureTypeWeapon.setCreatureType(creatureType);
// this is like "adoptionDate" in the link posted
// 0.5 means when I hit it with ak-47 it takes half of its max hit points
// so I have to fire 2 times on it and it will die.
creatureTypeWeapon.setEfficiency(0.5);

// this is actually injected
CreatureTypeWeaponDAO dao = new CreatureTypeWeaponDAOImpl();
dao.create(creatureTypeWeapon);

但问题是,如何实施?我能够正常坚持它,但假设我要删除这种关系:

dao.remove(creatureTypeWeapon);

可以这样实现

public class CreatureTypeWeaponDAOImpl implements CreatureTypeWeaponDAO {
    void remove(CreatureTypeWeapon arg) {
        CreatureTypeWeapon toRemove = em.find(/* WHAT TO PUT HERE */);
        em.remove(toRemove);
    }
}

通常,我放在那里

em.find(CreatureTypeWeapon.class, arg.getId());

但是关联实体中没有“id”....

更新

好的,这就是我在 CreatureTypeWeaponDAOImpl 中获取 CreatureTypeWeapon 实例的方式

TypedQuery<CreatureTypeWeapon> query = 
    em.createQuery(
        "SELECT ctw FROM CreatureTypeWeapon ctw WHERE "
            + "creatureType = :creatureType "
            + "AND "
            + "weapon = :weapon", 
        CreatureTypeWeapon.class);

    CreatureType creatureTypePersisted = 
        em.getReference(CreatureType.class, creatureType.getId());
    Weapon weaponPersisted = em.getReference(Weapon.class, weapon.getId());

    query.setParameter("creatureType", creatureTypePersisted);
    query.setParameter("weapon", weaponPersisted);
    return query.getSingleResult();

获取似乎没问题,但是当我要在删除方法中删除它时,就像 em.remove(creatureTypeWeapon); (根据建议)我收到此错误:

javax.persistence.NoResultException: No entity found for query at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:291)
at cz.muni.fi.pa165.creatures.dao.impl.CreatureTypeWeaponDAOImpl.get(CreatureTypeWeaponDAOImpl.java:101)

这是我测试它的一种方式:

public void testRemoveCreatureTypeWeapon() {

    Weapon w = new Weapon("ak-47");
    weaponDAO.create(w);

    CreatureType ct = new CreatureType("alien", 200);
    creatureTypeDAO.create(ct);

    assertNotNull(weaponDAO.get(w.getId()));
    assertNotNull(creatureTypeDAO.get(ct.getId()));

    CreatureTypeWeapon ctw = new CreatureTypeWeapon();
    ctw.setCreatureType(ct);
    ctw.setWeapon(w);
    ctw.setEffectivity(new Float(0.5));
    creatureTypeWeaponDAO.create(ctw);

    CreatureTypeWeapon ctw2 =
            creatureTypeWeaponDAO.get(ctw.getCreatureType(), ctw.getWeapon());

    ctw2.setCreatureType(ctw.getCreatureType());
    ctw2.setWeapon(ctw.getWeapon());

    creatureTypeWeaponDAO.remove(ctw);

    assertNull(creatureTypeWeaponDAO.get(ctw2.getCreatureType(), ctw2.getWeapon()));
} 

更新#2

好的,我明白了,出现这种错误的原因是,当 query.getSingleResult() 抛出 NoResultException 时,这意味着当我在测试中删除实体后尝试获取实体时,没有这样的记录,因此该方法会抛出一个执行,这很好。我是这样处理的:

try {
    return query.getSingleResult();
} catch(NoResultException ex) {
    return null;
}

这样做之后,可以正常删除它,(甚至在它之前也是可行的,但我很困惑并认为错误在那里)。

4

2 回答 2

0

你不能直接删除CreatureTypeWeapon实体吗?我认为您不需要进行find,只需删除CreatureTypeWeapon实体即可删除与CreatureTypeand的关联Weapon

public class CreatureTypeWeaponDAOImpl implements CreatureTypeWeaponDAO {
    void remove(CreatureTypeWeapon arg) {
        /*Some init codes here*/
        session.delete(arg);
    }
}
于 2012-10-31T17:26:45.137 回答
0

您需要定义一个新表,它是表CreatureTypeWeapon. 例如:

@Entity
public class CreatureTypeWeapon implements Serializable {

    @Id
    @ManyToOne
    @PrimaryKeyJoinColumn(name = "creature_id", referencedColumnName = "id")
    private CreatureType creatureType;

    @Id
    @ManyToOne
    @PrimaryKeyJoinColumn(name = "weapon_id", referencedColumnName = "id")
    private Weapon weapon;
}

然后,您可以按如下方式定义您的查询:

按生物查找

"SELECT CreatureTypeWeapon WHERE creatureType = ?"

按武器查找

"SELECT CreatureTypeWeapon WHERE weapon = ?"

按生物和武器查找

"SELECT  CreatureTypeWeapon WHERE creatureType = ? AND weapon = ?"

删除

em.delete(creatureTypeWeapon);
于 2012-10-31T17:27:27.960 回答