我有一個這樣的簡單的界面。Java通用類型與擴展和T使用休眠
public interface EntityClass
{
public void setId(final Integer id);
public Integer getId();
}
和一類
public Student implements EntityClass
我想代碼使用泛型類型T
一類,但仍然是一個的instanceof EntityClass
a EntityClass subclass
這樣的事情。
public abstract class CustomizableQueriesForClazzAndFields<T extends EntityClass>
這是該類中的項目,我想要一個像這樣的方法。
public List<T>getMultipleClazzWithFields(final String ...fields){return of(genericType).retrieve(fields);}
private MyCriteria of(final Class<? extends EntityClass>applyToClazz){........}
MyCriteria是org.hibernate.Criteria的包裝。
而在我的實現會做這樣的事情。
public final class TestCustom extends CustomizableQueriesForClazzAndFields<Student>
{
public static void main(String[] args)
{
final TestCustom clazz = new TestCustom();
final List<Student>a=clazz.getMultipleClazzWithFields("id","name","phone");
a.stream().forEach(clazz::consumer);
}
}
這樣做代碼我想是表示像這樣使用泛型。
public List<Student>getMultipleClazzWithFields(final String ...fields){return of(Student.class).retrieve(fields);}
但我不知道如何表示這個或如果它是可能的。
public List<T>getMultipleClazzWithFields(final String ...fields){return of(genericType).retrieve(fields);}
哪些genericType應該把它表示Student
相同Student
爲
public final class TestCustom extends CustomizableQueriesForClazzAndFields<Student>
,或者代表它作爲
public final class TestCustom extends CustomizableQueriesForClazzAndFields<School>
,並會做這樣的事情
public List<School>getMultipleClazzWithFields(final String ...fields){return of(School.class).retrieve(fields);}
因爲我是這樣潛水
public List<T>getMultipleClazzWithFields(final String ...fields){return of(T).retrieve(fields);}
但編譯器踢我說。
cannot find symbol
symbol: variable T
location: class CustomizableQueriesForClazzAndFields<T>
where T is a type-variable:
T extends EntityClass declared in class CustomizableQueriesForClazzAndFields
----
(Alt-Enter shows hints)
我也嘗試
public abstract class CustomizableQueriesForClazzAndFields<Class<T extends EntityClass>>
但是說。
> expected
----
(Alt-Enter shows hints)
或者我如何完成我的願望代碼..任何workaorund。
因爲我是一種煩人的傳遞參數。
UPDATE
所有我想要做的是傳遞的EntityClass
一個子類,並創建。
session.createCriteria(genericType);
除了給我的名單,但我不知道是否有可能趕上或捕獲泛型類型通CustomizableQueriesForClazzAndFields<Student>
並傳遞給of()
方法,它是誰,因爲在創建org.hibernate.Criteria object
我不能只傳遞一個T
值我圖想成爲一個EntityClass
不是一個子類只是一個raw T value.
我需要趕上或捕獲
CustomizableQueriesForClazzAndFields<Student>
Student generic type.
如果這可能或者我夢見我可憐的英語感到抱歉。
感謝了很多,從委內瑞拉的問候..
我不,如果十分確定我得到你的問題在哪裏,但爲什麼你使用'Class <?擴展EntityClass>'而不是簡單地使用T?是否有可能給予更多的在線人作爲你想要達到的目標? – wastl 2015-03-02 13:39:33
pleasee我編輯的問題... – chiperortiz 2015-03-02 14:19:59