问题
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.
First of all, is it possible to configure the QueryDSL plugin to automatically generate Java enums from the PostgreSQL database enumtypes? For example:
When we have an enum:
CREATE TYPE customertype AS ENUM ('person','company');
Which is used as a column type:
CREATE TABLE customer (customertype customertype,...
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?
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>
and use it in the type mapping
<typeMapping>
<table>customer</table>
<column>customertype</column>
<type>com.example.StringToCustomerType</type>
</typeMapping>
Or is there a better way achieving the wanted situation?
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!
回答1:
Your StringToCustomerType
approach looks reasonable. It should provide the enum mapping as expected.
来源:https://stackoverflow.com/questions/29164480/querydsl-postgresql-enums