【问题标题】:Why is JOOQ's fetchOneInto(Pojo.class) setting only id field, and others to null?为什么 JOOQ 的 fetchOneInto(Pojo.class) 只设置 id 字段,而其他字段为空?
【发布时间】: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);

上面的sn-p返回

但是,如果我尝试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

【问题讨论】:

    标签: java jooq


    【解决方案1】:

    原来这里的罪魁祸首是jakarta.persistence-api的使用。切换到javax.persistence-api 让一切都变得美好而美好。

    【讨论】:

    猜你喜欢
    • 2020-07-22
    • 2020-07-08
    • 1970-01-01
    • 2019-07-23
    • 2019-02-10
    • 2016-06-30
    • 2012-06-19
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多