내 응용 프로그램에 대한 일반 데이터 액세스 계층을 쓰려고합니다. 그 실체에 대한일반 및 Java : "확장"은 어떻게 작동합니까?
public abstract class Entity1 {
// some implementation
}
public class Entity2 extends Entity1 {
// some implementation
}
public class Entity3 extends Entity1 {
// some implementation
}
의 DAO는 제외 (대부분 동일합니다 : 나는 (그들이 최대 절전 모드에서 계층 구조로 구현되지 않음) 배수가 대부분 동일하고 자바의 클래스 계층 구조로 표현되는 엔티티를 최대 절전 모드가 메소드의 타입 시그니처, 그리고 Hibernate에 요청 된 클래스). 나는이 같은 일반적인 DAO를 쓸 수 있도록하고 싶습니다 : 정말 좋은
public interface EntityDao<T extends Entity1> {
List<T>getEntities();
void saveOrUpdateEntity(T entity);
}
public class EntityDaoImpl implements EntityDao<Entity1> {
private final HibernateTemplate hibernateTemplate;
private final Class<DBEnumerationDto> clazz;
public DBEnumerationDaoHibernateImpl(SessionFactory sessionFactory, Class<DBEnumerationDto> clazz) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
this.clazz = clazz;
}
@Override
public List<Entity1> getEntities() {
return this.hibernateTemplate.loadAll(this.clazz);
}
@Override
public void saveOrUpdateEntity(Entity1 entity) {
this.hibernateTemplate.saveOrUpdate(entity);
}
}
지금까지. 이를 사용하는 경우 그러나 문제는 발생 :이 질문은 Java Generics: casting to ? (or a way to use an arbitrary Foo<?>) 관련 추측 The method saveOrUpdateEntity(capture#5-of ? extends Entity1) in the type EntityDao<capture#5-of ? extends Entity1> is not applicable for the arguments (Entity1)
하지만 난 정말 어떤 방법으로 파악하지 못할 :
Entity1 test = new Entity1();
Entity1Dao<? extends Entity1> dao = ...; // get the dao forthe current operation
dao.saveOrUpdate(test);
이것은 컴파일러 오류를 제공합니다.
코드를 어떻게 수정해야합니까? 아니면 내 접근 방식이 잘못 되었나요?
추상 클래스 대신 구체적인 클래스를 저장하려고 했습니까? –