我被困在一個有問題的項目中。問題很簡單,但我不知道解決方案。這是我的問題:使用struts和hibernate在jsp中顯示數據
我有一個Employee類與Hibernate,Employee Table映射。
Class Employee
int empId;
int empName;
然後我有一個Assignment類與Hibernate,Assignment Table映射。對於每位員工,我有一份記錄在我的Assignment班以及一名員工的主管編號。主管也在Employee類中定義。現在
Class Assignment
int empId;
int supervisorId; //empId of the Supervisor in the Employee Table.
,在我的動作類,我產生一個迭代器首先具有分配類的一個對象,我在我的JSP像下面顯示的:
<s:iterator value="assignmentList">
<s:property value="empId"/>
<s:property value="supervisorId"/>
//Now here i want to display the empName of both the Employee and Supervisor.
//How can i pass the <s:property value="empId"/> and <s:property value="supervisorId"/> values to an action to get the Employee Class Objects so that i can display the employee names.
我認爲它是一個簡單的問題加入兩個表,但不知道,如何在struts2中實現它。請幫忙。
編輯
這是我修改後的代碼
Employee類
@Entity
@Table(name="usertable")
public class User implements java.io.Serializable{
private int id;
private String empName;
private String firstName;
private Assignment assignment;
public Employee(){
}
@Id
@GeneratedValue
@Column(name="id")
public int getId() {
return id;
}
@Column(name="empname")
public String getEmpname() {
return empName;
}
@OneToOne(fetch = FetchType.EAGER, mappedBy = "usertable", cascade = CascadeType.ALL)
public Assignment getAssignment() {
return assignment;
}
public void setAssignment(Assignment assignment) {
this.assignment = assignment;
}
}
分配類
@Entity
@Table(name="assignment")
public class Assignment implements java.io.Serializable{
private int id;
private Employee employee;
@Id
@GeneratedValue
@Column(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "id")
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}
保存方法在Action類
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Employee emp=new Employee();
Assignment assgn =new Assignment();
emp.setAssignment(assgn);
assgn.setEmployee(emp);
session.save(assgn);
session.save(emp);
session.getTransaction().commit();
session.close();
我的Util類:
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new AnnotationConfiguration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
當我在我的動作類運行的保存方法,我得到這個異常:
Struts has detected an unhandled exception:
Messages:
File: org/hibernate/cfg/OneToOneSecondPass.java
Line number: 135
java.lang.NullPointerException
org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:135)
org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1163)
org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1319)
common.HibernateUtil.(HibernateUtil.java:14)
backEnd.admin.saveemployee(admin.java:137)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
現在該怎麼解決這個?我正在使用Hibernate註釋版本3.2.1。請幫忙。
你能改變你的實體嗎?所以你的'Assignment'類擁有'Employee'的引用。 –
試過這個。得到一個例外,請看看。 –
這個問題的標題沒有意義。 –