我正在使用JPA更新一個表。Postgres是資料庫。有一個欄位的型別是String[]。當通過API更新時,其他欄位被更新。但這個String[]欄位被更新為空。我已經通過獲取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"/span>, (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()) 。
}
}
uj5u.com熱心網友回復:
你想映射String[],但是你的CustomStringArrayType映射了Integer[]。看看你的代碼:
@Override。
public Class returnedClass() {
return Integer[].class;
}
并且:
Array array = session. connection().createArrayOf("int", (Integer[])value)。
這應該被改為:
@Override
public Class returnedClass(>/span>) {
return String[] .class;
}
并且:
Array array = session. connection().createArrayOf("text", (String[])value)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/317503.html
標籤:
上一篇:我怎樣才能在Windows10上使用真正的iPhoneiOS設備測驗我在Xamarin.Forms上編碼的ios應用程式?
下一篇:如何映射非相關的JPA物體
