实体管理器返回@OneToMany相关实体的重复副本

Sco*_*ott 9 spring hibernate jpa-2.0

我正在开发一个应用程序来帮助我的办公室跟踪和管理评论.在应用程序中,我使用JPA 2.0和Hibernate 3.6.3作为我的底层提供程序.我也使用spring将持久化上下文注入到我的DAO中.我建立了域名,以便有一个评论,一个参与者和一个角色实体.

我遇到的问题是,如果参与者有多个角色,当我从实体经理获得评论时,参与者列表中存在相同参与者的重复副本(即相同的ID).我还发现重复数量与角色数量直接相关(即如果参与者有3个角色,那么参与者在评论的参与者列表中出现3次)

我之前使用过直接的Hibernate,但这是我第一次使用JPA,所以我确定我配置错了.我只是不知道它是什么.

这是代码:

评论:

@Entity
public class Review extends BaseModel{

@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER, optional=false)
private Item item;

@Column(name="ISNEW", nullable=false)
private boolean isNew;

@Enumerated(EnumType.STRING)
@Column(name="STATUS", nullable=false)
private ReviewStatus status;

@Enumerated(EnumType.STRING)
@Column(name="PHASE", nullable=false)
private Phase phase;

@Enumerated(EnumType.STRING)
@Column(name="REVIEW_TYPE", nullable=false)
private ReviewType reviewType;

@OneToMany( cascade=CascadeType.ALL, fetch=FetchType.EAGER)
private List<Participant> participants;

@OneToMany(cascade=CascadeType.ALL)
private List<Defect> defects;

@Column(name="START_DATE", nullable=false)
private Date startDate;

@Column(name="MEETING_DATE", nullable=false)
private Date meetingDate;

@Column(name="FINISH_DATE")
private Date finishDate;

@Column(name="DURATION", nullable=false)
private Double duration;

public Item getItem()
{
    return item;
}
public void setItem(Item item)
{
    this.item = item;
}   
public List<Participant> getParticipants() {
    return participants;
}
public void setParticipants(List<Participant> participants) {
    this.participants = participants;
}
public List<Defect> getDefects() {
    return defects;
}
public void setDefects(List<Defect> defects) {
    this.defects = defects;
}   
public boolean isNew() {
    return isNew;
}
public void setNew(boolean isNew) {
    this.isNew = isNew;
}
public Phase getPhase() {
    return phase;
}
public void setPhase(Phase phase) {
    this.phase = phase;
}
public Double getDuration() {
    return duration;
}
public void setDuration(Double duration) {
    this.duration = duration;
}
public ReviewStatus getStatus() {
    return status;
}
public void setStatus(ReviewStatus status) {
    this.status = status;
}
public Date getStartDate() {
    return startDate;
}
public void setStartDate(Date startDate) {
    this.startDate = startDate;
}
public Date getMeetingDate() {
    return meetingDate;
}
public void setMeetingDate(Date meetingDate) {
    this.meetingDate = meetingDate;
}
public Date getFinishDate() {
    return finishDate;
}
public void setFinishDate(Date finishDate) {
    this.finishDate = finishDate;
}
public ReviewType getReviewType()
{
    return reviewType;
}
public void setReviewType(ReviewType reviewType)
{
    this.reviewType = reviewType;
}
Run Code Online (Sandbox Code Playgroud)

}

参与者:

@Entity
public class Participant extends BaseModel{


        private Double inspectionTime;

        @ManyToOne(cascade=CascadeType.ALL, optional=false)
        private Person person;

        @ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER )
        private Set<Role> roles;

        public Double getInspectionTime() {
            return inspectionTime;
        }
        public void setInspectionTime(Double preInspectionTime) {
            this.inspectionTime = preInspectionTime;
        }
        public Person getPerson() {
            return person;
        }
        public void setPerson(Person person) {
            this.person = person;
        }
        public Set<Role> getRoles() {
            return roles;
        }
        public void setRoles(Set<Role> roles) {
            this.roles = roles;
        }

}
Run Code Online (Sandbox Code Playgroud)

角色:

@Entity
public class Role extends BaseModel{
            @Column(nullable=false, unique=true)
            private String name;
            private String responsiblity;

            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            public String getResponsiblity() {
                return responsiblity;
            }
            public void setResponsiblity(String responsiblity) {
                this.responsiblity = responsiblity;
            }

}
Run Code Online (Sandbox Code Playgroud)

基础:

@MappedSuperclass
public abstract class BaseModel {

    @Id
    @GeneratedValue
    private Long id;

    public Long getId() {
        return id;
    }

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

}
Run Code Online (Sandbox Code Playgroud)

使用entityManager的DataAccessObject:

@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public class ReviewDaoJpaImpl implements ReviewDao
{


    @PersistenceContext
    private EntityManager em;

    public Review getReviewById(Long id)
    {
        return em.find(Review.class, id);
    }
}
Run Code Online (Sandbox Code Playgroud)

接收重复的呼叫:

    Review review = reviewDao.getReviewById(1776L);
    List<Participant> participants = review.getParticipants();
    for (Participant participant : participants)
    {
        System.out.println(participant.getPerson().getName());
    }
Run Code Online (Sandbox Code Playgroud)

Ral*_*lph 12

该问题被称为(N + 1问题),并且与参与者中的角色的急切获取有关.

处理延迟加载取代eager fetch的最简单方法.

  • 也可以使用@Fetch(FetchMode.SUBSELECT)而不是让它变得懒惰. (3认同)