简体   繁体   English

JOOQ用另一个POJO获取POJO对象-表中的外键

[英]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个表的简单数据库: SellersClients -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的用例看起来与您的用例非常相似(您的ClientSeller ,他的BookAuthor )。

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专家,但是我过去使用的方法是在一个查询中返回层次结构类型的列时,对我来说效果很好:

  1. define a custom record mapper to map your jOOQ Record to your domain type/POJO 定义一个自定义记录映射器,以将您的jOOQ Record映射到您的域类型/ POJO
  2. define a custom RecordMapperProvider and use it to create DSLContext singleton that gets passed around to whatever components do your querying (eg through dependency injection) 定义一个自定义RecordMapperProvider并使用它创建DSLContext单例,该单例传递给您进行查询的任何组件(例如,通过依赖注入)
  3. using the aforementioned 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM