【发布时间】:2021-11-18 11:44:32
【问题描述】:
我正在使用users 表,定义为:
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'unique user id.',
`username` varchar(50) NOT NULL COMMENT 'username(unique identifier for each user.)',
`password` text NOT NULL COMMENT 'password encrypted with salt.',
`phone` varchar(20) DEFAULT NULL COMMENT 'phone number(phone number should be unique for each user.)',
`email` varchar(70) DEFAULT NULL COMMENT 'e-mail(e-mail should be unique for each user.)',
`status` enum('ACTIVE','LOCKED','DELETED') NOT NULL COMMENT 'user account status.',
`isDeleted` tinyint(4) NOT NULL DEFAULT 0 COMMENT 'boolean flag deleting whether the user is deleted or not.',
`created_at` timestamp NOT NULL DEFAULT current_timestamp() COMMENT 'Date and time of user creation.',
`updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() COMMENT 'Date and time when the user was last updated.',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`,`phone`,`email`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
另外,一个自定义的User POJO,看起来像:
@Table(name = "users")
public class User {
@Column(name = "id")
private Integer id;
@Column(name = "username")
private String userName;
@Column(name = "phone")
private String userPhone;
@Column(name = "email")
private String userEmail;
@Column(name = "password")
private String password;
// Getters and Setters omitted for brevity
}
现在,当我只是 fetch() 记录时,它工作正常:
UsersRecord user = new DSLContextFactory("demo").getDSLContext()
.selectFrom(USERS)
.where(USERS.USERNAME.eq("james"))
.fetchOne();
System.out.println(user);
但是,如果我尝试fetchOneInto(User.class),它只会将 id 字段提交给User:
User user = new DSLContextFactory("demo").getDSLContext()
.selectFrom(USERS)
.where(USERS.USERNAME.eq("james"))
.fetchOneInto(User.class);
System.out.println(user);
现在,上面的 sn-p 返回:
[User{id=3, userName='null', userPhone='null', userEmail='null'}]
我一点也不知道这里发生了什么。一些帮助将不胜感激!
我正在使用 JOOQ 3.15.0
【问题讨论】: