我正在尝试使用 Hibernate 从数据库中获取对象列表,但对我来说有些事情似乎很奇怪。
在数据库中,我有 5 次运行,每次 3 个位置。
当我从 RunDao 获取运行列表时,我获得了 15 个 Run 对象的列表!前三个运行具有相同的 ID。
就我而言,我只想获得 5 次运行。我做错了什么吗?
如何只获得 5 次运行?
非常感谢
这是我的类(class) Run/Position/RunDao :
运行.java
@Entity
@Table(name="kr_runs")
public class Run {
private long id;
private Date date;
private int indexedPages;
private int pr;
@JsonIgnore
private Site site;
private Set<Position> positions = new HashSet<Position>(0);
public Run() {
}
@Id
@GeneratedValue
@Column(name="id")
public long getId() {
return id;
}
public void setId( long id ) {
this.id = id;
}
@Temporal(value=TemporalType.TIMESTAMP)
public Date getDate() {
return date;
}
public void setDate( Date date ) {
this.date = date;
}
@Column(name="indexed_pages")
public int getIndexedPages() {
return indexedPages;
}
public void setIndexedPages( int indexedPages ) {
this.indexedPages = indexedPages;
}
@Column
public int getPr() {
return pr;
}
public void setPr( int pr ) {
this.pr = pr;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "kr_site_id", nullable = false)
public Site getSite() {
return site;
}
public void setSite( Site site ) {
this.site = site;
}
@Cascade({CascadeType.ALL})
@OneToMany(fetch = FetchType.EAGER, mappedBy = "run")
public Set<Position> getPositions() {
return positions;
}
public void setPositions( Set<Position> positions ) {
this.positions = positions;
}
}
位置.java
@Entity
@Table(name="kr_positions")
public class Position {
private long id;
private int pos;
private String url;
@JsonIgnore
private Run run;
private Keyword keyword;
public Position() {
}
@Id
@GeneratedValue
@Column(name="id")
public long getId() {
return id;
}
public void setId( long id ) {
this.id = id;
}
@Column
public int getPos() {
return pos;
}
public void setPos( int pos ) {
this.pos = pos;
}
@Column
public String getUrl() {
return url;
}
public void setUrl( String url ) {
this.url = url;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "kr_run_id", nullable = false)
public Run getRun() {
return run;
}
public void setRun( Run run ) {
this.run = run;
}
//@Cascade({CascadeType.SAVE_UPDATE})
//@OneToOne(mappedBy="position")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "kr_keyword_id", nullable = false)
public Keyword getKeyword() {
return keyword;
}
public void setKeyword( Keyword keyword ) {
this.keyword = keyword;
}
}
RunDao.java
@Transactional
public class RunDao extends HibernateDao<Run, Long> implements IRunDao {
public List<Run> find( long siteID, Date date_start, Date date_end ) {
Criteria cr = currentSession().createCriteria(Run.class);
cr.add(Restrictions.eq("site.id", siteID));
cr.add(Restrictions.ge("date",date_start));
cr.add(Restrictions.lt("date",date_end));
List<Run> list = (List<Run>) cr.list();
if (list.isEmpty()) return null;
else return list;
}
}
已解决
感谢 Guillaume 的帮助,我找到了解决方案。
- 我在 Run 类中替换了:
@OneToMany(fetch = FetchType.EAGER,mappedBy =“运行”)
由
@OneToMany(fetch = FetchType.LAZY,mappedBy =“运行”)
- 在我的 RunsService 类中,当我从 DAO 获取 Runs 时,我会提取惰性集合:
// Get data from DAO List<Run> list = runDao.find(siteID, cal.getTime(), date_today); for(Run run : list) { Hibernate.initialize(run.getPositions()); // run.getPositions().size(); if you are note in @Transactional }
最佳答案
代码中的所有内容看起来都是正确的。我唯一不太确定的是 @OneToMany(fetch = FetchType.EAGER ...
,因为我过去在 EAGER
获取类型方面遇到过错误。
尝试将其保留为 LAZY,并在您的条件查询中添加 fetch join
。
关于java - Spring/Hibernate 获取包含列表的对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33809521/