2012-10-31 3 views
0

필자는 CreatureType and Weapon (어떤 생물에 대해 사용할 수있는 가장 좋은 무기) 클래스를 가지고 있으며 무기가 특정 CreatureType에 대해 다양한 효율성을 갖는다는 사실을 모델링해야합니다. 그래서 나는 효율성 속성을 가진 연합 실체 인 CreatureTypeWeapon을 만들었습니다. 속성이있는 연관 엔터티의 DAO 클래스 - 구현 방법

나는 http://uaihebert.com/?p=1674&page=22

나는이 질문을하고 이유는 내가 연결 실체에 대한 DAO 클래스를 구현하는 방법을 모르겠입니다 (원칙은 동일)이 방법으로 코딩.

나는 CreatureType을위한 DAO 클래스를 가지고 있으며 CRUD 연산을 사용하는 인터페이스를 구현하는 Weapon을위한 DAO 클래스를 가지고있다.

그러나 나는 어떻게 든 법인에 대한 CRUD 작업을 처리 할 방법이 없다.

// 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); 

하지만 문제는, 어떻게 그것을 구현하는 것입니다 :의 내가

public class CreatureTypeWeaponDAOImpl implements CreatureTypeWeaponDAO { 

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

내가 이런 식으로 사용이 DAO 있다고 가정 해 봅시다? 나는 일반적으로 그것을 유지하지만, 이제 나는이 관계를 제거하기 위하여려고하고있다 가정 해 봅시다 수 있어요 :이이

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

일반적으로 다음과 같이 구현 될 수

dao.remove(creatureTypeWeapon); 

, 내가 넣어

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

하지만 제휴 엔티티에 "id"가 없습니다 ....

UPDATE

확인이 내가 CreatureTypeWeaponDAOImpl

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(); 

얻기에 CreatureTypeWeapon 인스턴스를 얻고 어떻게 방법은 괜찮을 것 같지만, 그래서 나는 한 간단하게 제거 방법으로 제거하기 위하여려고하는 경우 em.remove (creatureTypeWeapon);

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

좋아, 내가 그것을 가지고, 이유 : 이것은 내가 그것을 테스트입니다 방법 방법입니다

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) 

: (권고로)이 오류를 얻고있다 거기에 그런 오류가 있었는데 그 때 Query.getSingleResult() NoResultException 던져, 그것은 그 엔티티가 테스트에서 제거한 후, 그 레코드가 없다는 것을 의미합니다, 그 방법은 좋은 실행을 throw합니다. 나는이처럼 처리 :

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

이렇게 후에는, (심지어 그 전에 행할했지만, 난 혼란스러워하고 에러가 발생했을 생각) 일반적으로 제거 할 수 있습니다.

답변

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); 
+0

업데이트를 참조하십시오 삭제 – stewenson

0

CreatureTypeWeapon 엔티티를 삭제할 수 없습니까?나는 을 찾을 필요가 없다고 생각합니다.을 찾고, CreatureTypeWeapon 엔티티를 삭제하면 CreatureTypeWeapon과 연관이 제거됩니다.

public class CreatureTypeWeaponDAOImpl implements CreatureTypeWeaponDAO { 
    void remove(CreatureTypeWeapon arg) { 
     /*Some init codes here*/ 
     session.delete(arg); 
    } 
}