NutzCN Logo
问答 nutz Dao 查询映射实体类赋值问题。
发布于 2556天前 作者 苍蓝猛兽 3188 次浏览 复制 上一个帖子 下一个帖子
标签:
package com.elvish.oda.busin.pojo;

import java.util.Date;

import org.nutz.dao.entity.annotation.Column;
import org.nutz.dao.entity.annotation.Id;
import org.nutz.dao.entity.annotation.Table;
import org.nutz.json.Json;

@Table("t_user")
public class User {

	@Id
	private Integer id;
	@Column
	private String username;
	@Column
	private String password;
	@Column
	private String name;
	@Column("login_time")
	private Date loginTime;
	@Column
	private Integer delflag;
	@Column
	private Integer status;
	@Column
	private String remark;
	@Column("create_time")
	private Date createTime;
	@Column("update_time")
	private Date updateTime;
	@Column("create_user")
	private Date createUser;
	@Column("update_user")
	private Date updateUser;

	private String roleName;

	public Integer getId() {
		return id;
	}

	public void setId(Integer 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 String getName() {
		return name;
	}

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

	public Date getLoginTime() {
		return loginTime;
	}

	public void setLoginTime(Date loginTime) {
		this.loginTime = loginTime;
	}

	public Integer getDelflag() {
		return delflag;
	}

	public void setDelflag(Integer delflag) {
		this.delflag = delflag;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public Date getCreateUser() {
		return createUser;
	}

	public void setCreateUser(Date createUser) {
		this.createUser = createUser;
	}

	public Date getUpdateUser() {
		return updateUser;
	}

	public void setUpdateUser(Date updateUser) {
		this.updateUser = updateUser;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	@Override
	public String toString() {
		return Json.toJson(this);
	}

}

public List<User> findAllUserRole() {
		String querySql = "SELECT t1.*, t2.role_name roleName FROM t_user_role t0 LEFT JOIN t_user t1 ON t0.user_id = t1.id LEFT JOIN t_role t2 ON t0.role_id = t2.id";
		Sql sql = Sqls.fetchString(querySql);
		return NutzSqlUtil.getEntities(sql, dao, User.class);
	}
public static <T> List<T> getEntities(Sql sql, Dao dao, Class<T> clz) {
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao.getEntity(clz));
		dao.execute(sql);
		return sql.getList(clz);
	}

sql

SELECT
	t1.*, t2.role_name roleName
FROM
	t_user_role t0
LEFT JOIN t_user t1 ON t0.user_id = t1.id
LEFT JOIN t_role t2 ON t0.role_id = t2.id

进行数据库查询,想带出非User表中的roleName信息。
我在User实体中定义了roleName属性,settter/getter。

问题
执行dao操作后,User中roleName不能被赋值? why??

7 回复

@qq_9450e761 看了一下,必须使用视图啊。我这里并没有使用视图,就是一个sql查询。

加上@Readonly注解

package com.elvish.oda.busin.pojo;

import java.util.Date;

import org.nutz.dao.entity.annotation.Column;
import org.nutz.dao.entity.annotation.Id;
import org.nutz.dao.entity.annotation.Readonly;
import org.nutz.dao.entity.annotation.Table;
import org.nutz.json.Json;

@Table("t_user")
public class User {

	@Id
	private Integer id;
	@Column
	private String username;
	@Column
	private String password;
	@Column
	private String name;
	@Column("login_time")
	private Date loginTime;
	@Column
	private Integer delflag;
	@Column
	private Integer status;
	@Column
	private String remark;
	@Column("create_time")
	private Date createTime;
	@Column("update_time")
	private Date updateTime;
	@Column("create_user")
	private Date createUser;
	@Column("update_user")
	private Date updateUser;
	@Readonly
	private String roleName;

	public Integer getId() {
		return id;
	}

	public void setId(Integer 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 String getName() {
		return name;
	}

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

	public Date getLoginTime() {
		return loginTime;
	}

	public void setLoginTime(Date loginTime) {
		this.loginTime = loginTime;
	}

	public Integer getDelflag() {
		return delflag;
	}

	public void setDelflag(Integer delflag) {
		this.delflag = delflag;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public Date getCreateUser() {
		return createUser;
	}

	public void setCreateUser(Date createUser) {
		this.createUser = createUser;
	}

	public Date getUpdateUser() {
		return updateUser;
	}

	public void setUpdateUser(Date updateUser) {
		this.updateUser = updateUser;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	@Override
	public String toString() {
		return Json.toJson(this);
	}

}

@wendal @qq_9450e761 已将代码User中的roleName加上@Readonly注解,User中roleName依然不能被赋值。

加上@Column试试

值取到了,但我不是很理解啊。版本是1.r.63-SNAPSHOT.jar

添加回复
请先登陆
回到顶部