hibernate中最关键的就是关系如果你是查权限,直接查权限的对象,然后按照关系将对应的教师和课程取出
解决方案 »
- java.lang.ClassNotFoundException: com.opensymphony.xwork2.util.TextUtils
- 我刚做了个Socket聊天程序,现在还想做一个发送文件的功能
- strust2 标签问题~~
- 嵌入式tomcat下strut2启动报错问题
- struts2得问题
- org.apache.log4j 与 org.apache.commons.logging
- 如何利用<html:message>标签将消息资源包中验证失败的消息输出到页面?
- Error:The method equlas(String) is undefined for the type String
- EJB与多个模块通信,而且这些模块可加可减
- 急问:J2EE构架
- tomcat 问题!!!getJspApplicationContext(ServletContext)
- 现求hibernate中多表查询语句?
你直接拿出来就可以啊,查询语句hibernate自动生成,你应该看看hibernate的对应关系
一对多,多对多啊
教师 js;
课程 kc;
}
权限.hbm.xml<one-to-one class="教师" constrained="true" />
<one-to-one class="课程" constrained="true" />
test.java权限 qx = (权限)session.createQuery("from 权限").list().get(0);
qx.getJs();
qx.getKc();
课程表 Course
配置文件 course.xml
<hibernate-mapping package="com.po">
<class name="Course" table="Course">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="name" />
<set name="teachers" table="course_teacher" inverse="true">
<key column="id"/>
<many-to-many column="teacherid"
class="Teacher"/>
</set>
</class>
</hibernate-mapping>
Course类
public class Course{
private long id;
private String name;
private Set teachers;
}
教师表 Teacher
配置文件
<hibernate-mapping package="com.po">
<class name="Teacher" table="Teacher">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="name" />
<set name="courses" table="course_teacher" inverse="true">
<key column="id"/>
<many-to-many column="courseid"
class="Course"/>
</set>
</class>
</hibernate-mapping>
教师类
public class Teacher{
private long id;
private String name;
private Set courses;
}权限表就不用写配置文件了,对应于配置文件中course_teacher关联表。
通过教师查课程, teacher.getCourses();
通过课程查教师, course.getTeacher();