`
SeaAim
  • 浏览: 54306 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

解决办法could not initialize a collection

阅读更多
错误信息
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=?

分享到:
评论
1 楼 gembler 2008-12-24  
connection url 中写不是很爽,每种数据的写法可能不一样。
还是习惯在cfg.xml中用default_schema。
至于hbm.xml里的schema,特殊情况才用上,一般不用。

相关推荐

    Plsql 12连接Oracle时出现Could not initialize oci.dll解决方案 oracle 客户端

    以下是一些解决“Could not initialize oci.dll”问题的步骤: 1. **下载Oracle Instant Client**: 首先,你需要从Oracle官方网站下载适合你的操作系统的Oracle Instant Client版本。在这个案例中,我们看到的...

    Could not initialize class net.sf.jasperreports.engine.util.JRStyledTextParser

    总之,解决"Could not initialize class net.sf.jasperreports.engine.util.JRStyledTextParser"这类问题需要对项目结构、依赖关系和Java运行时环境有深入的理解。通过细致的排查和测试,一般都可以找到问题的根源并...

    oracle_plsql连服务端时 Initialization error could initialize错误处理方法

    解决:oracle_plsql连服务端时 Initialization error could initialize错误处理方法

    Plsql Developer连接Oracle时出现Could not initialize oci.dll解决方案

    总的来说,解决“Could not initialize oci.dll”问题的关键在于确保PL/SQL Developer与Oracle客户端的位数匹配,并正确配置环境变量。完成上述步骤后,应该可以顺利连接到远程Oracle数据库。在日常工作中,记录并...

    解决hbase client在windows环境下报NoClassDefFound问题

    解决hbase client在windows环境下报Could not initialize class org.fusesource.jansi.internal.Kernel32的问题,把jar包放入hbase client的lib包下,重新运行hbase.cmd shell即可

    java串口所需依赖dll文件合集

    监听COM口启动程序报错 Could not initialize class gnu.io.RXTXCommDriver异常 是因为需要在%JAVA_HOME%/jre/bin目录中添加rxtxParallel.dll、rxtxSerial.dll文件

    jacob_1.14.3.rar

    内涵jacob_1.14.3-x64.dll;jacob_1.14.3-64.jdk(maven地址);解决方法:Could not initialize class com.jacob.com.ComThread

    instantclient_11_2.zip

    Could not initialize "D:\app\Happy\product\11.2.0\dbhome_1\bin\oci.dll" Make sure you have the 32 bits Oracle Client installed. OracleHomeKey: OracleHomeDir: D:\app\Happy\product\11.2.0\dbhome_1 ...

    linux上实现视频截图

    在Linux操作系统上实现视频截图是一项常见的任务,尤其对于开发者来说,可能需要在处理多媒体内容时进行这样的操作。这里我们将深入探讨如何使用JavaCPP库在32位Linux环境下完成这一目标。 JavaCPP是一个强大的Java...

    ezmorph-1.0.6.jar

    缺少这个包可能导致Could not initialize class net.sf.json.JsonConfig 使用json时候将会用到的一个jar包,发现这个包在网上提供的比较少.缺少这个包可能导致Could not initialize class net.sf.json.JsonConfig ...

    derby.jar 。。

    java.lang.NoClassDefFoundError: Could not initialize class org.apache.derby.jdbc.AutoloadedDriver40 导致的原因: 在azkaban的server和executor中缺少一个叫derby.jar的包

    WINSOCK坏了 winsockfix

    错误消息“unable to initialize windows sockets interface error code 0”表明Winsock初始化失败,错误代码0通常表示系统级的错误,可能是由于Winsock配置出错、相关动态链接库(DLL)文件损坏、网络驱动程序冲突...

    IDA6.4中Pin debugger介绍

    IDA6.4中支持pin的动态二进制插桩功能,这是其中的描述文档

    jasperreport maven打包后找不到字体解决方案

    jasperreport 用maven打包后找不到字体解决方案 net.sf.jasperreports.engine.JRRuntimeException: Could not load the following font

    gnu.io.rar_android_gnu.io _librxtxSerial_librxtxSerial._librxtxS

    在Android平台上进行串口通信开发时,常常会遇到需要与硬件设备进行低级别交互的情况,例如读写传感器数据、控制外部设备等。标题中的“gnu.io.rar_android_gnu.io _librxtxSerial_librxtxSerial._librxtxS”提到了`...

    json-lib-2.1 2.2 2.3 2.4-jdk15

    JSONArray.fromObject(map)报错:Could not initialize class net.sf.json.JsonConfig。ireport 需要高于2.1版本的包。于是就找了这些包。最后2.2.2适合

    64位操作系统使用plsql

    背景:  windows 7 64位操作系统,安装数据库:win64_11gR2_database_1of... Could not initialize C:oracleproduct10.2.0client_1inoci.dll Make sure you have the 32 bits Oracle Client installed. OCIDLL forc

    instantclient_11_2_2

    描述中提到的问题是用户在尝试使用PL/SQL Developer工具连接Oracle数据库时遇到了错误提示:“Could not initialize "%ORACLE_HOME%\bin\oci.dll"”,这表明系统缺少oci.dll文件,这是Oracle客户端与数据库交互的...

    InstantClient-64bit-安装包-完美解决PL/SQL连接数据库提示oci.dll加载失败的问题

    为了解决oci.dll加载失败的问题,你可以按照以下步骤操作: 1. **下载并安装Instant Client**: 首先,你需要从Oracle官方网站下载适合你系统的Instant Client版本。在这个案例中,你需要的是64位版本。下载的文件...

Global site tag (gtag.js) - Google Analytics