试验了一下,对无主键表更新不能成功,添加删除没问题
public class SsSysIpFilter implements Serializable { /** * */ private static final long serialVersionUID = 1L; private SsSysIpFilterId id; public SsSysIpFilter(SsSysIpFilterId id) { super(); this.id = id; } public SsSysIpFilter() { super(); } public SsSysIpFilterId getId() { return id; } public void setId(SsSysIpFilterId id) { this.id = id; } }
public class SsSysIpFilterId implements Serializable
{ /** * */ private static final long serialVersionUID = 1L; private String ipFrom;//IP段起始 private String ipTo;//IP段结束 private long ipFromNumber;//起始IP转成的数字 private long ipToNumber;//结束IP转成的数字 /** * 转换规则 * 10.1.1.1 * 转:10*256*256*256+1*256*256+1*256+1 * 相关于转成256进制的数 */ private boolean ipFilteType;//0为黑名单,1为白名单 public SsSysIpFilterId(String ipFrom, String ipTo, long ipFromNumber, long ipToNumber, boolean ipFilteType) { super(); this.ipFrom = ipFrom; this.ipTo = ipTo; this.ipFromNumber = ipFromNumber; this.ipToNumber = ipToNumber; this.ipFilteType = ipFilteType; } public SsSysIpFilterId() { super(); } public String getIpFrom() { return ipFrom; } public void setIpFrom(String ipFrom) { this.ipFrom = ipFrom; } public String getIpTo() { return ipTo; } public void setIpTo(String ipTo) { this.ipTo = ipTo; } public long getIpFromNumber() { return ipFromNumber; } public void setIpFromNumber(long ipFromNumber) { this.ipFromNumber = ipFromNumber; } public long getIpToNumber() { return ipToNumber; } public void setIpToNumber(long ipToNumber) { this.ipToNumber = ipToNumber; } public boolean isIpFilteType() { return ipFilteType; } public void setIpFilteType(boolean ipFilteType) { this.ipFilteType = ipFilteType; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (ipFilteType ? 1231 : 1237); result = prime * result + ((ipFrom == null) ? 0 : ipFrom.hashCode()); result = prime * result + (int) (ipFromNumber ^ (ipFromNumber >>> 32)); result = prime * result + ((ipTo == null) ? 0 : ipTo.hashCode()); result = prime * result + (int) (ipToNumber ^ (ipToNumber >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final SsSysIpFilterId other = (SsSysIpFilterId) obj; if (ipFilteType != other.ipFilteType) return false; if (ipFrom == null) { if (other.ipFrom != null) return false; } else if (!ipFrom.equals(other.ipFrom)) return false; if (ipFromNumber != other.ipFromNumber) return false; if (ipTo == null) { if (other.ipTo != null) return false; } else if (!ipTo.equals(other.ipTo)) return false; if (ipToNumber != other.ipToNumber) return false; return true; } }
<hibernate-mapping> <class name="com.vixtel.model.SsSysIpFilter" table="SS_SYS_IP_FILTER"> <composite-id name="id" class="com.vixtel.model.SsSysIpFilterId"> <key-property name="ipFrom" type="java.lang.String"> <column name="IP_FROM" precision="100" scale="0" /> </key-property> <key-property name="ipTo" type="java.lang.String"> <column name="IP_TO" precision="100" scale="0" /> </key-property> <key-property name="ipFromNumber" type="java.lang.Long"> <column name="IP_FROM_L" precision="10" scale="0" /> </key-property> <key-property name="ipToNumber" type="java.lang.Long"> <column name="IP_TO_L" precision="10" scale="0" /> </key-property> <key-property name="ipFilteType" type="java.lang.Boolean"> <column name="FILTER_TYPE" precision="1" scale="0" /> </key-property> </composite-id> </class> </hibernate-mapping>
应该是很标准的无主键表的配置了,担心其会调用equals方法来判断对象是否变化,就将它加上了,结果更新还是失败
SsSysIpFilter ssSysIpFilter = (SsSysIpFilter)list.get(0);
SsSysIpFilterId id = ssSysIpFilter.getId(); id.setIpFrom(new_ipFrom); id.setIpTo(new_ipTo); id.setIpFromNumber(new_ipFromNumber); id.setIpToNumber(new_ipToNumber); id.setIpFilteType(ipFilteType_new); hibernateTemplate.update(ssSysIpFilter);
只能看到select语句,根本看不到update语句出来,估计hibernate无法判断无主键表的信息是否发生了变化.
public void deleteSsSysIpFilter(String ipFrom ,String ipTo){ List list = hibernateTemplate.find("from SsSysIpFilter s where s.id.ipFrom = '"+ipFrom+"' and s.id.ipTo ='"+ipTo+"'"); if(list.size()>0){ hibernateTemplate.delete((SsSysIpFilter)list.get(0)); } } public void addSsSysIpFilter(SsSysIpFilter ipFilter){ hibernateTemplate.save(ipFilter); } 添加删除都没问题