【问题标题】:JAVA - How to pass array into @NamedNativeQueryJAVA - 如何将数组传递给@NamedNativeQuery
【发布时间】:2016-04-30 20:19:27
【问题描述】:

你知道如何将这个属性(日历数组)传递给@NamedNativeQuery 吗?

'["1996-01-01 12:00:00","1996-01-01 17:00:00"]'

我尝试了以下方法但不起作用。

@NamedNativeQuery(
      name = "BusinessHours.deleteBusinessHours",
      query = "delete from business_hours " +
              "where company_address_id = ?1 and " +
              "hours = [?2,?3]"
)

编辑: postgres 中的原生查询是:

DELETE FROM business_hours 
WHERE company_address_id = 7 
AND hours = '["1996-01-01 18:15:00","1996-01-01 18:30:00"]'

【问题讨论】:

  • 你试过了吗? ...和 ​​?2 中的小时数并传递数组而不是 2 个参数
  • 看不懂你能不能写的详细一点。
  • query = "delete from business_hours where company_address_id = ?1 and hours in ?2" As ?2 你传递日期数组
  • 不,它不再起作用了..
  • 尝试查询 =“从 business_hours 中删除 company_address_id = ?1 和 hours = ARRAY[?2, ?3]”

标签: java sql jpa


【解决方案1】:

据我了解,business_hours.hours 在 DB 中是作为字符输入的。因此,您需要将数组序列化为与数据库预期格式匹配的字符串,然后通过单个占位符传递该字符串:

query = "delete from business_hours where company_address_id = ?1 and hours = ?2"

【讨论】:

    猜你喜欢
    • 2014-09-21
    • 1970-01-01
    • 2014-10-20
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    相关资源
    最近更新 更多