我被困在一个有问题的项目中。问题很简单,但我不知道解决方案。这是我的问题:使用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'的引用。 –
试过这个。得到一个例外,请看看。 –
这个问题的标题没有意义。 –