我有一個簡單的 LocalDateTime - Timestamp - Converter 在 @Entity 中使用,如下所示:
@Converter(autoApply = true)
public class TimestampConverter implements AttributeConverter<LocalDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) {
return localDateTime == null ? null : DateUtil.convertToTimestamp(localDateTime);
}
@Override
public LocalDateTime convertToEntityAttribute(Timestamp timestamp) {
return timestamp == null ? null : DateUtil.convertToLocalDateTime(timestamp);
}
}
這在 LocalDateTime 欄位上的物體中使用,如下所示:
@Entity
@Table(name = "LOG")
@Data
public class Log implements Serializable {
@Id
private long id;
private String content;
@Column(name = "DATE_RECORDED")
@Convert(converter = TimestampConverter.class)
private LocalDateTime dateRecorded;
}
需要進行此轉換以避免在空(空)值上出現以下錯誤:
ORA-00932: 不一致的資料型別:預期的 TIMESTAMP 為 BINARY
假設在本地針對 oracle-xe testcontainer 執行以下測驗作業正常:
@Testcontainers
public class DbLogTest {
protected static EntityManager entityManager;
@Container
protected static OracleContainer oracle = new OracleContainer();
private static Map<String, String> getProperties() {
Map<String, String> properties = new HashMap<>();
properties.put("javax.persistence.jdbc.url", oracle.getJdbcUrl());
properties.put("javax.persistence.jdbc.user", oracle.getUsername());
properties.put("javax.persistence.jdbc.password", oracle.getPassword());
return properties;
}
@BeforeAll
static void beforeAll() {
entityManager = Persistence.createEntityManagerFactory("persistenceUnitName", getProperties()).createEntityManager();
}
@Test
public void insertLog() throws Exception {
Log log = new Log();
entityManager.persist(new Log());
entityManager.flush();
assertTrue(log.getId() > 0);
}
}
mvn test由于上述錯誤,使用 maven build ( )執行相同的測驗失敗。除錯顯示在 maven 測驗中執行時根本沒有呼叫轉換器。
誰能指出我正確的方向理解并最終解決這個問題?與 maven 相比,在 IDE 中執行測驗有什么區別?
作為解決方法,我可以避免使用 LocalDateTime,但無論如何我想了解這個問題。
uj5u.com熱心網友回復:
該問題是由 MultiModuleBuild 引起的。Entity 和 Converter 與DbLogTest. 由于尚未進行整個構建,因此 DbLogTest 的 Maven 測驗運行導致了觀察到的行為。表演clean install解決了問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/369095.html
