gpt4 book ai didi

java - ManyToMany 的 MappingException

转载 作者:行者123 更新时间:2023-11-29 05:52:22 26 4
gpt4 key购买 nike

我正在使用 JBoss AS 7 和 IDEA 11。当我尝试部署我的应用程序时遇到了这个问题:

我的应用中有两个实体:

首先:

@Entity
@Table(name="users")
public class User extends AbstractEntity {

private static final long serialVersionUID = -3839809902040717567L;

@Column(name="email") private String email;
@Column(name="first_name") private String firstName;
@Column(name="last_name") private String lastName;
@Column(name="middle_name") private String middleName;
@Column(name="password") private String password;
@Column(name="client_id") private int clientId;
@Column(name="user_name") private String userName;
private List<Role> roles;

public User() {
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getMiddleName() {
return middleName;
}

public void setMiddleName(String middleName) {
this.middleName = middleName;
}

public String getPassword() {
return password;
}

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

public int getClientId() {
return clientId;
}

public void setClientId(int clientId) {
this.clientId = clientId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name = "users_roles",
joinColumns = {@JoinColumn(name = "user_id")},
inverseJoinColumns = @JoinColumn(name = "role_id"))
public List<Role> getRoles() {
return roles;
}

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

第二个:

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

private static final long serialVersionUID = -192049947690313026L;

@Column(name="ad_name") private String adName;
@Column(name="description") private String description;
@Column(name="name") private String name;
@OneToMany(mappedBy = "roles") private List<RolesPermissions> rolesPermissions;
@ManyToMany(mappedBy = "roles") List<User> users;


public String getAdName() {
return adName;
}

public void setAdName(String adName) {
this.adName = adName;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getName() {
return name;
}

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

public List<User> getUsers() {
return users;
}

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

public List<RolesPermissions> getRolesPermissions() {
return rolesPermissions;
}

public void setRolesPermissions(List<RolesPermissions> rolesPermissions) {
this.rolesPermissions = rolesPermissions;
}

}

它们有ManyToMany 关系。

部署时出现异常:

Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: users, for columns: [org.hibernate.mapping.Column(roles)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:304)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:288)
at org.hibernate.mapping.Property.isValid(Property.java:216)

可能是什么原因?

附言AbstractEntitymappedsuperclass 并且具有带有 @Id 注释的字段。

最佳答案

您的 User 实体没有注释 roles 关系。 @ManyToMany 注释应该可以解决问题。

正如@Perception 指出的那样,您可能还想在角色 上分配连接表,例如:

@JoinTable(name="USER_ROLE", joinColumns= @JoinColumn(name="EMP_ID"), inverseJoinColums = @JoinColumn(name="ROLE_ID"))

编辑:此注释仅在您使用现有架构或默认表名和列名不适合您时才相关。如果让 Hibernate 生成表,则可以安全地省略 @JoinTable 注释。

关于java - ManyToMany 的 MappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13419080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com