---
title: "用DetachedCriteria避免一对多时的重复数据"
author: "yezi"
date: "2010-03-03T15:57:34Z"
tags: []
categories: ["企业架构"]
slug: "detachedcriteria"
draft: false
lastmod: "2010-03-03T15:57:34Z"
id: 607013
url: "https://yezi.iteye.com/blog/607013"
---

1. DetachedCriteria criteria = DetachedCriteria.forClass() // 父对象
2. 将所有查询条件写进去
3. DetachedCriteria rootCriteria = DetachedCriteria.forClass() // 父对象
4. criteria.setProjection(Projections.distinct(Projections.projectionList().add(Projections.alias(Projections.property("id"), "id")))); // id 是distinct对象

5. rootCriteria.add(Subqueries.propertyIn("id", criteria));