I've searched for this exception before. But no solution worked for my problem.
I have two classes:
@Entity
@Table(name = "user")
public class User {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@OneToMany
@JoinColumn(name = "tutor_id")
private Set<Tutorium> tutoria;
}
and
@Entity
@Table(name = "tutorium")
public class Tutorium {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Colum(name = "tutor_id")
private int tutor_id;
}
So the semantic is "One user could teach many tutoria".
But when I'm starting my application, I get the exception:
"Failed to create sessionFactory object.org.hibernate.AnnotationException: Use of
@OneToMany
or@ManyToMany
targeting an unmapped class: model.User.tutoria[model.Tutorium]".
Does anyone know, how I can solve this problem?
Edit:
Trying to give you a small example of how I'm accessing the classes:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
public class UserFilter {
private static SessionFactory factory;
public UserFilter() {
try{factory = new Configuration().configure().addAnnotatedClass(User.class).buildSessionFactory();}
catch(Exception e) {}
}
public Collection<User> getUser() {
Session session = factory.openSession();
Transaction t = null;
try{
t = session.beginTransaction();
CriteriaBuilder cb = session.getEntityManagerFactory().getCriteriaBuilder();
Join<User, Tutorium> tutoria = this.root().join("tutoria");
CriteriaQuery output = cb.createQuery(User.class);
output.groupBy(this.root().get("id"));
TypedQuery q = session.getEntityManagerFactory().createEntityManager().createQuery(output);
List users = q.getResultList();
return users;
}
catch(Exception e) {}
finally {
session.close();
}
}
}
The part with the join and the groupBy-clause isn't tested yet. That was the function, I wanted to implement next. But before I was able to do this, the exception was thrown.
I hope, the example is mostly clear.