【发布时间】:2017-01-07 07:53:12
【问题描述】:
我是使用 JOOQ 的新手,我遇到了问题,但找不到解决方案。我有 2 个表的简单数据库:Sellers 和 Clients - sql 下面:
CREATE TABLE Sellers
(
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
);
CREATE TABLE Clients
(
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
seller_id int,
FOREIGN KEY (seller_id) REFERENCES Sellers(id)
);
Client 具有foreign key,它定义了分配给他的Seller。
我想使用 JOOQ 从数据库中获取客户端,但使用 join() 也可以为每个客户端获取 Seller 对象。可能吗?如果是这样怎么做?这是我的 POJO 对象:
public class Seller {
private final SimpleIntegerProperty id = new SimpleIntegerProperty();
private final SimpleStringProperty name = new SimpleStringProperty();
...
//setters and getters here
...
}
public class Client {
private final SimpleIntegerProperty id = new SimpleIntegerProperty();
private final SimpleStringProperty name = new SimpleStringProperty();
private final SimpleIntegerProperty sellerId = new SimpleIntegerProperty();
//private Seller seller; //not working
...
//setters and getters here
...
}
这是我获取客户的 JOOQ 代码:
context.select()
.from(CLIENTS)
.join(SELLERS)
.on(CLIENTS.ID.eq(SELLERS.ID))
.fetchInto(Client.class);
我应该改变什么才能得到我想要的?
【问题讨论】:
-
Client将seller作为对象。 DB 有seller_id作为int。 JOOQ 会自动解决这个问题吗? -
@bradimus 它会自动将 Seller_id 解析为 int,但正如您在我的 Client 类中看到的那样,我也尝试添加对象 Seller Seller,但没有解决。