Reputation: 998
I have the followin abstract class...
public abstract class IRepository<T> {
public T getEntityById(int idEntity){
try{
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
T result = (T) session.createQuery( getByIdQuery() + idEntity ).uniqueResult();
tx.commit();
if (result != null){
System.out.println("Fetched " + result.toString());
return result;
}
else return null;
}
catch (Exception e){
// TO DO : logging
handleException(e);
return null;
}
}
And another class, that inherits from this one...
public class ProductRepository extends IRepository<Product> {
public ProductRepository(){
}
}
When I do the following call from a main class, I'm getting an error...
ProductRepository prodRep = new ProductRepository();
Product result = prodRep.getEntityById(111);
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types
required: gestor.model.Product
found: java.lang.Object
My question is.. why is this happening? isn't my getEntityById returning something of type T, whichi in this case should be products?
I'm working in Netbeans, and no error is shown in Compile time.
Thank you for the help =)
Upvotes: 3
Views: 1058
Reputation: 16037
I would suspect an error in your imports or configuration. In Eclipse, you can "Clean" a project to purge all binaries and rebuild; try the equivalent in Netbeans.
The following code works fine for me, and it seems to be a direct simplification of your problem:
// Test.java
public abstract class Test<T> {
public T get(int i) {
return null;
}
public static void main(String[] args) {
StringTest st = new StringTest();
String s = st.get(0);
System.out.println(s); // prints: null
}
}
class StringTest extends Test<String> {
public StringTest() { }
}
Upvotes: 3