10

I am developing an application to help my office track and manage reviews. In the application, I am using JPA 2.0 with Hibernate 3.6.3 as my underlying provider. I am also using spring to inject the persistence context into my DAO. I have built the domain such that there is a Review, a Participant and a Role Entity.

The problem I am having is when I get a Review from the Entity Manager there are duplicate copies of the same Participant(i.e. same ID) in the list of participants if the Participant has more than one Role. I have also found the number of duplicates is directly related to the number Roles (i.e. if the a Participant has 3 Roles then the Participant occurs 3 times in the Review's list of participants)

I have used straight Hibernate before but this is my first time using JPA so I am sure I have configured something wrong. I just don't know what it is.

Here is the code:

Review:

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

}

Participant:

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

}

Role:

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

}

Base:

@MappedSuperclass
public abstract class BaseModel {

    @Id
    @GeneratedValue
    private Long id;

    public Long getId() {
        return id;
    }

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

}

DataAccessObject which uses the entityManager:

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

Call which receives duplicates:

    Review review = reviewDao.getReviewById(1776L);
    List<Participant> participants = review.getParticipants();
    for (Participant participant : participants)
    {
        System.out.println(participant.getPerson().getName());
    }
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
Scott
  • 103
  • 1
  • 4
  • gandonies which upvotes the answer -> before read it and then draw a conclusion! This is a helpful for 10 most known issue on Hibernate(ORM) - https://www.thoughts-on-java.org/common-hibernate-mistakes-cripple-performance/ Additionall info for massaging result set -> https://howtoprogramwithjava.com/how-to-fix-duplicate-data-from-hibernate-queries/ – Musa Jul 03 '18 at 13:36

2 Answers2

12

The problem is called (N+1 Problem) and is related to the eager fetch of Roles in Participant.

The easiest way the handle it would be replacing the eager fetch by lazy loading.

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • That was it. I am embarrassed that N+1 did not turn up on any of my searches about the problem. I guess I wasn't using the right keywords. Thanks for your help. I knew I was doing something wrong. – Scott Apr 21 '11 at 21:21
  • Can you please explain more about the N+1? – Dejell Jun 20 '13 at 22:01
  • @Odelya Google for "n+1 problem in hibernate" first hit is: http://stackoverflow.com/questions/97197/what-is-the-n1-selects-issue – Ralph Jun 21 '13 at 12:37
  • 3
    Can also use @Fetch(FetchMode.SUBSELECT) instead of making it lazy. – mR_fr0g Jun 18 '15 at 11:08
4

Complete Reference to Hibernate Documentation

Just put Criteria.DISTINCT_ROOT_ENTITY it will internally create a Set and duplicates are automatically removed.

List result = session.createCriteria(Order.class)  
                        .setFetchMode("lineItems", FetchMode.JOIN)  
                        .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)  
                        .list();  
AZ_
  • 21,688
  • 25
  • 143
  • 191