NutzCN Logo
问答 fetchLinks 查询问题
发布于 2631天前 作者 wx_c01kjs3phq3su2fts0u3 2149 次浏览 复制 上一个帖子 下一个帖子
标签:

多对多关系:

@ManyMany(relation = "sys_role_user",from = "user_account:user_account",  to = "pk_role_id")
    private List<SysRole> roles;

java代码:

log.info(user.getRoles());
            log.info(user.getUser_account());
            dao.fetchLinks(user, null);
            log.info(user.getRoles());

日志:

[INFO ] 2017-10-09 19:57:56,873 com.kmia.mdkj.contract.action.sys.loginAction 
[INFO ] 2017-10-09 19:57:56,875 com.kmia.mdkj.contract.action.sys.loginAction admin
[DEBUG] 2017-10-09 19:57:56,880 org.nutz.dao.impl.sql.run.NutDaoExecutor SELECT * FROM sys_role  WHERE pk_role_id IN (SELECT pk_role_id FROM sys_role_user WHERE user_account=45)
[INFO ] 2017-10-09 19:57:56,929 com.kmia.mdkj.contract.action.sys.loginAction []

其中的 45 是user的id值,不是说:
你为 from 声明了一个 "pid:name",这个冒号后面的,就是大声告诉 NutDao 请用 Pet.name 来映射这个 pid 字段。这样 NutDao 就不会自己瞎猜了。

11 回复

没看懂你想说什么

按照@ManyMany 的设置 应该是:

SELECT * FROM sys_role  WHERE pk_role_id IN (SELECT pk_role_id FROM sys_role_user WHERE user_account=‘admin’)

条件应该是

SELECT pk_role_id FROM sys_role_user WHERE user_account=user.getUser_account

实际上是

SELECT pk_role_id FROM sys_role_user WHERE user_account=user.getId

这个 from = "user_account:user_account" 没起作用啊 ,user_account 是普通的字段,没加@Id @Name

from = "user_account:user_account" 

冒号后面不是已经指定了user_account字段 ,而nutz在执行时 却使用了下面这个

@Id
 private int pk_user_id

求解答

3个POJO都贴一下, 感觉哪里不对呢

主要逻辑在 ManyManyLinkField 里面

user类:

@Table("sys_user")
public class SysUser implements Serializable {
    /**
     * 用户信息主键id
     */
    //@Id
    private int pk_user_id;
    /**
     * 工号
     */
    @Excel(name = "工号", width = 20, orderNum = "4")
    private String user_employee_id;
    /**
     * 用户姓名
     */

    @Excel(name = "用户姓名", width = 20, orderNum = "5")
    private String user_name;
    /**
     * 用户登录账号
     */
    @Name
    @Excel(name = "登陆帐号", width = 20, orderNum = "8")
    private String user_account;
    /**
     * 用户性别 1:男 0:女
     */
    @Excel(name = "性别", width = 20, orderNum = "6", replace = {"男_1", "女_0"})
    private Integer user_sex;
    /**
     * 用户联系电话
     */
    @Excel(name = "联系电话", width = 20, orderNum = "10")
    private String user_telphone;
    /**
     * 用户手机号码
     */
    @Excel(name = "手机号码", width = 20, orderNum = "11")
    private String user_mobilephone;
    /**
     * 用户归属公司
     */
    @Excel(name = "归属公司", width = 20, orderNum = "1")
    private String user_company;
    /***
     * 用户归属部门
     */
    @Excel(name = "归属部门", width = 20, orderNum = "2")
    private String user_department;
    /**
     * 用户职务
     */
    @Excel(name = "职务", width = 20, orderNum = "3")
    private String user_duty;
    /**
     * 用户密码
     */
    @Excel(name = "登陆密码", width = 20, orderNum = "9")
    private String user_password;
    /**
     * 密码盐
     */
    private String salt;
    /**
     * 用户电子邮箱
     */
    private String user_email;
    /**
     * 用户电子邮箱密码
     */
    private String user_email_password;
    /**
     * 用户身份证号码
     */
    @Excel(name = "身份证号码", width = 20, orderNum = "7")
    private String user_idcard;
    /**
     * 用户QQ号码
     */
    private String user_qq;
    /**
     * 用户头像
     */
    private String user_avatar;
    /**
     * 是否允许登录  1:允许登录  0:禁止登录
     */
    @Excel(name = "是否允许登陆", width = 20, orderNum = "12", replace = {"允许_1", "禁止_0"})
    private Integer is_login;
    /**
     * 记录创建时间
     */
    private Date gmt_create;
    /**
     * 记录最近修改时间
     */
    private Date gmt_modified;

