Hibernate多表查询显示数据到jsp

今天做一个小程序,因为是一个新手,碰到了一个其实也不大的问题,但是不知道怎么去解决,后来网上找资料,终于找到了一个办法。

我要从两个表中有外键关联的表中去查询数据,首先,在model类中设定好关系,many2one等等,因为我做的是一个查看请假审批结果,一个user类有多个请假数据,所以在请假的详情这个实体类中写了一个映射关系,many2one,然后使用HibernateTemplate.find()获取所有的数据保存list中,在action处理方法中把list数据存到request中去,然后在页面中使用遍历。看代码

这里是user实体类

package com.OASystem.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "T_user")
public class User {
	private int id;
	private String username;
	private String password;
	private String loginname;

	public String getLoginname() {
		return loginname;
	}

	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}

	private int type;

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public User() {

	}
}
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "T_askfroleavel")
public class AskForLeavel {
	private int id;
	private Date datetime;
	private String timeTnterval;
	private String type;
	private String reason;
	private User user;
	private String state;

	@Column(nullable = true, insertable=false)
	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	@ManyToOne
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Date getDatetime() {
		return datetime;
	}

	public void setDatetime(Date datetime) {
		this.datetime = datetime;
	}

	public String getTimeTnterval() {
		return timeTnterval;
	}

	public void setTimeTnterval(String timeTnterval) {
		this.timeTnterval = timeTnterval;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}
}
这里是查询结果
	@Override
	public List getAskForLeavelInfo() {
		return hibernateTemplate.find("from AskForLeavel");
	}




 

getRequest().setAttribute("AskForLeavellist", askForLeavellist);


 


				
					
					
					
					
					
					
				
			


 

你可能感兴趣的:(JavaEE)