0
public class Test {

public static void main(String[] args) throws Exception 
{
    Generic<Customer> x = new Generic(Customer.class);
    Customer cust = x.get("1");
    System.out.println(cust.getFullname());
}


class Generic<T> {
private Class<T> clazz;

public Generic(Class<T> clazz) {
    this.clazz = clazz;
}

public  Session openSession() {
    Configuration config = new AnnotationConfiguration().configure();
    SessionFactory sf = config.buildSessionFactory();
    Session session = sf.openSession();
    return session;
}

public T get(String id) {
    Session session = openSession();
    T t = (T) session.get(clazz.getClass(), id);
    return t;
}

}

When i run this program it dont except my Customer.class(Customer is just an entity class) and throw exception
org.hibernate.MappingException: Unknown entity: java.lang.Class.Please explain me why

1 Answers1

0

You need to pass an additional Class object of type Class<T>.

Edit: I may be wrong, haven't used Hibernate for too long. Some versions of get() require a Class object.

Either way, look up the definition of Session.get here.

Stefan Reich
  • 1,000
  • 9
  • 12