- 爱易网页
-
Java教程
- SSH里怎查询两个表得数据显示在jsp里呢?该怎么解决
日期:2014-05-17 浏览次数:20782 次
SSH里怎查询两个表得数据显示在jsp里呢?
就是,有两个表,主表是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>