Hibernate根据查询内容封装成对象(DTO)

本文详细解析了在使用Hibernate进行查询并映射到自定义DTO对象时遇到的构造函数异常问题。具体展示了如何确保DTO类的构造函数与查询字段相匹配,避免出现PropertyNotFoundException。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

List<SpacesDTO> list=session.createQuery("select new dto.SpacesDTO(s.id, s.num, s.state, s.type, o.name) from Spaces s inner join s.owner o").list();


SpacesDTO属性必须和查询信息一致 (s.id, s.num, s.state, s.type, o.name) 


package dto;

public class SpacesDTO {

	private int id;
	private String num;
	private String state;
	private String type;
	private String name;

	public SpacesDTO(int id, String num, String state, String type, String name) {
		super();
		this.id = id;
		this.num = num;
		this.state = state;
		this.type = type;
		this.name = name;
	}
	
	public int getId() {
		return id;
	}

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

	public String getNum() {
		return num;
	}

	public void setNum(String num) {
		this.num = num;
	}

	public String getState() {
		return state;
	}

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

	public String getType() {
		return type;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

注意:在SpacesDTO中必须根据属性(与查询内容对应)创建构造函数,否则会抛异常:[cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: dto.SpacesDTO].

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值