2017-02-19 49 views
-1

所以,你好堆棧溢出。 我有麻煩hibrnate/JPA,得到這個異常:使用@OneToMany或@ManyToMany定位未映射的類:

org.hibernate.AnnotationException:使用@OneToMany或@ManyToMany 針對未映射類: com.vlad9pa.springapp.entity.User。角色[com.vlad9pa.springapp.entity.Role]

我有3個表:

  1. 用戶:ID,用戶名,密碼;
  2. 角色:id,name;
  3. user_roles:user_id,roles_id。

導入:

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> 

+0

你檢查[這](http://stackoverflow.com/questions/4956855/hibernate-problem -use-of-onetomany-or-manytomany-targeting-an-unmapped-clas) –

+0

是的,沒有幫助 – Vlad9pa

+0

只是100%確定..你可以添加實體類的所有進口嗎? –

回答

0

所以,我剛剛創建新的項目和重寫整個代碼。你知道嗎?它的工作(也許是因爲我用另一個框架不休眠 - Spring JPA)。相同的代碼...

0

因爲我使用了一個HibernateUtil類,我通過添加這一行來解決這個問題「configuration.addAnnotatedClass(Role.class);」 (你應該加入這一行你在你的模型有每一個類,否則你會得到「未映射的錯誤」)

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; 
     } 
} 
相關問題