将图像插入BYTES列-类型“二进制”不存在

时间:2019-05-09 13:28:04

标签: java sql jooq

我正在尝试使用jOOQ将Spring的MultipartFile字段中的图像插入到Postgres的BYTEA列中,但我不断收到令人困惑的错误消息。这是什么意思,应该如何插入?

CREATE TABLE image
(
    id      SERIAL    PRIMARY KEY,
    data    BYTEA     NOT NULL
)
import org.springframework.web.multipart.MultipartFile;

public class AddIMageForm {
    private MultipartFile image;
}
import javax.persistence.Column;

public class Image {

    @Column(name = "id")
    private Integer id;

    @Column(name = "data")
    private byte[] data;
}
import static com.test.Tables.IMAGE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.jooq.DSLContext;

@Controller
public class AddImageController {

  @Autowired
  DSLContext jooq;

  @RequestMapping(value = "/addImage", method = RequestMethod.POST)
  public ModelAndView addImagePost(Model model, 
              @ModelAttribute("addImageForm") AddImageForm addImageForm) {

    byte[] imageBytes = addImageForm.getImage().getBytes();
    jooq.insertInto(IMAGE).columns(IMAGE.DATA).values(imageBytes).execute();
    return new ModelAndView("/viewImage");
  }
}

执行时会给出令人困惑的错误消息:

org.jooq.exception.DataAccessException: 
SQL [insert into "image" ("data") values (cast(? as binary))]; 
ERROR: type "binary" does not exist
  Position: 58
    at org.jooq_3.10.8.H2.debug(Unknown Source)
    at org.jooq.impl.Tools.translate(Tools.java:2241)
        ...

1 个答案:

答案 0 :(得分:3)

您的异常堆栈跟踪向您显示了原因。您已经配置了SQLDialect.H2方言,但是在PostgreSQL上运行了查询。使用SQLDialect.POSTGRES的方言。