我陷入了一个有问题的项目。问题很简单,但我不知道解决方案。这是我的问题:
我有一个使用Hibernate,Employee Table映射的Employee类。
Class Employee
int empId;
int empName;
然后我有一个使用Hibernate,Assignment Table映射的Assignment类。对于每个员工,我在Assignment类中都有一个记录以及一个员工的主管ID。主管也在Employee类中定义。
Class Assignment
int empId;
int supervisorId; //empId of the Supervisor in the Employee Table.
现在,在我的动作类中,我首先生成了一个迭代器,它首先有一个Assignment Class的对象,我将在我的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中实现它。请帮忙。
修改
这是我修改后的代码
员工类
@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;
}
}
当我在我的动作类中运行save方法时,我遇到了这个异常:
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。请帮忙。
答案 0 :(得分:0)
您需要在此处配置一对一(或一对多,具体取决于您的型号)映射。由于您提到您有针对每个员工的分配记录,因此这意味着Employee bean应该具有对其相关分配bean的引用。
我假设你在hibernate中使用注释配置,所以你的Employee类中的一些小改动应该可以解决这个问题
//Injecting the assignment bean inside the employee bean
private Assignement employeeAssignement;
//Lazy type fetching means to get the assignemnt tuple only when it is needed
@OneToOne(fetch = FetchType.LAZY, mappedBy = "Employee", cascade = CascadeType.ALL)
public Assignment getEmployeeAssignment() {
return this.employeeAssignement;
}
public void setEmployeeAssignement(EmployeeAssignement employeeAssignement) {
this.employeeAssignement = employeeAssignement;
}
另一个优良做法是保留一个单独的bean,用于显示与数据库映射的实体bean的用途。 创建一个bean,比如EmployeeDetailBean,并指定要在JSP上显示的字段。
public class EmployeeDetailBean{
//The fields you want to display on the JSP
......
//valid getter/setter
}
在调用操作类时设置这些字段,为此bean创建getter / setter,并在调用JSP时迭代此bean,就像迭代帖子中提到的其他bean一样。