2014-06-15 1 views
1

GenericDao를 구현 중입니다. 나는 getAll()과 getById (Long id)라는 두 가지 메소드에 문제가있다. 엔티티 클래스는 null 값을 갖는다. 클래스가 설정되지 않은 것 같습니다. 어떻게이 문제를 해결할 수 있습니까?GenericDao, Class <T>이 null입니다.

@Repository 
public class GenericDaoImpl<T> implements GenericDao<T> { 

private Class<T> clazz; 

@Autowired 
SessionFactory sessionFactory; 

public void setClazz(final Class<T> clazzToSet) { 
    this.clazz = clazzToSet; 
} 

public T getById(final Long id) { 
    return (T) this.getCurrentSession().get(this.clazz, id); 
} 

public List<T> getAll() { 

    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
      this.clazz); 
    return criteria.list(); 

} 
    protected final Session getCurrentSession() { 
    return this.sessionFactory.getCurrentSession(); 
    } 
} 

PersonDao

public interface PersonDao extends GenericDao<Person> { } 

PersonDaoImpl

@Repository("PersonDAO") 
public class PersonDaoImpl extends GenericDaoImpl<Person> implements PersonDao {} 

서비스 :

@Service 
public class PersonServiceImpl implements PersonService { 

    @Autowired 
    private PersonDao personDao; 


@Transactional 
public List<Person> getAll() { 

    return personDao.getAll(); 
} 


@Transactional 
public Person getById(Long id) { 
    return personDao.getById(id); 
} 
} 

답변

2

당신은 PersonDaoclazz 속성을 설정해야합니다. 이는 @PostConstruct 주석을 사용하여 post initialization callback을 선언하여 수행 할 수 있습니다.

@Repository("PersonDAO") 
public class PersonDaoImpl extends GenericDaoImpl<Person> implements PersonDao { 

     @PostConstruct 
     public void init(){ 
     super.setClazz(Person.class); 
     } 
} 
+0

감사합니다. – kxyz

+0

@kxyz 기꺼이 도와 드리겠습니다. –

관련 문제