    @ManyMany(relation = "sys_role_user",from = "user_account",  to = "pk_role_id")
    private List<SysRole> roles;

    public int getPk_user_id() {
        return pk_user_id;
    }

    public void setPk_user_id(int pk_user_id) {
        this.pk_user_id = pk_user_id;
    }

    public String getUser_employee_id() {
        return user_employee_id;
    }

    public void setUser_employee_id(String user_employee_id) {
        this.user_employee_id = user_employee_id;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getUser_account() {
        return user_account;
    }

    public void setUser_account(String user_account) {
        this.user_account = user_account;
    }

    public Integer getUser_sex() {
        return user_sex;
    }

    public void setUser_sex(Integer user_sex) {
        this.user_sex = user_sex;
    }

    public String getUser_telphone() {
        return user_telphone;
    }

    public void setUser_telphone(String user_telphone) {
        this.user_telphone = user_telphone;
    }

    public String getUser_mobilephone() {
        return user_mobilephone;
    }

    public void setUser_mobilephone(String user_mobilephone) {
        this.user_mobilephone = user_mobilephone;
    }

    public String getUser_company() {
        return user_company;
    }

    public void setUser_company(String user_company) {
        this.user_company = user_company;
    }

    public String getUser_department() {
        return user_department;
    }

    public void setUser_department(String user_department) {
        this.user_department = user_department;
    }

    public String getUser_duty() {
        return user_duty;
    }

    public void setUser_duty(String user_duty) {
        this.user_duty = user_duty;
    }

    public String getUser_password() {
        return user_password;
    }

    public void setUser_password(String user_password) {
        this.user_password = user_password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public String getUser_email() {
        return user_email;
    }

    public void setUser_email(String user_email) {
        this.user_email = user_email;
    }

    public String getUser_email_password() {
        return user_email_password;
    }

    public void setUser_email_password(String user_email_password) {
        this.user_email_password = user_email_password;
    }

    public String getUser_idcard() {
        return user_idcard;
    }

    public void setUser_idcard(String user_idcard) {
        this.user_idcard = user_idcard;
    }

    public String getUser_qq() {
        return user_qq;
    }

    public void setUser_qq(String user_qq) {
        this.user_qq = user_qq;
    }

    public String getUser_avatar() {
        return user_avatar;
    }

    public void setUser_avatar(String user_avatar) {
        this.user_avatar = user_avatar;
    }

    public Integer getIs_login() {
        return is_login;
    }

    public void setIs_login(Integer is_login) {
        this.is_login = is_login;
    }

    public Date getGmt_create() {
        return gmt_create;
    }

    public void setGmt_create(Date gmt_create) {
        this.gmt_create = gmt_create;
    }

    public Date getGmt_modified() {
        return gmt_modified;
    }

    public void setGmt_modified(Date gmt_modified) {
        this.gmt_modified = gmt_modified;
    }

    public List<SysRole> getRoles() {
        return roles;
    }

    public void setRoles(List<SysRole> roles) {
        this.roles = roles;
    }

role类:

@Table("sys_role")
@ExcelTarget("sys_role")
public class SysRole implements Serializable {
    /**
     * 角色信息主键id
     */
    @Id
    private int pk_role_id;
    /**
     * 角色名称
     */
    @Name
    @Excel(name = "名称", width = 20, orderNum = "1")
    private String role_name;
    /**
     * 角色所属部门编号
     */
    @Excel(name = "所属部门", width = 20, orderNum = "2")
    private Integer pk_dep_id;
    /**
     * 是否有效 1:有效 0:无效
     */
    @Excel(name = "是否有效", width = 20, orderNum = "3", replace = {"已启用_1", "已禁用_0"})
    private Integer is_valid;
    /**
     * 角色备注描述
     */
    @Excel(name = "备注描述", width = 20, orderNum = "4")
    private String role_remraks;
    /**
     * 记录创建时间
     */
    @Excel(name = "创建时间", orderNum = "5", width = 20, format = "yyyy-MM-dd")
    private Date gmt_create;

    /**
     * 记录最近修改时间
     */
    @Excel(name = "修改时间", orderNum = "6", width = 20, format = "yyyy-MM-dd")
    private Date gmt_modified;

    @ManyMany(from = "pk_role_id", relation = "sys_role_user", to = "user_account")
    private List<SysUser> users;

    @ManyMany(from = "pk_role_id", relation = "sys_role_menu", to = "pk_menu_id")
    protected List<SysMenu> menus;

    public int getPk_role_id() {
        return pk_role_id;
    }

    public void setPk_role_id(int pk_role_id) {
        this.pk_role_id = pk_role_id;
    }

    public String getRole_name() {
        return role_name;
    }

    public void setRole_name(String role_name) {
        this.role_name = role_name;
    }

    public Integer getPk_dep_id() {
        return pk_dep_id;
    }

    public void setPk_dep_id(Integer pk_dep_id) {
        this.pk_dep_id = pk_dep_id;
    }

    public Integer getIs_valid() {
        return is_valid;
    }

    public void setIs_valid(Integer is_valid) {
        this.is_valid = is_valid;
    }

    public String getRole_remraks() {
        return role_remraks;
    }

    public void setRole_remraks(String role_remraks) {
        this.role_remraks = role_remraks;
    }

    public Date getGmt_create() {
        return gmt_create;
    }

    public void setGmt_create(Date gmt_create) {
        this.gmt_create = gmt_create;
    }

    public Date getGmt_modified() {
        return gmt_modified;
    }

    public void setGmt_modified(Date gmt_modified) {
        this.gmt_modified = gmt_modified;
    }

    public List<SysUser> getUsers() {
        return users;
    }

    public void setUsers(List<SysUser> users) {
        this.users = users;
    }

    public List<SysMenu> getMenus() {
        return menus;
    }

    public void setMenus(List<SysMenu> menus) {
        this.menus = menus;
    }
    @ManyMany(relation = "sys_role_user",from = "user_account",  to = "pk_role_id")
    private List<SysRole> roles;

刚改过?? 没有冒号呢...

最开始的user类:

@Table("sys_user")
public class SysUser implements Serializable {
    /**
     * 用户信息主键id
     */
    @Id
    private int pk_user_id;
    /**
     * 工号
     */
    @Excel(name = "工号", width = 20, orderNum = "4")
    private String user_employee_id;
    /**
     * 用户姓名
     */
    @Name
    @Excel(name = "用户姓名", width = 20, orderNum = "5")
    private String user_name;
    /**
     * 用户登录账号
     */
    
    @Excel(name = "登陆帐号", width = 20, orderNum = "8")
    private String user_account;
    /**
     * 用户性别 1:男 0:女
     */
    @Excel(name = "性别", width = 20, orderNum = "6", replace = {"男_1", "女_0"})
    private Integer user_sex;
    /**
     * 用户联系电话
     */
    @Excel(name = "联系电话", width = 20, orderNum = "10")
    private String user_telphone;
    /**
     * 用户手机号码
     */
    @Excel(name = "手机号码", width = 20, orderNum = "11")
    private String user_mobilephone;
    /**
     * 用户归属公司
     */
    @Excel(name = "归属公司", width = 20, orderNum = "1")
    private String user_company;
    /***
     * 用户归属部门
     */
    @Excel(name = "归属部门", width = 20, orderNum = "2")
    private String user_department;
    /**
     * 用户职务
     */
    @Excel(name = "职务", width = 20, orderNum = "3")
    private String user_duty;
    /**
     * 用户密码
     */
    @Excel(name = "登陆密码", width = 20, orderNum = "9")
    private String user_password;
    /**
     * 密码盐
     */
    private String salt;
    /**
     * 用户电子邮箱
     */
    private String user_email;
    /**
     * 用户电子邮箱密码
     */
    private String user_email_password;
    /**
     * 用户身份证号码
     */
    @Excel(name = "身份证号码", width = 20, orderNum = "7")
    private String user_idcard;
    /**
     * 用户QQ号码
     */
    private String user_qq;
    /**
     * 用户头像
     */
    private String user_avatar;
    /**
     * 是否允许登录  1:允许登录  0:禁止登录
     */
    @Excel(name = "是否允许登陆", width = 20, orderNum = "12", replace = {"允许_1", "禁止_0"})
    private Integer is_login;
    /**
     * 记录创建时间
     */
    private Date gmt_create;
    /**
     * 记录最近修改时间
     */
    private Date gmt_modified;

    @ManyMany(relation = "sys_role_user",from = "user_account:user_account",  to = "pk_role_id")
    private List<SysRole> roles;

    public int getPk_user_id() {
        return pk_user_id;
    }

    public void setPk_user_id(int pk_user_id) {
        this.pk_user_id = pk_user_id;
    }

    public String getUser_employee_id() {
        return user_employee_id;
    }

    public void setUser_employee_id(String user_employee_id) {
        this.user_employee_id = user_employee_id;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getUser_account() {
        return user_account;
    }

    public void setUser_account(String user_account) {
        this.user_account = user_account;
    }

    public Integer getUser_sex() {
        return user_sex;
    }

    public void setUser_sex(Integer user_sex) {
        this.user_sex = user_sex;
    }

    public String getUser_telphone() {
        return user_telphone;
    }

    public void setUser_telphone(String user_telphone) {
        this.user_telphone = user_telphone;
    }

    public String getUser_mobilephone() {
        return user_mobilephone;
    }

    public void setUser_mobilephone(String user_mobilephone) {
        this.user_mobilephone = user_mobilephone;
    }

    public String getUser_company() {
        return user_company;
    }

    public void setUser_company(String user_company) {
        this.user_company = user_company;
    }

    public String getUser_department() {
        return user_department;
    }

    public void setUser_department(String user_department) {
        this.user_department = user_department;
    }

    public String getUser_duty() {
        return user_duty;
    }

    public void setUser_duty(String user_duty) {
        this.user_duty = user_duty;
    }

    public String getUser_password() {
        return user_password;
    }

    public void setUser_password(String user_password) {
        this.user_password = user_password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public String getUser_email() {
        return user_email;
    }

    public void setUser_email(String user_email) {
        this.user_email = user_email;
    }

    public String getUser_email_password() {
        return user_email_password;
    }

    public void setUser_email_password(String user_email_password) {
        this.user_email_password = user_email_password;
    }

    public String getUser_idcard() {
        return user_idcard;
    }

    public void setUser_idcard(String user_idcard) {
        this.user_idcard = user_idcard;
    }

    public String getUser_qq() {
        return user_qq;
    }

    public void setUser_qq(String user_qq) {
        this.user_qq = user_qq;
    }

    public String getUser_avatar() {
        return user_avatar;
    }

    public void setUser_avatar(String user_avatar) {
        this.user_avatar = user_avatar;
    }

    public Integer getIs_login() {
        return is_login;
    }

    public void setIs_login(Integer is_login) {
        this.is_login = is_login;
    }

    public Date getGmt_create() {
        return gmt_create;
    }

    public void setGmt_create(Date gmt_create) {
        this.gmt_create = gmt_create;
    }

    public Date getGmt_modified() {
        return gmt_modified;
    }

    public void setGmt_modified(Date gmt_modified) {
        this.gmt_modified = gmt_modified;
    }

    public List<SysRole> getRoles() {
        return roles;
    }

    public void setRoles(List<SysRole> roles) {
        this.roles = roles;
    }
}

java代码块:
这个代码是在controller层

log.info(user.getRoles());
log.info(user.getUser_account());
dao.fetchLinks(user, null);
log.info(user.getRoles());

对应的日志:

[INFO ] 2017-10-10 08:44:49,843 com.kmia.mdkj.contract.action.sys.loginAction 
[INFO ] 2017-10-10 08:44:49,843 com.kmia.mdkj.contract.action.sys.loginAction admin
[DEBUG] 2017-10-10 08:44:49,847 org.nutz.dao.impl.sql.run.NutDaoExecutor SELECT * FROM sys_role  WHERE role_name IN (SELECT pk_role_id FROM sys_role_user WHERE user_account='admin')
[INFO ] 2017-10-10 08:44:49,883 com.kmia.mdkj.contract.action.sys.loginAction []

在这个日志中 WHERE role_name IN 这个 role_name 并不是想要的
我想要的执行语句为下面这个:

SELECT * FROM sys_role  WHERE pk_role_id IN (SELECT pk_role_id FROM sys_role_user WHERE user_account='admin')

当我把user类的多对多改成:

@ManyMany(relation = "sys_role_user",from = "user_account:user_account",  to = "pk_role_id:pk_role_id")
    private List<SysRole> roles;

日志为:

[INFO ] 2017-10-10 08:56:36,883 com.kmia.mdkj.contract.action.sys.loginAction 
[INFO ] 2017-10-10 08:56:36,884 com.kmia.mdkj.contract.action.sys.loginAction admin
[DEBUG] 2017-10-10 08:56:36,890 org.nutz.dao.impl.sql.run.NutDaoExecutor SELECT * FROM sys_role  WHERE pk_role_id IN (SELECT pk_role_id FROM sys_role_user WHERE user_account=45)
[INFO ] 2017-10-10 08:56:36,941 com.kmia.mdkj.contract.action.sys.loginAction []

发现如果条件是 int类型 时,对应WHERE pk_role_id IN ,
条件是 String类型时,对应WHERE role_name IN

nutz版本是多少? 你debug一下ManyManyLinkField 吧

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