hibernate-DetachedCriteria实现关联表条件复查
表结果如下
CREATE TABLE `ent_lable` ( `idStr` char(32) NOT NULL, `pk_1` char(6) NOT NULL, `pk_2` char(32) NOT NULL, PRIMARY KEY (`idStr`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
本身为一张多对多的中间表
业务如下
要根据pk_1查询所有的pk_2,然后再根据所得的pk_2的值查询出相关连的pk_1
模型:
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="ent_lable") public class EntLable extends BaseModel{ private static final long serialVersionUID = 1L; @Id @GeneratedValue(generator="paymentableGenerator") @GenericGenerator(name="paymentableGenerator",strategy="uuid") @Column(name="idStr") private String idStr; @Column(name="pk_1") private String pk_1; @Column(name="pk_2") private String pk_2; public void setIdStr(String idStr){ this.idStr=idStr; } public String getIdStr(){ return idStr; } public String getPk_1() { return pk_1; } public void setPk_1(String pk_1) { this.pk_1 = pk_1; } public String getPk_2() { return pk_2; } public void setPk_2(String pk_2) { this.pk_2 = pk_2; } }
DetachedCriteria的处理逻辑
DetachedCriteria dc = DetachedCriteria.forClass(EntLable.class); if(StringUtils.isBlank(pk_1)) return null; dc.add(Restrictions.eq("pk_1", pk_1)); dc.setProjection(Projections.property("pk_2")); DetachedCriteria dc1 = DetachedCriteria.forClass(EntLable.class); dc1.add(Property.forName("pk_2").eq(dc));
另,父类
import org.springframework.util.StringUtils; public abstract class BaseModel extends BaseObject implements Modelable { private static final long serialVersionUID = 1L; public int hashCode() { String idStr = getIdStr(); return StringUtils.isEmpty(idStr)?super.hashCode():idStr.hashCode(); } public boolean equals(Object other) { if (other == null) { return false; } if (other == this) { return true; } /*因为字节码增强的关系,getClass()不能用作判断的依据*/ if (getClass().getPackage() != other.getClass().getPackage()) { return false; } if (hashCode() == other.hashCode()) { return true; } return false; } }
import java.io.Serializable; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import org.apache.log4j.Logger; public class BaseObject implements Cloneable, Serializable { private static final long serialVersionUID = 1L; /**日志对象*/ protected final Logger log = Logger.getLogger(getClass()); /**覆盖toString方法,ToStringStyle取值为ToStringStyle.SHORT_PREFIX_STYLE * ,调试的时候注意会自动取所有引用的值,会触发所有的延迟加载 * @return String * @see org.apache.commons.lang.builder.ToStringBuilder#reflectionToString(Object, ToStringStyle) */ public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); } /** * 提供默认的clone方法的实现,不支持深层复制 * @return Object */ public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException ex) { throw new IllegalArgumentException(ex.getMessage()); } } }
import java.io.Serializable; public interface Modelable extends JSONNotAware,Serializable, Cloneable { String getIdStr(); void setIdStr(String idStr); }
/** * 实现此接口的对象,都不会被ModelUtils中的model2JSON和modelList2JSONArray转换方法序列化 */ public interface JSONNotAware { }
知识只有共享才能传播,才能推崇出新的知识,才能学到更多,这里写的每一篇文字/博客,基本都是从网上查询了一下资料然后记录下来,也有些是原滋原味搬了过来,也有时加了一些自己的想法