现在正在实现的一个模块,是用Hibernate做的,一个用户对应多个频道,用的是一对多的关联,配置方件如下:
User表:<?xml version="1.0"?>
<!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 lazy="false" name="com.autonavi.monitor.model.User" table="user">
<id name="userId" type="integer" unsaved-value="0">
<column name="userId"/>
<generator class="native"/>
</id>
<property name="name" type="string">
<column name="name" />
</property>
<property name="userName" type="string">
<column name="userName" not-null="true"/>
</property>
<property name="password" type="string">
<column name="password" not-null="true"/>
</property>
<property name="actor" type="int">
<column name="actor"/>
</property>
<property name="authority" type="string">
<column name="authority"/>
</property>
<property name="email" type="string">
<column name="email"/>
</property>
<property name="phone" type="string">
<column name="phone"/>
</property>
<property name="phone1" type="string">
<column name="phone1"/>
</property>
<property name="phone2" type="string">
<column name="phone2"/>
</property>
<set name="channels" cascade="all" lazy="false">
<key column="userId" />
<one-to-many class="com.autonavi.monitor.model.Channel"/>
</set>
</class>
</hibernate-mapping>
频道表:<?xml version="1.0"?>
<!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 lazy="false" name="com.autonavi.monitor.model.Channel" table="channel">
<id name="channelId" type="integer" unsaved-value="0">
<column name="channelId"/>
<generator class="native"/>
</id>
<property name="channel" type="string">
<column name="channel" />
</property>
<property name="channelName" type="string">
<column name="channel_name"/>
</property>
<property name="delayTime" type="integer">
<column name="delay_time" not-null="true"/>
</property>
</class>
</hibernate-mapping>在Action里面做相应的查询,由于取消了懒加载,在查出的User里面会取到对应的频道列表List<Channel> userChannelList = new ArrayList<Channel>();
Channel channel = null;
Set<Channel> channelSet = modifyUser.getChannels();
Iterator<Channel> iterator = channelSet.iterator();
while(iterator.hasNext()) {
channel = (Channel)iterator.next();
userChannelList.add(channel);
}
用Hibernate的HQL功能取到所有的频道:@SuppressWarnings("unchecked")
public List<Channel> getChannelList() {
String sql = "";
sql = "from Channel";
List<Channel> list = this.getHibernateTemplate().find(sql);
return list;
}
现在我要得到剩下的频道,所以想着是用得到的所有的频道List调用removeAll方法去掉当前用户占用的频道ListchannelList.removeAll(userChannelList);
本来挺简单的,可执行后发现并没有达到预期的效果,不能从所有的频道中把当前用户占用的频道remove掉,找了很久才发现所有频道List中包含的当前用户占用的频道的hashcode和当前用户占用的频道List中的频道的hashcode值不一样,其他值都相同,猜测可能是因为这所以不能执行remove操作,我想知道为什么会出现这种情况?该怎么实现我上面所说的需求?
User表:<?xml version="1.0"?>
<!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 lazy="false" name="com.autonavi.monitor.model.User" table="user">
<id name="userId" type="integer" unsaved-value="0">
<column name="userId"/>
<generator class="native"/>
</id>
<property name="name" type="string">
<column name="name" />
</property>
<property name="userName" type="string">
<column name="userName" not-null="true"/>
</property>
<property name="password" type="string">
<column name="password" not-null="true"/>
</property>
<property name="actor" type="int">
<column name="actor"/>
</property>
<property name="authority" type="string">
<column name="authority"/>
</property>
<property name="email" type="string">
<column name="email"/>
</property>
<property name="phone" type="string">
<column name="phone"/>
</property>
<property name="phone1" type="string">
<column name="phone1"/>
</property>
<property name="phone2" type="string">
<column name="phone2"/>
</property>
<set name="channels" cascade="all" lazy="false">
<key column="userId" />
<one-to-many class="com.autonavi.monitor.model.Channel"/>
</set>
</class>
</hibernate-mapping>
频道表:<?xml version="1.0"?>
<!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 lazy="false" name="com.autonavi.monitor.model.Channel" table="channel">
<id name="channelId" type="integer" unsaved-value="0">
<column name="channelId"/>
<generator class="native"/>
</id>
<property name="channel" type="string">
<column name="channel" />
</property>
<property name="channelName" type="string">
<column name="channel_name"/>
</property>
<property name="delayTime" type="integer">
<column name="delay_time" not-null="true"/>
</property>
</class>
</hibernate-mapping>在Action里面做相应的查询,由于取消了懒加载,在查出的User里面会取到对应的频道列表List<Channel> userChannelList = new ArrayList<Channel>();
Channel channel = null;
Set<Channel> channelSet = modifyUser.getChannels();
Iterator<Channel> iterator = channelSet.iterator();
while(iterator.hasNext()) {
channel = (Channel)iterator.next();
userChannelList.add(channel);
}
用Hibernate的HQL功能取到所有的频道:@SuppressWarnings("unchecked")
public List<Channel> getChannelList() {
String sql = "";
sql = "from Channel";
List<Channel> list = this.getHibernateTemplate().find(sql);
return list;
}
现在我要得到剩下的频道,所以想着是用得到的所有的频道List调用removeAll方法去掉当前用户占用的频道ListchannelList.removeAll(userChannelList);
本来挺简单的,可执行后发现并没有达到预期的效果,不能从所有的频道中把当前用户占用的频道remove掉,找了很久才发现所有频道List中包含的当前用户占用的频道的hashcode和当前用户占用的频道List中的频道的hashcode值不一样,其他值都相同,猜测可能是因为这所以不能执行remove操作,我想知道为什么会出现这种情况?该怎么实现我上面所说的需求?
你只有通过嵌套for,根据判断channelList.get(i).channelId==userChannelList.get(j).channelId
如果相同,则channelList.remove(i),这样一个一个删除。