我有三个表A
,B
和C
。表B
具有对表a_id
的外键A
引用,表B
具有对表a_id
的外键A
引用。我的hibernate实体文件如下:
A.java
@Entity
@Table(name = "A")
public class A {
private long id;
private String contentA;
private List<B> bList;
private List<C> cList;
@OneToMany(fetch = FetchType.EAGER , mappedBy = "b")
public List<B> getbList() {
return bList;
}
public void setbList(List<B> bList) {
this.bList = bList;
}
@OneToMany(fetch = FetchType.EAGER , mappedBy = "b")
public List<C> getcList() {
return cList;
}
public void setcList(List<C> cList) {
this.cList = cList;
}
@Id
@Column(name = "id", nullable = false)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Basic
@Column(name = "content_a", nullable = false, length = 1000)
public String getContentA() {
return contentA;
}
public void setContentA(String contentA) {
this.contentA = contentA;
}
}
B.java
@Entity
@Table(name = "B")
public class B {
private long id;
private String contentB;
private A a;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "a_id", nullable = false)
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
@Id
@Column(name = "id", nullable = false)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Basic
@Column(name = "content_b", nullable = false, length = 1000)
public String getContentB() {
return contentB;
}
public void setContentB(String contentB) {
this.contentB = contentB;
}
}
C.java
@Entity
@Table(name = "C")
public class C {
private long id;
private String contentC;
private A a;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "a_id", nullable = false)
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
@Id
@Column(name = "id", nullable = false)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Basic
@Column(name = "content_c", nullable = false, length = 1000)
public String getContentC() {
return contentC;
}
public void setContentC(String contentC) {
this.contentC = contentC;
}
}
问题有时我只想用A
获取List<B>
,而在另一时间我希望A
获得List<C>
。我有什么方法可以通过hibernate来做到这一点吗?
------------------------分界线--------------------- -
在我的真实项目中,我有三个实体类:ChapterEntity
ChapterTitle1Entity
ChapterTitle2Entity
ChapterTitle3Entity
。 private List<ChapterTitle1Entity> chapterTitle1EntityList;
中有ChapterEntity
,private List<ChapterTitle2Entity> chapterTitle2EntityList;
中ChapterTitle2Entity
,private List<ChapterTitle3Entity> chapterTitle3EntityList;
中有ChapterTitle2Entity
。
我只想获得一个带有子列表ChapterEntity
的{{1}}列表,延迟加载注释不起作用。以下是我的班级文件
ChapterEntity.java
List<ChapterTitle1Entity> chapterTitle1EntityList
ChapterTitle1Entity.java
package com.hnu.tutorial.model.entity;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import javax.persistence.*;
import java.util.List;
/**
* Created by shiqin_zhang@qq.com on 2016-09-9/6/16
* Time 11:16 PM
*/
@Entity
@Table(name = "chapter")
public class ChapterEntity {
private List<ChapterTitle1Entity> chapterTitle1EntityList;
private long id;
private int sequence;
private String content;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "chapterEntity")
@Fetch(FetchMode.SUBSELECT)
public List<ChapterTitle1Entity> getChapterTitle1EntityList() {
return chapterTitle1EntityList;
}
public void setChapterTitle1EntityList(List<ChapterTitle1Entity> chapterTitle1EntityList) {
this.chapterTitle1EntityList = chapterTitle1EntityList;
}
@Id
@Column(name = "id", nullable = false)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Basic
@Column(name = "sequence", nullable = false)
public int getSequence() {
return sequence;
}
public void setSequence(int sequence) {
this.sequence = sequence;
}
@Basic
@Column(name = "content", nullable = false, length = 1000)
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ChapterEntity that = (ChapterEntity) o;
if (id != that.id) return false;
if (sequence != that.sequence) return false;
if (content != null ? !content.equals(that.content) : that.content != null) return false;
return true;
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + sequence;
result = 31 * result + (content != null ? content.hashCode() : 0);
return result;
}
}
ChapterTitle2Entity.java
package com.hnu.tutorial.model.entity;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import javax.persistence.*;
import java.util.List;
/**
* Created by shiqin_zhang@qq.com on 2016-09-11
* Time 6:09 PM
*/
@Entity
@Table(name = "chapter_title_1", schema = "tutorial2")
public class ChapterTitle1Entity {
private long id;
private int sequence;
private String content;
// private long chapterId;
/**
* 1:章前测试,2:实际的测试
*/
private int type;
private ChapterEntity chapterEntity;
private List<ChapterTitle2Entity> chapterTitle2EntityList;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "chapterTitle1Entity")
@Fetch(FetchMode.SUBSELECT)
public List<ChapterTitle2Entity> getChapterTitle2EntityList() {
return chapterTitle2EntityList;
}
public void setChapterTitle2EntityList(List<ChapterTitle2Entity> chapterTitle2EntityList) {
this.chapterTitle2EntityList = chapterTitle2EntityList;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "chapter_id", nullable = false)
public ChapterEntity getChapterEntity() {
return chapterEntity;
}
public void setChapterEntity(ChapterEntity chapterEntity) {
this.chapterEntity = chapterEntity;
}
@Id
@Column(name = "id", nullable = false)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Basic
@Column(name = "sequence", nullable = false)
public int getSequence() {
return sequence;
}
public void setSequence(int sequence) {
this.sequence = sequence;
}
@Basic
@Column(name = "content", nullable = false, length = 1000)
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
/*
@Basic
@Column(name = "chapter_id", nullable = false)
public long getChapterId() {
return chapterId;
}
public void setChapterId(long chapterId) {
this.chapterId = chapterId;
}
*/
@Basic
@Column(name = "type", nullable = false)
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ChapterTitle1Entity that = (ChapterTitle1Entity) o;
if (id != that.id) return false;
if (sequence != that.sequence) return false;
// if (chapterId != that.chapterId) return false;
if (type != that.type) return false;
if (content != null ? !content.equals(that.content) : that.content != null) return false;
return true;
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + sequence;
result = 31 * result + (content != null ? content.hashCode() : 0);
// result = 31 * result + (int) (chapterId ^ (chapterId >>> 32));
result = 31 * result + type;
return result;
}
}
ChapterTitle3Entity.java
package com.hnu.tutorial.model.entity;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import javax.persistence.*;
import java.util.List;
/**
* Created by shiqin_zhang@qq.com on 2016-09-9/6/16
* Time 11:16 PM
*/
@Entity
@Table(name = "chapter_title_2")
public class ChapterTitle2Entity {
private long id;
private int sequence;
private String content;
// private long title1Id;
private List<ChapterTitle3Entity> chapterTitle3EntityList;
private ChapterTitle1Entity chapterTitle1Entity;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "title_1_id", nullable = false)
public ChapterTitle1Entity getChapterTitle1Entity() {
return chapterTitle1Entity;
}
public void setChapterTitle1Entity(ChapterTitle1Entity chapterTitle1Entity) {
this.chapterTitle1Entity = chapterTitle1Entity;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "chapterTitle2Entity")
@Fetch(FetchMode.SUBSELECT)
public List<ChapterTitle3Entity> getChapterTitle3EntityList() {
return chapterTitle3EntityList;
}
public void setChapterTitle3EntityList(List<ChapterTitle3Entity> chapterTitle3EntityList) {
this.chapterTitle3EntityList = chapterTitle3EntityList;
}
@Id
@Column(name = "id", nullable = false)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Basic
@Column(name = "sequence", nullable = false)
public int getSequence() {
return sequence;
}
public void setSequence(int sequence) {
this.sequence = sequence;
}
@Basic
@Column(name = "content", nullable = false, length = 1000)
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
/*
@Basic
@Column(name = "title_1_id", nullable = false)
public long getTitle1Id() {
return title1Id;
}
public void setTitle1Id(long title1Id) {
this.title1Id = title1Id;
}
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ChapterTitle2Entity that = (ChapterTitle2Entity) o;
if (id != that.id) return false;
if (sequence != that.sequence) return false;
// if (title1Id != that.title1Id) return false;
if (content != null ? !content.equals(that.content) : that.content != null) return false;
return true;
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + sequence;
result = 31 * result + (content != null ? content.hashCode() : 0);
// result = 31 * result + (int) (title1Id ^ (title1Id >>> 32));
return result;
}
}
我的DAO图层package com.hnu.tutorial.model.entity;
import javax.persistence.*;
/**
* Created by shiqin_zhang@qq.com on 2016-09-9/6/16
* Time 11:16 PM
*/
@Entity
@Table(name = "chapter_title_3")
public class ChapterTitle3Entity {
private long id;
private int sequence;
private String content;
// private long title2Id;
private ChapterTitle2Entity chapterTitle2Entity;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "title_2_id", nullable = false)
public ChapterTitle2Entity getChapterTitle2Entity() {
return chapterTitle2Entity;
}
public void setChapterTitle2Entity(ChapterTitle2Entity chapterTitle2Entity) {
this.chapterTitle2Entity = chapterTitle2Entity;
}
@Id
@Column(name = "id", nullable = false)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Basic
@Column(name = "sequence", nullable = false)
public int getSequence() {
return sequence;
}
public void setSequence(int sequence) {
this.sequence = sequence;
}
@Basic
@Column(name = "content", nullable = false, length = 1000)
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
/*
@Basic
@Column(name = "title_2_id", nullable = false)
public long getTitle2Id() {
return title2Id;
}
public void setTitle2Id(long title2Id) {
this.title2Id = title2Id;
}
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ChapterTitle3Entity that = (ChapterTitle3Entity) o;
if (id != that.id) return false;
if (sequence != that.sequence) return false;
if (content != null ? !content.equals(that.content) : that.content != null) return false;
return true;
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + sequence;
result = 31 * result + (content != null ? content.hashCode() : 0);
return result;
}
}
ChapterDAO.java
我的服务层package com.hnu.tutorial.model.dao;
import com.hnu.tutorial.model.entity.ChapterEntity;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* Created by shiqin_zhang@qq.com on 2016-09-9
* Time 11:13 PM
*/
@Repository
public class ChapterDAO extends BaseDAO<ChapterEntity> {
public List<ChapterEntity> chapterEntityList(){
Criteria criteria = session().createCriteria(ChapterEntity.class, "chapter");
criteria.createAlias("chapter.chapterTitle1EntityList", "title1List");
criteria.addOrder(Order.desc("title1List.sequence"));
List<ChapterEntity> chapterEntityList = criteria.list();
return chapterEntityList;
/*String hql = "from ChapterEntity";
List<ChapterEntity> chapterEntityList = session().createQuery(hql).list();
return chapterEntityList;*/
}
}
ChapterSvc
每当我查询章节列表时,我都会得到所有子列表。即使我在服务层的方法package com.hnu.tutorial.service.impl;
import com.hnu.tutorial.model.dao.ChapterDAO;
import com.hnu.tutorial.model.entity.ChapterEntity;
import com.hnu.tutorial.model.entity.ChapterTitle1Entity;
import com.hnu.tutorial.service.dto.ChapterDTO;
import com.hnu.tutorial.service.dto.ChapterTitle1DTO;
import com.hnu.tutorial.service.interfaces.IChapterSvc;
import com.hnu.tutorial.utils.BeanMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* Created by shiqin_zhang@qq.com on 2016-09-10
* Time 10:59 AM
*/
@Service
@Transactional
public class ChapterSvc implements IChapterSvc {
@Autowired
private ChapterDAO chapterDAO;
@Override
public List<ChapterDTO> chapterDTOList() {
List<ChapterEntity> chapterEntityList = chapterDAO.chapterEntityList();
List<ChapterDTO> chapterDTOList = BeanMap.mapList(chapterEntityList, ChapterDTO.class);
for(ChapterDTO chapterDTO:chapterDTOList){
List<ChapterTitle1Entity> chapterTitle1EntityList = chapterDTO.getChapterTitle1EntityList();
for (ChapterTitle1Entity chapterTitle1Entity:chapterTitle1EntityList){
chapterTitle1Entity.setChapterEntity(null);
}
}
return chapterDTOList;
}
}
的末尾设置断点,我也得到所有子列表。我也尝试使用hql查询列表,但它仍然无效。
答案 0 :(得分:1)
在FetchType.EAGER
注释中将FetchType.LAZY
更改为@OneToMany
。只有当你直接引用它时,这才会让Hibernate加载一个集合。