【发布时间】:2012-11-21 02:24:16
【问题描述】:
我有一个 JSP 页面,我想以这种方式显示会话中的一些信息
<div>Fonction : <s:property value="%{#session.employe.emploi.nomEmploi}"/>
但问题是它什么都不显示。
所以为了解释一下我有一个包含另一个类emploi的员工类,这里是示例代码
雇员:
public class Employe
{
private Long employeId;
private Emploi emploi;
private String nom;
private String prenom;
private String adresse;
private String civilite;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "ID", unique = true, nullable = false)
public Long getEmployeId() {
return this.employeId;
}
public void setEmployeId(Long employeId) {
this.employeId = employeId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "EMP_ID", nullable = false)
public Emploi getEmploi() {
return this.emploi;
}
public void setEmploi(Emploi emploi) {
this.emploi = emploi;
}
@Column(name = "NOM", nullable = false)
public String getNom() {
return this.nom;
}
public void setNom(String nom) {
this.nom = nom;
}
@Column(name = "PRENOM", nullable = false)
public String getPrenom() {
return this.prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
@Column(name = "ADRESSE", nullable = false)
public String getAdresse() {
return this.adresse;
}
public void setAdresse(String adresse) {
this.adresse = adresse;
}
@Column(name = "CIVILITE", nullable = false, length = 20)
public String getCivilite() {
return this.civilite;
}
public void setCivilite(String civilite) {
this.civilite = civilite;
}
}
员工:
public class Emploi
{
private Long emploiId;
private String nomEmploi;
private Set<Employe> employes = new HashSet<Employe>(0);
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "EMP_ID", unique = true, nullable = false)
public Long getEmploiId() {
return this.emploiId;
}
public void setEmploiId(Long emploiId) {
this.emploiId = emploiId;
}
@Column(name = "NOM_EMPLOI", nullable = false)
public String getNomEmploi() {
return this.nomEmploi;
}
public void setNomEmploi(String nomEmploi) {
this.nomEmploi = nomEmploi;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "emploi")
public Set<Employe> getEmployes() {
return this.employes;
}
public void setEmployes(Set<Employe> employes) {
this.employes = employes;
}
}
任何帮助将不胜感激。
提前致谢。
【问题讨论】:
-
您是如何定义员工的?
-
在我的 jsp 页面中,我可以像这样访问 nom 和 prenom 属性:
但对于 Emploi 我不知道有什么问题 -
您是从数据库中获取对象吗?也许你的
Emploi类没有被初始化。在进入会话之前在你的代码中检查它。 -
感谢 @AleksandrM 的提示,我已通过将 Employe 类中的 @ManyToOne(fetch = FetchType.LAZY) 注释更改为 @ManyToOne(fetch = FetchType.EAGER) 来解决此问题,如下所示: @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "EMPLOI_ID", nullable = false) public Emploi getEmploi() { return this.emploi; }
标签: hibernate session struts2 ognl