【问题标题】:Field is updated to null while updating table更新表时字段更新为空
【发布时间】:2021-11-11 09:05:51
【问题描述】:

我正在使用 JPA 更新表。 Postgres 是数据库。有一个类型为 String[] 的字段。通过 API 更新时,其他字段正在更新。但是这个 String[] 字段正在更新为 null 。我已经通过参考 https://www.baeldung.com/java-hibernate-map-postgresql-array 来实现。任何帮助表示赞赏

CustomMapper.java

public class CustomStringArrayType implements UserType {

        @Override
        public int[] sqlTypes() {
            return new int[]{Types.ARRAY};
        }

        @Override
        public Class returnedClass() {
            return Integer[].class;
        }

    @Override
    public boolean equals(Object o, Object o1) throws HibernateException {
        return false;
    }

    @Override
    public int hashCode(Object o) throws HibernateException {
        return 0;
    }

    @Override
        public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
                throws HibernateException, SQLException {
            Array array = rs.getArray(names[0]);
            return array != null ? array.getArray() : null;
        }

        @Override
        public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session)
                throws HibernateException, SQLException {
            if (value != null && st != null) {
                Array array = session.connection().createArrayOf("int", (Integer[])value);
                st.setArray(index, array);
            } else {
                st.setNull(index, sqlTypes()[0]);
            }
        }

    @Override
    public Object deepCopy(Object o) throws HibernateException {
        return null;
    }

    @Override
    public boolean isMutable() {
        return false;
    }

    @Override
    public Serializable disassemble(Object o) throws HibernateException {
        return null;
    }

    @Override
    public Object assemble(Serializable serializable, Object o) throws HibernateException {
        return null;
    }

    @Override
    public Object replace(Object o, Object o1, Object o2) throws HibernateException {
        return null;
    }

更新部分:

@Override
  public CompletableFuture<Void> updateContactGroup_1_0(
      ContactGroupDTO contactGroupDTO, String groupId, String fleetId) {
    var contactGroupEntity =
        contactGroupRepository.findContactGroupByGroupAndFleetId(Long.parseLong(groupId), fleetId);
    if (contactGroupEntity == null) {
      throw new BadRequestException("Group doesn't exist !!");
    } else {
      return CompletableFuture.supplyAsync(
          () -> {
            contactGroupRepository.save(
                ContactGroupMapper.updateInDB(
                    contactGroupDTO, groupId, fleetId, contactGroupEntity.getCreatedTime()));
            return null;
          },
          RepositoryUtil.executor());
    }
  }

【问题讨论】:

标签: java spring postgresql jpa


【解决方案1】:

您想要映射 String[],但您的 CustomStringArrayType 映射 Integer[]。看看你的代码:

@Override
public Class returnedClass() {
    return Integer[].class;
}

和:

Array array = session.connection().createArrayOf("int", (Integer[])value);

这应该改为:

@Override
public Class returnedClass() {
    return String[].class;
}

和:

Array array = session.connection().createArrayOf("text", (String[])value);

【讨论】:

  • 成功了!谢谢。
猜你喜欢
  • 2010-12-24
  • 1970-01-01
  • 1970-01-01
  • 2019-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-02
  • 2017-11-05
相关资源
最近更新 更多