简体   繁体   中英

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

I'm using Spring boot and postgresql, this is my "pom.xml" file:

<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>

I want to insert a User object like this:

User:
  userId --> Long
  name --> String
  password --> String
  creationDate --> ZonedDateTime
  modifitionDate --> ZonedDateTime

In database:

Users:
  user_id --> bigint
  name --> character varying
  password --> character varying
  creation_date --> timestamp with time zone
  modifition_date --> timestamp with time zone

but when I try to insert the user:

@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();

I get this error:

column 'creation_date' is of type timestamp with time zone but expression is of type character varying

I realize that the Users class that jooq created has the "creation_date" field as "TableField<UsersRecord, OffsetDateTime>" type instead of "TableField<UsersRecord, ZonedDateTime>"

public final TableField<UsersRecord, OffsetDateTime> CREATION_DATE= createField(DSL.name("creation_date"), SQLDataType.TIMESTAMPWITHTIMEZONE(6).nullable(false), this, "");

I tried to convert the creation_date field from ZonedDateTime to OffsetDateTime before to putting it in the "values" method but it doesn't work either, I get the same error

What should I do to register the user correctly?

I think you need to add this parameter to the connection string: stringtype=unspecified

Eg: jdbc:postgresql://127.0.0.1:5432/testdb?stringtype=unspecified

I'm using Spring boot and postgresql, this is my "pom.xml" file:

<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>

I want to insert a User object like this:

User:
  userId --> Long
  name --> String
  password --> String
  creationDate --> ZonedDateTime
  modifitionDate --> ZonedDateTime

In database:

Users:
  user_id --> bigint
  name --> character varying
  password --> character varying
  creation_date --> timestamp with time zone
  modifition_date --> timestamp with time zone

but when I try to insert the user:

@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();

I get this error:

column 'creation_date' is of type timestamp with time zone but expression is of type character varying

I realize that the Users class that jooq created has the "creation_date" field as "TableField<UsersRecord, OffsetDateTime>" type instead of "TableField<UsersRecord, ZonedDateTime>"

public final TableField<UsersRecord, OffsetDateTime> CREATION_DATE= createField(DSL.name("creation_date"), SQLDataType.TIMESTAMPWITHTIMEZONE(6).nullable(false), this, "");

I tried to convert the creation_date field from ZonedDateTime to OffsetDateTime before to putting it in the "values" method but it doesn't work either, I get the same error

What should I do to register the user correctly?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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