我有两个名为“地址和客户”的实体。 我有一个界面看起来像:
public interface IDAOEnitityFactory extends Serializable{
List<Address> findAllByClass();
}
实现如下:
@Stateless
@Local(IDAOEnitityFactory.class)
public class DAOEnitityFactory implements IDAOEnitityFactory {
@PersistenceContext(unitName = "MY_PU")
protected EntityManager entityManager;
@Override
public List<Address> findAllByClass() {
return this.getAuditReader().createQuery()
.forRevisionsOfEntity(Address.class, true, false)
.setMaxResults(10).getResultList();
}
所以我想做一个通用的实现,我只传递Adress或Client和findAllByClass返回它们。 我怎么能这样做?
我有一个豆子:
@EJB
private IDAOEnitityFactory dao;
for(Address st:dao.findAllByClass()){
System.out.println(st.getID);
}
答案 0 :(得分:0)
我认为我找到了解决方案:
public interface IDAOEnitityFactory extends Serializable{
T List<T> findAllByClass(Class c);
}
@Stateless
@Local(IDAOEnitityFactory.class)
public class DAOEnitityFactory implements IDAOEnitityFactory {
@PersistenceContext(unitName = "MY_PU")
protected EntityManager entityManager;
@Override
public List<T> findAllByClass(Class c) {
return this.getAuditReader().createQuery()
.forRevisionsOfEntity(c, true, false)
.setMaxResults(10).getResultList();
}
@EJB
private IDAOEnitityFactory dao;
for(Object st:dao.findAllByClass(Address.class)){
System.out.println((Address)st)st.getID);
}
也许还有其他解决方案。母鸡?