【发布时间】:2015-09-12 15:26:56
【问题描述】:
我遇到了对象的休眠和延迟加载问题。 基本上我想加载一个具有热切加载字段的类,而不是加载子类的惰性字段
参加以下 QuestionVO 课
@Entity
@Table(name = "question")
public class QuestionVO extends BaseDAOVO implements Serializable {
/**
*
*/
private static final long serialVersionUID = -5867047752936216092L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@Column(name = "questionText", unique = false, nullable = false, length = 4000)
@Size(min = 3, max = 4000)
@Pattern(regexp = MobileAppsRegexConstants.GENERAL_ALLOWED_CHARCHTERS, message = "Question Text Not valid.")
private String questionText;
@ManyToOne(fetch = FetchType.EAGER)
@Cascade({ CascadeType.SAVE_UPDATE })
@JoinColumn(name = "MENU_STYLE_ID", nullable = true)
private MenuStyleVO menuStyle;
}
采取以下 MenuStyleVO 类
@Entity
@Table(name = "menu_style")
public class MenuStyleVO extends BaseDAOVO implements Serializable{
/**
*
*/
private static final long serialVersionUID = 3697798179195096156L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@Column(name = "menuStyleName", unique = false, nullable = false, length = 200)
private String menuStyleName;
@Column(name = "menuTemplate", unique = false, nullable = false, length = 200)
private String menuTemplate;
@OneToOne(fetch = FetchType.LAZY, optional=false)
@Cascade({ CascadeType.SAVE_UPDATE })
@JoinColumn(name="logo_id")
@JsonProperty("logo")
private ApplicationImageVO logo;
}
还有这个 ApplicationImageVO 类
@Entity
@Table(name = "application_image")
public class ApplicationImageVO extends BaseDAOVO implements Serializable {
/**
*
*/
private static final long serialVersionUID = -9158898930601867545L;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "image1242x2208")
@Cascade({ CascadeType.ALL })
@JsonIgnore
private SubmissionLauncherImagesVO launcherImage1242x2208;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "image1536x2048")
@Cascade({ CascadeType.ALL })
@JsonIgnore
private SubmissionLauncherImagesVO launcherImage1536x2048;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "image2048x1536")
@Cascade({ CascadeType.ALL })
@JsonIgnore
private SubmissionLauncherImagesVO launcherImage2048x1536;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "logo")
@Cascade({ CascadeType.ALL })
@JsonIgnore
private MenuStyleVO menuStyleLogo;
}
如果 L 使用以下休眠标准代码从数据库中加载 QuestionVO 类 - MenuStyleVO 和 ApplicationImageVO 的所有惰性字段也会被加载。
在复杂的用例中,这会导致查询变得非常慢
public QuestionVO findMasterAppQuestionById(int id) {
Criteria criteria = currentSession().createCriteria(QuestionVO.class);
criteria.add(Restrictions.eq("id", id));
QuestionVO questionVO = (QuestionVO) criteria.uniqueResult();
return questionVO;
}
我想知道的是 - 是否可以加载 QuestionVO 类及其急切字段并告诉 hibernate 忽略其他类中的惰性字段,而不是那些需要的字段?
干杯 达米安
【问题讨论】:
标签: java hibernate hibernate-criteria