使用@OneToMany或@ManyToMany定位未映射的类:

时间:2017-02-19 20:10:40

标签: java hibernate

所以,你好堆栈溢出。 我遇到了hibrnate / jpa的麻烦,得到了这个例外:

  

org.hibernate.AnnotationException:使用@OneToMany或@ManyToMany   定位未映射的类:   com.vlad9pa.springapp.entity.User.roles [com.vlad9pa.springapp.entity.Role]

我有3张桌子:

  1. 用户:id,username,password;
  2. 角色:id,name;
  3. user_roles:user_id,roles_id。
  4. 导入:

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

    这是我的实体和hibernate.cfg.xml: 用户:

    @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;
    }
    

    作用:

    @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>
    

1 个答案:

答案 0 :(得分:1)

由于我使用的是HibernateUtil类,因此我通过添加此行来解决了这个问题&#34; configuration.addAnnotatedClass(Role.class);&#34; (你应该为模型中的每个类添加这一行,否则你会得到&#34;未映射的错误&#34;)

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;
        }
}