Reputation: 77
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.
Upvotes: 0
Views: 3827
Reputation: 502
You need to map reverse way e.g. if User
is parent table and Tutorium
is child table you will put mappedBy
attribute in User
class and for child table Tutorium
you will use @JoinColumn
which will refer to foreign key column in parent class. See code below
@Entity
@Table(name = "user")
public class User {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@OneToMany(mappedBy = "user")
private Set<Tutorium> tutoria;
}
@Entity
@Table(name = "tutorium")
public class Tutorium {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@ManyToOne
@JoinColumn(name = "id")
private User user;
}
Upvotes: -1
Reputation: 5087
In this line:
try{factory = new Configuration().configure().addAnnotatedClass(User.class).buildSessionFactory();}
You are telling Hibernate that User
is a mapped class. That is the one and only class you tell it about, so that's the one and only class that it examines for mapping info. If you want it to also treat Tutorium
as a mapped class, you need to tell it about that too. Simply adding .addAnnotatedClass(Tutorium.class)
into the configuration call chain should do it.
Upvotes: 4