如何通过使用Strius2和Hibernate3实体映射关系从Code获取Name以显示在JSP上的记录

时间:2014-06-19 07:28:27

标签: java jsp struts2 hibernate3

我有两个表Group(Parent)Type(Child),它们由GroupId映射。这是由Hibernate @OneToMany Mapping。

完成的

我的问题是当我在 JSP 中显示Type Table的记录时,它显示TypeName& GroupId因为我正在使用TypeEntity类,但我想显示TypeNameGroupName而不是GroupId

尽可能简单。像:SELECT A.TYPE_NAME, B.GROUP_NAME FROM TYPE A, GROUP B WHERE A.GROUP_ID = B.GROUP_ID;

目前我正在使用:

 List<UserTypeEntity> typeList = criteria.add(Restrictions.eq("userActive", 
"Y").ignoreCase()).addOrder(Property.forName("userTypeName").asc()).list();

只需帮助解决这个问题,我如何使用 hibernate3

进行approch

我的TypeEntity类:

@Entity
@Table(name="USER_TYPE_MSTR")
public class UserTypeEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="USER_TYPE_ID")
    private int userTypeId;

    @Column(name="USER_TYPE")
    private String userTypeName;

    @Column(name="USER_TYPE_DESC")
    private String userTypeDesc;

//  @ManyToOne
    @Column(name="USER_GRP_ID")
    private int userGroupId;

          ..... Setter Getters....
}

我的GroupEntity Class:

@Entity
@Table(name="USER_GROUP_MSTR")
public class UserGroupEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="USER_GRP_ID")
    private int userGroupId;

    @Column(name="USER_GRP")
    private String userGroupName;

    @Column(name="USER_GRP_DESC")
    private String userGroupDesc;


    @Column(name="ACTIVE")
    private String userActive;

    @OneToMany(cascade=CascadeType.ALL)
    @JoinTable( name                ="USER_GROUP_TYPE", 
                joinColumns         = {@JoinColumn(name="GROUP_ID", referencedColumnName="USER_GRP_ID")},
                inverseJoinColumns  = {@JoinColumn(name="TYPE_ID",  referencedColumnName="USER_TYPE_ID")}
    )
    private Set<UserTypeEntity> userTypeEntities;

    /**
     * @return the userTypeEntities
     */
    public Set<UserTypeEntity> getUserTypeEntities() {
        return userTypeEntities;
    }

    /**
     * @param userTypeEntities the userTypeEntities to set
     */
    public void setUserTypeEntities(Set<UserTypeEntity> userTypeEntities) {
        this.userTypeEntities = userTypeEntities;
    }
          .... Getters & Setters ....
}

我的DAO课程:

public class UserTypeDAO {
private List<UserTypeEntity> typeList = null;
private Criteria criteria;
@SuppressWarnings("unchecked")
    public List<UserTypeEntity> getUserTypes() throws HibernateException{
        log.info("...In UserTypeDAO.getUserTypes...");
        try {
            criteria = ConnectionFactory.getSessionfactory().openSession().createCriteria(UserTypeEntity.class);
            typeList = criteria.add(Restrictions.eq("userActive", "Y").ignoreCase()).addOrder(Property.forName("userTypeName").asc()).list();
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return typeList;
    }
}

我的行动类:

public class UserTypeAction extends ActionSupport implements Preparable{
private List<UserTypeEntity> typeList;
@SkipValidation
    public String userTypeView() throws Exception{
        try {
            typeList = (List<UserTypeEntity>)UserTypeController.getUserTypes();
            if(typeList.size() > 0){
                setTypeList(typeList);              

                result = SUCCESS;
            }else {
                result = SUCCESS;
            }
        } catch (Exception e) {
            result = ERROR;
            e.printStackTrace();
        }
        return result;
    }
}

和我的JSP:

<s:if test="%{typeList.size() != 0}">
                <s:iterator value="typeList" var="type">
                    <tr>
                        <td align="left"><s:property value="userTypeName"/></td>
                        <td align="left"><s:property value="userTypeDesc"/></td>
                        <td align="left"><s:property value="userGroupId"/></td>
                        <td align="center"><s:date name="userCreatedDate" format="dd-MMM-yyyy" /></td>
                        <td align="center"><s:date name="userModifiedDate" format="dd-MMM-yyyy" /></td>
                        <td align="center"><b><a href="#" onclick="javascript:userTypeEdit('edit','${userTypeId}','${userTypeName}','${userTypeDesc}','${userGroupId}','${userCreatedDate}','${userModifiedDate}');">Edit</a></b>
                            &nbsp;&nbsp;|&nbsp;&nbsp;<b><a href="#" onclick="javascript:userTypeDelete('userTypeDelete.action?typeId=${userTypeId}');">Delete</a></b>
                        </td>
                    </tr>
                </s:iterator>
            </s:if>
            <s:else>
                <tr>
                    <td colspan="100%"><div style="color:red;">No Results found</div></td>
                </tr>
            </s:else>

1 个答案:

答案 0 :(得分:0)

我是在类型实体类中创建的

@ManyToOne
    @JoinColumn(name="USER_GRP_ID")
    private UserGroupEntity userGroupEntity;

在集团实体中:

@OneToMany(mappedBy="userGroupEntity")
    private Set<UserTypeEntity> userTypeEntities;

然后在JSP中我访问了Like:

<td align="left"><s:property value="userGroupEntity.userGroupDesc"/></td>

我的问题得到了解决......