-1

So, hello stack overflow. I've trouble with hibrnate/jpa, got this exception:

org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.vlad9pa.springapp.entity.User.roles[com.vlad9pa.springapp.entity.Role]

I have 3 tables:

  1. users:id,username, password;
  2. roles:id, name;
  3. user_roles:user_id, roles_id.

Import:

import javax.persistence.*;
import java.util.Set;

Here is my entities and hibernate.cfg.xml: User:

@Entity
@Table(name = "users")
public class User {
private int id;
private String username;
private String password;
private Set<Role> roles;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

@Basic
@Column(name = "username")
public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

@Basic
@Column(name = "password")
public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Role.class)
@JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"),
        inverseJoinColumns = @JoinColumn(name = "role_id"))
public Set<Role> getRoles() {
    return roles;
}

public void setRoles(Set<Role> roles) {
    this.roles = roles;
}

Role:

@Entity
@Table(name = "roles")
public class Role {

private int id;
private String name;

private Set<User> users;

@Id
@Column(name = "id")
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

@Basic
@Column(name = "name")
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}



@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "roles")
public Set<User> getUsers() {
    return users;
}

public void setUsers(Set<User> users) {
    this.users = users;
}

hibernate.cfg.xml:

 <hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/e_shop</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="show_sql">false</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <mapping class="com.vlad9pa.springapp.entity.Role"/>
    <mapping class="com.vlad9pa.springapp.entity.User"/>
</session-factory>

Vlad9pa
  • 13
  • 2
  • 7

1 Answers1

1

Since I use an HibernateUtil class I solved this issue by adding this line "configuration.addAnnotatedClass(Role.class);" (you should add this line for every class you have in your model otherwise you get "unmapped error")

public class HibernateUtil {
        private static SessionFactory sessionFactory;
        private static ServiceRegistry serviceRegistry;
        static {
            try {
                Configuration configuration = new Configuration().configure();
                configuration.addAnnotatedClass(User.class);
                configuration.addAnnotatedClass(Role.class);

                serviceRegistry = new StandardServiceRegistryBuilder()
                        .applySettings(configuration.getProperties()).build();
                sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            } catch (HibernateException e) {
                System.out.println(e);
            }
        }

        public static SessionFactory getSessionFactory(){
            return sessionFactory;
        }
}
yasin
  • 270
  • 4
  • 6
  • Well, 1.5 years delay. Thanks for your question. Really appreciate your answer. Now I know a lot about java and hibernate. – Vlad9pa Aug 29 '18 at 17:09