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;
    }     
}


版权声明:本文为yanjiaye520原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。