不同的java对象引用相同的内存地址异常解决方案

1、首先数据库查询有四条记录: 
 

查询视图:select * from tav_user_dept_role  where USER_TITLE  = '罗可扬';
返回结果:


那么也就对应了4个不同的对象。


2、实际操作中并非如此,返回了4对对象,但是却都引用了同一个内存地址:

		List userInfoList = swfBiz.getTaViewUserDeptRoleList(user);

		TaViewUserDeptRole taViewUserDeptRole0 = (TaViewUserDeptRole)userInfoList.get(0);
                TaViewUserDeptRole taViewUserDeptRole1 = (TaViewUserDeptRole)userInfoList.get(1);
                TaViewUserDeptRole taViewUserDeptRole2 = (TaViewUserDeptRole)userInfoList.get(2);
                TaViewUserDeptRole taViewUserDeptRole3 = (TaViewUserDeptRole)userInfoList.get(3);

                System.out.println("taViewUserDeptRole0--->"+taViewUserDeptRole0+"    taViewUserDeptRole0---->"+taViewUserDeptRole0.getDeptName());
                System.out.println("taViewUserDeptRole1--->"+taViewUserDeptRole1+"    taViewUserDeptRole1---->"+taViewUserDeptRole1.getDeptName());
                System.out.println("taViewUserDeptRole2--->"+taViewUserDeptRole2+"    taViewUserDeptRole2---->"+taViewUserDeptRole2.getDeptName());
                System.out.println("taViewUserDeptRole3--->"+taViewUserDeptRole3+"    taViewUserDeptRole3---->"+taViewUserDeptRole3.getDeptName());
控制台打印结果:

控制台打印输出:

taViewUserDeptRole0--->adam.bp.workflow.entity.TaViewUserDeptRole@111ebb3    taViewUserDeptRole0---->江南营业所
taViewUserDeptRole1--->adam.bp.workflow.entity.TaViewUserDeptRole@111ebb3    taViewUserDeptRole1---->江南营业所
taViewUserDeptRole2--->adam.bp.workflow.entity.TaViewUserDeptRole@111ebb3    taViewUserDeptRole2---->江南营业所
taViewUserDeptRole3--->adam.bp.workflow.entity.TaViewUserDeptRole@111ebb3    taViewUserDeptRole3---->江南营业所
四个对象的内存引用均为:adam.bp.workflow.entity.TaViewUserDeptRole@111ebb3。

显然出现了异常情况,但是控制台并没有报出任何异常。


3、发现问题

经过研究发现,问题出在了我的hibernate配置上,在利用hibernate反向生成视图 tav_user_dept_role对应的实体类时,本来是以联合主键的方式生成了两个类:TaViewUserDeptRole 和TaViewUserDeptRoleId。将所有的属性字段都放在了TaViewUserDeptRoleId中,然后通过TaViewUserDeptRole 的属性TaViewUserDeptRoleId调用。

我感觉不爽,直接干掉了。把所有的属性都移到了TaViewUserDeptRole 类中,并指定了一个主键:

配置如下:TaViewUserDeptRole.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 - Hibernate Tools
-->
<hibernate-mapping>
    <class name="adam.bp.workflow.entity.TaViewUserDeptRole" table="TAV_USER_DEPT_ROLE" schema="JMJL">
        <id name="userName" type="string">
            <column name="USER_NAME" length="21" />
			<generator class="native"></generator>
		</id>
        <property name="userTitle" type="string">
            <column name="USER_TITLE" length="30" />
        </property>
       	<property name="deptId" type="string">
            <column name="DEPT_ID" length="39" />
        </property>
        <property name="deptName" type="string">
            <column name="DEPT_NAME" length="40" />
        </property>
        <property name="roleName" type="string">
            <column name="ROLE_NAME" length="21" />
        </property>
        <property name="roleTitle" type="string">
            <column name="ROLE_TITLE" length="30" />
        </property>
    </class>
</hibernate-mapping>
这样的修改便是导致问题的原因了!

4、解决问题

通过hibernate反向重新生成实体类:TaViewUserDeptRole_new和TaViewUserDeptRole_newId。

如下配置TaViewUserDeptRole_new.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 - Hibernate Tools
-->
<hibernate-mapping>
    <class name="adam.bp.workflow.entity.TaViewUserDeptRole_new" table="TAV_USER_DEPT_ROLE" schema="JMJL">
        <composite-id name="id" class="adam.bp.workflow.entity.TaViewUserDeptRole_newId">
            <key-property name="userName" type="string">
                <column name="USER_NAME" length="21" />
            </key-property>
            <key-property name="userTitle" type="string">
                <column name="USER_TITLE" length="30" />
            </key-property>
            <key-property name="deptId" type="string">
                <column name="DEPT_ID" length="39" />
            </key-property>
            <key-property name="deptName" type="string">
                <column name="DEPT_NAME" length="40" />
            </key-property>
            <key-property name="roleName" type="string">
                <column name="ROLE_NAME" length="21" />
            </key-property>
            <key-property name="roleTitle" type="string">
                <column name="ROLE_TITLE" length="30" />
            </key-property>
        </composite-id>
    </class>
</hibernate-mapping>

重新查找:

		List userInfoList = swfBiz.getTaViewUserDeptRole_newList(user);
		TaViewUserDeptRole_new taViewUserDeptRole0 = (TaViewUserDeptRole_new)userInfoList.get(0);
                TaViewUserDeptRole_new taViewUserDeptRole1 = (TaViewUserDeptRole_new)userInfoList.get(1);
                TaViewUserDeptRole_new taViewUserDeptRole2 = (TaViewUserDeptRole_new)userInfoList.get(2);
                TaViewUserDeptRole_new taViewUserDeptRole3 = (TaViewUserDeptRole_new)userInfoList.get(3);
                System.out.println("taViewUserDeptRole0--->"+taViewUserDeptRole0+"    taViewUserDeptRole0---->"+taViewUserDeptRole0.getId().getDeptName());
                System.out.println("taViewUserDeptRole1--->"+taViewUserDeptRole1+"    taViewUserDeptRole1---->"+taViewUserDeptRole1.getId().getDeptName());
                System.out.println("taViewUserDeptRole2--->"+taViewUserDeptRole2+"    taViewUserDeptRole2---->"+taViewUserDeptRole2.getId().getDeptName());
                System.out.println("taViewUserDeptRole3--->"+taViewUserDeptRole3+"    taViewUserDeptRole3---->"+taViewUserDeptRole3.getId().getDeptName());
控制台打印输出:
taViewUserDeptRole0--->adam.bp.workflow.entity.TaViewUserDeptRole_new@38f07b    taViewUserDeptRole0---->江南营业所
taViewUserDeptRole1--->adam.bp.workflow.entity.TaViewUserDeptRole_new@3459ed    taViewUserDeptRole1---->江南营业所
taViewUserDeptRole2--->adam.bp.workflow.entity.TaViewUserDeptRole_new@fbef54    taViewUserDeptRole2---->恩平供电局
taViewUserDeptRole3--->adam.bp.workflow.entity.TaViewUserDeptRole_new@1d9313    taViewUserDeptRole3---->恩平供电局

显然这样输出的不同对象在内存中也是引用不同的地址了。


你可能感兴趣的:(java,Hibernate,MyEclipse,String,user,encoding)