hibernate,使用fetch=FetchType.EAGER Criteria分页关联查询解决重复记录

可以使用这句Criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)也可以,但是这句有缺陷,不建议使用

最好将fetch=FetchType.EAGER替换 @LazyCollection(LazyCollectionOption.FALSE)。

@Entity
@Table(name = "image_path")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class ImagePathEntity extends BaseIdEntity
{
    private String image_path;            
    private String new_path;             
    private ImageEntity image;

    @Column(name = "image_path", unique = false, nullable = true, length = 255)
    public String getImage_path() 
    {
        return image_path;
    }
    public void setImage_path(String imagePath) 
    {
        image_path = imagePath;
    }
    
    @Column(name = "new_path", unique = false, nullable = true, length = 255)
    public String getNew_path() 
    {
        return new_path;
    }
    public void setNew_path(String newPath) 
    {
        new_path = newPath;
    }
    
    @ManyToOne(/*fetch=FetchType.EAGER, */cascade = CascadeType.ALL)
    @JoinColumn(name="f_image_id", nullable=true)
    @LazyCollection(LazyCollectionOption.FALSE)
    public ImageEntity getImage() 
    {
        return image;
    }
    public void setImage(ImageEntity image) 
    {
        this.image = image;
    }
}
 

@Entity
@Table(name = "image")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class ImageEntity extends BaseIdEntity 
{
    private String image_name;                   
    private List<ImagePathEntity> image_path = Lists.newArrayList();
    
    @Column(name = "image_name", unique = true, nullable = false, length = 50)
    public String getImage_name() 
    {
        return image_name;
    }
    public void setImage_name(String imageName) 
    {
        image_name = imageName;
    }
    
    @JSON(serialize=false)
    @OneToMany(mappedBy = "image", /*fetch=FetchType.EAGER,*/ cascade = CascadeType.ALL)
    @LazyCollection(LazyCollectionOption.FALSE)
    public List<ImagePathEntity> getImage_path() 
    {
        return image_path;
    }
    public void setImage_path(List<ImagePathEntity> imagePath) 
    {
        image_path = imagePath;
    }     
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值