2013-11-21 87 views
0

我被困在一个有问题的项目中。问题很简单,但我不知道解决方案。这是我的问题:使用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。请帮忙。

+0

你能改变你的实体吗?所以你的'Assignment'类拥有'Employee'的引用。 –

+0

试过这个。得到一个例外,请看看。 –

+0

这个问题的标题没有意义。 –

回答

0

您需要在此处配置一对一(或一对多,取决于您的型号)映射。既然你提到你对每个员工都有一个赋值记录,所以这意味着Employee bean应该有一个对其相关赋值bean的引用。

我假设你正在使用注解配置休眠所以在你的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 
} 

设置这些字段时,你的动作类叫做,创建的getter/setter方法这个bean和遍历这个bean一旦你的JSP被调用,就像你迭代在您的文章中提到的其他豆。

+0

谢谢,我会试试这个,让你知道它是怎么回事。你提到的好习惯意味着,我将为所有实体bean保留一个单独的Bean来显示目的。这些单独的bean将不会映射到数据库,对吗?这是如何改进设计和性能的。我问,因为我在这些设计中是新的。感谢 –

+1

单独的bean将仅用于显示目的,不会与数据库映射。我不能说性能,但它提供了数据层和视图层之间的良好分离。数据bean(与数据库映射)应该只包含表中的字段,而另一方面,您可以按照您的要求更改显示bean中的字段,以了解如何在页面上显示。就像例如数据bean可能包含字段firstName和lastName,而在显示bean中,您可以将它们合并到单个字段UserName = firstName + lastName中。 –

相关问题