使用DetachedCriteria进行关联查询


其中MobiUser和Rewards是多对多的关系,MobiuserRewardsR是连接它们的关联表。

 

 

@Override
    public List<MobiuserRewardsR> findMyRewards(TableModel<MobiuserRewardsR> tableModel, Long mobiUserId) {
        DetachedCriteria dc = DetachedCriteria.forClass(MobiuserRewardsR.class);
        dc.setFetchMode("mobiUser", FetchMode.JOIN);
        dc.setFetchMode("rewards", FetchMode.JOIN);
        if (mobiUserId != null) {
            dc.createAlias("mobiUser", "mobiUser");
            dc.add(Restrictions.eq("mobiUser.id", mobiUserId));
        }
        dc.createAlias("rewards", "rewards");
        dc.add(Restrictions.eq("rewards.deleted", Constant.NO));
        dc.add(Restrictions.eq("rewards.enabled", Constant.ENABLED));
        dc.add(Restrictions.gt("rewards.endTime", new Date()));
        dc.addOrder(Order.asc("rewards.endTime"));
        
        return findPageByCriteria(dc, tableModel);
    }

 

/**
 * MobiuserRewardsR generated by hbm2java
 */
@Entity
@Table(name = "mobiuser_rewards_r", schema = "public")
public class MobiuserRewardsR extends BaseBo {

	private long id;
	private Rewards rewards;
	private MobiUser mobiUser;
	private Boolean gift;

	public MobiuserRewardsR() {
	}

	@SequenceGenerator(name = "seq_mobiuser_rewards_r", sequenceName = "seq_mobiuser_rewards_r", initialValue = 1, allocationSize = 1)
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "seq_mobiuser_rewards_r")
	@Id
	@Column(name = "id", unique = true, nullable = false)
	public long getId() {
		return this.id;
	}

	public void setId(long id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "rew_id")
	public Rewards getRewards() {
		return this.rewards;
	}

	public void setRewards(Rewards rewards) {
		this.rewards = rewards;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "mobiuser_id")
	public MobiUser getMobiUser() {
		return this.mobiUser;
	}

	public void setMobiUser(MobiUser mobiUser) {
		this.mobiUser = mobiUser;
	}

    public Boolean getGift() {
        return gift;
    }

    @Column(name = "gift", nullable = false)
    public void setGift(Boolean gift) {
        this.gift = gift;
    }
}
 

你可能感兴趣的:(Criteria)