所以,你好堆棧溢出。 我有麻煩hibrnate/JPA,得到這個異常:使用@OneToMany或@ManyToMany定位未映射的類:
org.hibernate.AnnotationException:使用@OneToMany或@ManyToMany 針對未映射類: com.vlad9pa.springapp.entity.User。角色[com.vlad9pa.springapp.entity.Role]
我有3個表:
- 用戶:ID,用戶名,密碼;
- 角色:id,name;
- 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>
你檢查[這](http://stackoverflow.com/questions/4956855/hibernate-problem -use-of-onetomany-or-manytomany-targeting-an-unmapped-clas) –
是的,沒有幫助 – Vlad9pa
只是100%確定..你可以添加實體類的所有進口嗎? –