论坛首页 入门技术论坛

解决办法could not initialize a collection

浏览 6048 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-12-24  
错误信息
SQLGrammarException: could not initialize a collection
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]对象名 'EmployeeRole' 无效。


Role Class

package com.many_to_many_s;

import java.util.HashSet;
import java.util.Set;

/**
* Role entity.
*
* @author MyEclipse Persistence Tools
*/

public class Role implements java.io.Serializable {

// Fields

private String roleId;
private String roleName;

private Set employees = new HashSet(0);



public Set getEmployees() {
return employees;
}

public void setEmployees(Set employees) {
this.employees = employees;
}

// Property accessors

public String getRoleId() {
return this.roleId;
}

public void setRoleId(String roleId) {
this.roleId = roleId;
}

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

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

}

role.hbm.xml 中

<hibernate-mapping package="com.many_to_many_s">
    <class name="Role" table="Role" schema="dbo" catalog="mypro">
        <id name="roleId" type="java.lang.String">
            <column name="roleID" length="50" />
            <generator class="uuid.hex" />
        </id>
        <property name="roleName" type="java.lang.String">
            <column name="roleName" length="50" />
        </property>
       
       
   <set name="employees" table="EmployeeRole" >
        <key column="roleID"></key>
        <many-to-many column="operatorID" class="Employee"  ></many-to-many>
        </set>
    </class>
</hibernate-mapping>


Employee Class

package com.many_to_many_s;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
* Employee entity.
*
* @author MyEclipse Persistence Tools
*/

public class Employee implements java.io.Serializable {

// Fields

private String operatorId;
private String employeeName;
private String password;
private String operatorName;
private Date birthDate;
private String orgId;
private Date ragDate;
private Date createTime;
private Date lastModifyTime;
private String status;

private Set roles = new HashSet(0);


// Property accessors

public String getOperatorId() {
return this.operatorId;
}

public void setOperatorId(String operatorId) {
this.operatorId = operatorId;
}

public String getEmployeeName() {
return this.employeeName;
}

public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}

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

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

public String getOperatorName() {
return this.operatorName;
}

public void setOperatorName(String operatorName) {
this.operatorName = operatorName;
}

public Date getBirthDate() {
return this.birthDate;
}

public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}

public String getOrgId() {
return this.orgId;
}

public void setOrgId(String orgId) {
this.orgId = orgId;
}

public Date getRagDate() {
return this.ragDate;
}

public void setRagDate(Date ragDate) {
this.ragDate = ragDate;
}

public Date getCreateTime() {
return this.createTime;
}

public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

public Date getLastModifyTime() {
return this.lastModifyTime;
}

public void setLastModifyTime(Date lastModifyTime) {
this.lastModifyTime = lastModifyTime;
}

public String getStatus() {
return this.status;
}

public void setStatus(String status) {
this.status = status;
}

public Set getRoles() {
return roles;
}

public void setRoles(Set roles) {
this.roles = roles;
}

}

Employee.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping package="com.many_to_many_s">
    <class name="Employee" table="Employee" schema="dbo" catalog="mypro">
        <id name="operatorId" type="java.lang.String">
            <column name="operatorID" length="50" />
            <generator class="uuid.hex" />
        </id>
        <property name="employeeName" type="java.lang.String">
            <column name="employeeName" length="50" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" length="50" />
        </property>
        <property name="operatorName" type="java.lang.String">
            <column name="operatorName" length="50" />
        </property>
        <property name="birthDate" type="java.util.Date">
            <column name="birthDate" length="23" />
        </property>
        <property name="orgId" type="java.lang.String">
            <column name="orgID" length="50" />
        </property>
        <property name="ragDate" type="java.util.Date">
            <column name="ragDate" length="23" />
        </property>
        <property name="createTime" type="java.util.Date">
            <column name="createTime" length="23" />
        </property>
        <property name="lastModifyTime" type="java.util.Date">
            <column name="lastModifyTime" length="23" />
        </property>
        <property name="status" type="java.lang.String">
            <column name="status" length="50" />
        </property>
         <set name="roles" table="EmployeeRole">
        <key column="operatorID"></key>
        <many-to-many column="roleID" class="Role"  ></many-to-many>
        </set>
       
       
    </class>
</hibernate-mapping>


解决办法

修改hibernage.cfg.xml
<property name="connection.url">
jdbc:microsoft:sqlserver://localhost:1433
</property>

<!-- 加入指定的数据库名-->
<property name="connection.url"> jdbc:microsoft:sqlserver://localhost:1433;databasename=mypro
</property>

Pojo类的代码不需要改动
而hbm.xml中修改这里即可
Role.hbm.xml

<hibernate-mapping package="com.many_to_many_s">
    <class name="Role" table="Role" schema="dbo" catalog="mypro">
        <id name="roleId" type="java.lang.String">
改为
<hibernate-mapping package="com.many_to_many_s">
    <class name="Role" table="Role" >
        <id name="roleId" type="java.lang.String">

<!--去掉schema="dbo" catalog="mypro" -->


改为

Employee.hbm.xml

<hibernate-mapping package="com.many_to_many_s">
    <class name="Employee" table="Employee" schema="dbo" catalog="mypro">
        <id name="operatorId" type="java.lang.String">

<!--去掉schema="dbo" catalog="mypro" -->

改为

<hibernate-mapping package="com.many_to_many_s">
    <class name="Employee" table="Employee" >
        <id name="operatorId" type="java.lang.String">

原因解释

这是从Employee端查询其对应的Role

select
        employees0_.roleID as roleID1_,
        employees0_.operatorID as operatorID1_,
        employee1_.operatorID as operatorID3_0_,
        employee1_.employeeName as employee2_3_0_,
        employee1_.password as password3_0_,
        employee1_.operatorName as operator4_3_0_,
        employee1_.birthDate as birthDate3_0_,
        employee1_.orgID as orgID3_0_,
        employee1_.ragDate as ragDate3_0_,
        employee1_.createTime as createTime3_0_,
        employee1_.lastModifyTime as lastModi9_3_0_,
        employee1_.status as status3_0_
    from
        EmployeeRole employees0_   <------------注意这里 没有指定EmployeeRole属于的数据库名称,所有就会出现
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]对象名 'EmployeeRole' 无效。

    left outer join
        mypro.dbo.Employee employee1_
            on employees0_.operatorID=employee1_.operatorID
    where
        employees0_.roleID=?

   发表时间:2008-12-24  
connection url 中写不是很爽,每种数据的写法可能不一样。
还是习惯在cfg.xml中用default_schema。
至于hbm.xml里的schema,特殊情况才用上,一般不用。
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics