Home > database >  Springboot error when inserting to Oracle DB using Oracle JDBC java API with DATE
Springboot error when inserting to Oracle DB using Oracle JDBC java API with DATE

Time:07-07

I got the error Caused by: Error : 932, Position : 70, Sql = insert into T_table1 (CREATED_TIME, LOG, ID) values (:1 , :2 , :3 ), OriginalSql = insert into T_table1 (CREATED_TIME, LOG, ID) values (?, ?, ?), Error Msg = ORA-00932: inconsistent datatypes: expected TIMESTAMP got BINARY.

None of the other posts solved my problem

Main:

DATE th = new DATE(); //oracle.sql.DATE
LogService.saveLoggingData(new LogEntity("test log",th));

LogEntity:

import oracle.sql.DATE;
import javax.persistence.*;

@Entity
@Table(name = "T_table1")
public class LogEntity{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "ID")
    private long id;

    @Column(name = "LOG")
    private String log;

    @Column(name = "CREATED_TIME")
    private DATE ts;

    public LogEntity(){

    }
    public LogEntity(String logEvent, DATE createdAt){
        this.ts = createdAt;
        this.logEvent = logEvent;
    }

}

LogService:

@Service
public class LogServiceImpl implements LogService {

    @Autowired
    LogRepository LogRepository;

    @Override
    public void saveLoggingData(LogEntity log) {
        LogRepository.save(log);
    }
}

LogRepository:

@Repository
public interface LogRepository extends JpaRepository<LogEntity, Long>{

}

Oracle Table field CREATED_TIME is of type TIMESTAMP (6). But I am clearly inserting a nonnull DATE in the main, why did it become binary?

CodePudding user response:

I think you have two problems

First, the LogEntity constructor is setting the value to the argument itself.

public LogEntity(String logEvent, DATE createdAt){
    this.ts = createdAt;
    this.logEvent = logEvent;
}

Its must be:

public LogEntity(String logEvent, DATE createdAt){
    this.ts = createdAt;
    this.log = logEvent;
}

On the other hand, I think that using the Oracle Date is not suitable for this insert. You should use the Date from the Java Util library

Replace oracle.sql.DATE for java.util.Date;
  • Related