查询DSL PostgreSQL枚举? [英] QueryDSL & PostgreSQL enums?

查看:148
本文介绍了查询DSL PostgreSQL枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将QueryDSL与PostgreSQL数据库一起使用,并首先构建我们的域数据库.我们的Java域Bean是使用QueryDSL Maven插件版本3.6.1自动生成的.

We're using QueryDSL with PostgreSQL database and building our domain database first. Our Java domain beans are automatically generated using the QueryDSL Maven Plugin Version 3.6.1.

首先,是否可以配置QueryDSL插件以从PostgreSQL数据库枚举类型自动生成Java枚举?例如:

First of all, is it possible to configure the QueryDSL plugin to automatically generate Java enums from the PostgreSQL database enumtypes? For example:

当我们有一个枚举时:

CREATE TYPE customertype AS ENUM ('person','company');

用作列类型:

CREATE TABLE customer (customertype customertype,...

我希望将Java类生成为:

I would like to have the Java class to be generated as:

public class Customer {

private CustomerType customerType;

...以及所引用的枚举为:

...and the refered enum as:

public enum CustomerType {
    PERSON,
    COMPANY,
}

从我搜索了一段时间后的经验来看,使用插件的当前功能可能无法做到这一点吗?

Judging from what I've learned after searching around for quite a while, this might not be possible with the current functionality of the plugin?

在那种情况下,显然的方法是手动定义Java枚举,然后使用插件类型映射将 customer customertype 列与其手动绑定定义.问题是应该怎么做?我想我将不得不创建一个转换器类

In that case, apparently the way to go would be to define the Java enum manually and then manually bind the customer tables customertype column to it using the plugins type mapping definition. The question is how should this be done? I would suppose I would have to create a converter class

public class StringToCustomerType extends EnumAsObjectType<CustomerType>

并在类型映射中使用它

<typeMapping>
   <table>customer</table>
   <column>customertype</column>
   <type>com.example.StringToCustomerType</type>
</typeMapping>

还是有一种更好的方法来解决所需的情况?

Or is there a better way achieving the wanted situation?

显然,解决该问题的粗略方法是在JDBC连接字符串中定义 stringtype = unspecified ,定义要映射为字符串的列,并直接使用枚举字符串值将值放置到域对象中.但这听起来并不是一个可靠的解决方案.

Obviously the crude way of getting around the problem would be to define stringtype=unspecified in the JDBC connection string, define the column to be mapped as a string and directly use the enums string values as when placing values to the domain objects. This doesn't sound like a solid solution though.

所有建议都将不胜感激!谢谢!

All suggestions and advice would be greatly appreciated! Thank you!

推荐答案

您的StringToCustomerType方法看起来很合理.它应该提供预期的枚举映射.

Your StringToCustomerType approach looks reasonable. It should provide the enum mapping as expected.

这篇关于查询DSL PostgreSQL枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