我正在开发一个应用程序来帮助我的办公室跟踪和管理评论。在应用程序中,我使用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;
}
}
参与者:
@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;
}
}
作用:
@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;
}
}
基地:
@MappedSuperclass
public abstract class BaseModel {
@Id
@GeneratedValue
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
使用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);
}
}
接收重复的呼叫:
Review review = reviewDao.getReviewById(1776L);
List<Participant> participants = review.getParticipants();
for (Participant participant : participants)
{
System.out.println(participant.getPerson().getName());
}
答案 0 :(得分:12)
该问题被称为(N + 1问题),并且与参与者中的角色的急切获取有关。
处理它的最简单方法是通过延迟加载替换eager fetch。
答案 1 :(得分:2)
Complete Reference to Hibernate Documentation
只需将Criteria.DISTINCT_ROOT_ENTITY置于内部即可创建一个Set,并自动删除重复项。
List result = session.createCriteria(Order.class)
.setFetchMode("lineItems", FetchMode.JOIN)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.list();