[英]ERROR: column "DOJ" is of type timestamp without time zone but expression is of type character varying
[英]When I insert a date in Jooq, I get this error: column creation_date is of type timestamp with time zone but expression is of type character varying
我正在使用 Spring 引導和 postgresql,這是我的“pom.xml”文件:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jooq</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<executions>
<execution>
<id>generate-postgre</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<jdbc>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://localhost:5432/DEMO</url>
<user>postgres</user>
<password>password</password>
</jdbc>
<generator>
<database>
<name>org.jooq.meta.postgres.PostgresDatabase</name>
<includes>.*</includes>
<excludes></excludes>
<schemata>
<schema> <inputSchema>comun</inputSchema> </schema>
</schemata>
</database>
<generate>
<pojos>true</pojos>
<pojosEqualsAndHashCode>true</pojosEqualsAndHashCode>
<javaTimeTypes>true</javaTimeTypes>
<fluentSetters>true</fluentSetters>
</generate>
<target>
<packageName>com.firedragon.erp.comun.entities</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
我想像這樣插入一個用戶 object:
User:
userId --> Long
name --> String
password --> String
creationDate --> ZonedDateTime
modifitionDate --> ZonedDateTime
在數據庫中:
Users:
user_id --> bigint
name --> character varying
password --> character varying
creation_date --> timestamp with time zone
modifition_date --> timestamp with time zone
但是當我嘗試插入用戶時:
@Autowired
DSLContext ctx;
...
Users users=Users.USERS;
ctx.insertInto(users, Arrays.asList(users.NAME, users.PASSWORD, users.CREATION_DATE, users.MODIFICATION_DATE))
.values(Arrays.asList("user1","password",ZonedDateTime.now(),ZonedDateTime.now()))
.execute();
我收到此錯誤:
column 'creation_date' is of type timestamp with time zone but expression is of type character varying
我意識到 jooq 創建的用戶 class 的“creation_date”字段為“TableField<UsersRecord, OffsetDateTime>”類型而不是“TableField<UsersRecord, ZonedDateTime>”
public final TableField<UsersRecord, OffsetDateTime> CREATION_DATE= createField(DSL.name("creation_date"), SQLDataType.TIMESTAMPWITHTIMEZONE(6).nullable(false), this, "");
我嘗試將 creation_date 字段從 ZonedDateTime 轉換為 OffsetDateTime,然后將其放入“值”方法中,但它也不起作用,我得到了同樣的錯誤
我應該怎么做才能正確注冊用戶?
我認為您需要將此參數添加到連接字符串:stringtype=unspecified
例如:jdbc:postgresql://127.0.0.1:5432/testdb?stringtype=unspecified
我正在使用 Spring 引導和 postgresql,這是我的“pom.xml”文件:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jooq</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<executions>
<execution>
<id>generate-postgre</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<jdbc>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://localhost:5432/DEMO</url>
<user>postgres</user>
<password>password</password>
</jdbc>
<generator>
<database>
<name>org.jooq.meta.postgres.PostgresDatabase</name>
<includes>.*</includes>
<excludes></excludes>
<schemata>
<schema> <inputSchema>comun</inputSchema> </schema>
</schemata>
</database>
<generate>
<pojos>true</pojos>
<pojosEqualsAndHashCode>true</pojosEqualsAndHashCode>
<javaTimeTypes>true</javaTimeTypes>
<fluentSetters>true</fluentSetters>
</generate>
<target>
<packageName>com.firedragon.erp.comun.entities</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
我想像這樣插入一個用戶 object :
User:
userId --> Long
name --> String
password --> String
creationDate --> ZonedDateTime
modifitionDate --> ZonedDateTime
在數據庫中:
Users:
user_id --> bigint
name --> character varying
password --> character varying
creation_date --> timestamp with time zone
modifition_date --> timestamp with time zone
但是當我嘗試插入用戶時:
@Autowired
DSLContext ctx;
...
Users users=Users.USERS;
ctx.insertInto(users, Arrays.asList(users.NAME, users.PASSWORD, users.CREATION_DATE, users.MODIFICATION_DATE))
.values(Arrays.asList("user1","password",ZonedDateTime.now(),ZonedDateTime.now()))
.execute();
我收到此錯誤:
column 'creation_date' is of type timestamp with time zone but expression is of type character varying
我意識到 jooq 創建的用戶 class 的“creation_date”字段為“TableField<UsersRecord, OffsetDateTime>”類型而不是“TableField<UsersRecord, ZonedDateTime>”
public final TableField<UsersRecord, OffsetDateTime> CREATION_DATE= createField(DSL.name("creation_date"), SQLDataType.TIMESTAMPWITHTIMEZONE(6).nullable(false), this, "");
我嘗試將 creation_date 字段從 ZonedDateTime 轉換為 OffsetDateTime 之前將其放入“值”方法但它也不起作用,我得到同樣的錯誤
我應該怎么做才能正確注冊用戶?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.