就是,有两个表,主表是Role ,从表是User ,是一对多的关系。怎查询主表,带出从表的数据。
实体类:Role
public class Role {
private Integer roleid;
private String rolename;
private Set<User> users;
private User user;
实体类:User
public class User {
private Integer id;
private String username;
private String sex;
private Role role;
Role.hbm.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.test.bean.Role" table="T_Role">
<id name="roleid" type="java.lang.Integer" column="roleid">
<generator class="increment"></generator>
</id>
<property name="rolename" type="string" column="rolename" not-null="false" length="50" lazy="false"></property>
<set name="users" inverse="true" lazy="false">
<key column="rid"
foreign-key="fk_user_rid_role_id"
not-null="true"/>
<one-to-many class="com.test.bean.User" />
</set>
</class>
</hibernate-mapping>
User.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.test.bean.User" table="t_user" >
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="username" type="java.lang.String">
<column name="username" length="50" />
</property>
<property name="sex" type="java.lang.String">
<column name="sex" length="50" />
</property>
<!--
<many-to-one name="role" foreign-key="fk_user_rid_role_id" lazy="false">
<column name="rid" not-null="false"/>
</many-to-one>
-->
<many-to-one name="role" class="com.test.bean.Role" column="rid" lazy="false"></many-to-one>
</class>
</hibernate-mapping>
ListRoleAction.java
public class ListRoleAction extends ActionSupport
{
private RoleService service;
public RoleService getService() {
return service;
}
public void setService(RoleService service) {
this.service = service;
}
@SuppressWarnings("unchecked")
@Override
public String execute() throws Exception
{
Map request = (Map) ActionContext.getContext().get("request");
request.put("list", service.findAllRole());
return SUCCESS;
}
}
jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'list.jsp' starting page</title>
<script type="text/javascript">
</script>
</head>
<body>
<center>
<table border="1">
<s:iterator value="#request.list" id="us" status="s">
<tr>
<td>编号</td>
<td>
<label>
<s:property value="#us.roleid"/>
</label>
</td>
<td rowspan="2"> </td>
</tr>
<tr>
<td>角色:</td>
<td><s:property value="#us.rolename"/></td>
</tr>
<tr>
<td>用户名称</td>