[英]JOOQ get POJO object with another POJO - foreign key in table
I'm new at using JOOQ and I have problem but I can't find solution. 我是使用JOOQ的新手,但有问题,但找不到解决方案。 I have simple database with 2 tables:
Sellers
and Clients
- sql below: 我有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
has foreign key
and it defines which Seller
is assigned to him. Client
具有foreign key
,它定义了分配给他的Seller
。
I would like to get clients from database using JOOQ but using join()
also get Seller
object to each of client. 我想使用JOOQ从数据库中获取客户端,但使用
join()
还会为每个客户端获取Seller
对象。 Is it possible? 可能吗? If so how to do that?
如果是这样,该怎么做? Here's my POJO objects:
这是我的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
...
}
And here's my JOOQ code to get clients: 这是我获取客户的JOOQ代码:
context.select()
.from(CLIENTS)
.join(SELLERS)
.on(CLIENTS.ID.eq(SELLERS.ID))
.fetchInto(Client.class);
What should I change to get what I want? 我应该改变什么才能得到我想要的东西?
check out the conversation here between Lukas Eder (the author of jOOQ) and some other jOOQ users. 在此处查看 Lukas Eder(jOOQ的作者)与其他jOOQ用户之间的对话。 Garrett Wilson's use case looks very similar to yours (where you have
Client
: Seller
, he has Book
: Author
). Garrett Wilson的用例看起来与您的用例非常相似(您的
Client
: Seller
,他的Book
: Author
)。
there's quite a bit said, but by design it seems jOOQ isn't geared toward automatically hydrating a Seller
instance within your fetched Client
record. 有人说了很多,但是从设计上看,jOOQ似乎不适合自动为您获取的
Client
记录中的Seller
实例添加水分。 this is the classic N+1 problem associated with ORMs (ie multiple queries against your table of sellers triggered by a query for clients). 这是与ORM相关的经典N + 1问题(即,由对客户的查询触发的针对您的卖方表的多个查询)。
one proposal is to to break your join up into discrete queries: 一个建议是将您的联接分解为离散查询:
select * from Client where ...
select * from Seller where id in (select seller_id from Client where ...)
... and then do your client.setSeller()
type logic somewhere in your application. ...然后在应用程序中的某处执行
client.setSeller()
类型的逻辑。 in this case you avoid the N+1 problem, and you don't need to rely on writing custom any RecordMapper
types. 在这种情况下,可以避免N + 1问题,并且不需要依赖于自定义编写任何
RecordMapper
类型。
i'm no jOOQ expert, but the approach i used in the past when i had columns from hierarchical types being returned in one query worked out pretty well for me: 我不是jOOQ专家,但是我过去使用的方法是在一个查询中返回层次结构类型的列时,对我来说效果很好:
Record
to your domain type/POJO Record
映射到您的域类型/ POJO RecordMapperProvider
and use it to create DSLContext
singleton that gets passed around to whatever components do your querying (eg through dependency injection) RecordMapperProvider
并使用它创建DSLContext
单例,该单例传递给您进行查询的任何组件(例如,通过依赖注入) DSLContext
, compose your query and use fetchInto()
to designate your target type (which should have been accounted for in your RecordMapperProvider
) DSLContext
,组成查询并使用fetchInto()
来指定目标类型(应该在RecordMapperProvider
) in the conversation referenced earlier, Lukas drops a link to the documentation about using custom RecordMapperProvider
instances, so this might very well be idiomatic. 在前面提到的对话中,Lukas删除了有关使用自定义
RecordMapperProvider
实例的文档的链接 ,因此这很可能是惯用的。
hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.