日期:2014-05-20  浏览次数:21197 次

SQLGrammarException:could not initialize a collection:
hibernate异常:org.hibernate.exception.SQLGrammarException: could not initialize a collection: could not initialize a collection: [com.net.sys.entity.Net_User.Net_Roles#1] 

问题是这样的,我在做一个登陆模块,开始没有设置多对多关系的时候一切正常,可以正常登录,但是在.hbm.xml设置了many to many 关系之后,就会出现这个异常,奇怪的是控制台的执行语句为:Hibernate: select net_roles0_.user_id as user1_1_, net_roles0_.role_id as role2_1_, net_role1_.id as id2_0_, net_role1_.role_name as role2_2_0_ from Net_RoleToUser net_roles0_, Net_role net_role1_ where net_roles0_.role_id=net_role1_.id(+) and net_roles0_.user_id=?  亮点是(+)

下面贴出文件,按照entity ,action  ,service ,dao 的顺序:

Net_User ----用户的实体类---很有可能是问题的出现所在

public class Net_User implements java.io.Serializable {

// Fields

private Integer id;
private String username;
private String password;
private Set  Net_Roles = new HashSet(0);


// Constructors

/** default constructor */
public Net_User() {
}

/** full constructor */
public Net_User(String username, String password) {
this.username = username;
this.password = password;
this.Net_Roles=Net_Roles;
}

// Property accessors

public Set<Net_Role> getNet_Roles() {
return Net_Roles;
}

public void setNet_Roles(Set netRoles) {
Net_Roles = netRoles;
}

public Integer getId() {
return this.id;
}

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

public String getUsername() {
return this.username;
}

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

public String getPassword() {
return this.password;
}

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

}



Net_Role----权限的实体类,这个类应该不是问题的关键

public class Net_Role implements java.io.Serializable {

// Fields

private Integer id;
private String roleName;
private Set<Net_User> Net_Users = new HashSet(0);

// Constructors

/** default constructor */
public Net_Role() {
}

/** full constructor */
public Net_Role(String roleName) {
this.roleName = roleName;
this.Net_Users=Net_Users;
}

// Property accessors

public Set<Net_User> getNet_Users() {
return Net_Users;
}

public void setNet_Users(Set<Net_User> netUsers) {
Net_Users = netUsers;
}

public Integer getId() {
return this.id;
}

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

public String getRoleName() {
return this.roleName;
}

public void setRoleName(String roleName) {
this.roleName = roleName;