表包含:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
public class SourceContentMappingPK implements Serializable {
@Column(name = "hr_code")
private String hrCode;
@Column(name = "muse_id")
private String museId;
@Column(name = "source_type")
private String sourceType;
@Column(name = "cts")
private LocalDateTime cts;
}
这是模型:
@Getter
@Setter
@NoArgsConstructor
public class SourceContentMappingDTO {
private String hrCode;
private String museId;
private String sourceType;
private String masterHotelId;
private LocalDateTime cts;
public SourceContentMappingDTO(String hrCode, String museId, String sourceType, String masterHotelId, LocalDateTime cts) {
this.hrCode = hrCode;
this.museId = museId;
this.sourceType = sourceType;
this.masterHotelId = masterHotelId;
this.cts = cts;
}
}
当我通过邮差发送请求时,当前日期和时间被写入数据库。当我通过页面发出相同的请求时,除了日期之外的所有内容都被写入数据库。必须输入日期,而不是从前端发送。
@Override
@Transactional
public void updateImageSources(HotelMasterListSubItemDTO dto) {
dto.setContentSources(Arrays.asList("IMAGES"));
List<SourceContentMapping> sourceContentMappings = entityManager.createNativeQuery("select * from SOURCE_CONTENT_MAPPING where master_hotel_id = :masterHotelId", SourceContentMapping.class)
.setParameter("masterHotelId", dto.getMasterHotelId())
.setHint(QueryHints.READ_ONLY, true)
.getResultList();
for(String contentSourceType : dto.getContentSources()) {
boolean contentSourceTypeExists = false;
for(SourceContentMapping sourceContentMapping : sourceContentMappings) {
if(contentSourceType.equals(sourceContentMapping.getId().getSourceType())) {
contentSourceTypeExists = true;
entityManager.createNativeQuery("update SOURCE_CONTENT_MAPPING set muse_id = :museId, hr_code = :hrCode, cts = :cts where master_hotel_id = :masterHotelId and source_type = :sourceType")
.setParameter("museId", dto.getMuseId())
.setParameter("hrCode", dto.getHrCode())
.setParameter("masterHotelId", dto.getMasterHotelId())
.setParameter("sourceType", contentSourceType)
.setParameter("cts", LocalDateTime.now())
.executeUpdate();
}
}
if(!contentSourceTypeExists) {
entityManager.createNativeQuery("insert into SOURCE_CONTENT_MAPPING (hr_code, muse_id, source_type, master_hotel_id, cts) values (?, ?, ?, ?, ?)")
.setParameter(1, dto.getHrCode())
.setParameter(2, dto.getMuseId())
.setParameter(3, contentSourceType)
.setParameter(4, dto.getMasterHotelId())
.setParameter(5, LocalDateTime.now())
.executeUpdate();
}
}
}
我尝试添加。setparameter ("cts", LocalDateTime)。now())和. setparameter (5, LocalDateTime。现在()),但就像我说的,它只通过邮差工作,而不是通过GUI。我需要将参数设为可选的、可空的,例如
使用MySQL。表包含:
museId (varchar 50, notNull),
hrCode (varchar 50, notNull),
source_type (varchar 50, notNull),
masterHotelId (varchar 100),
cts (timestamp)
…
这个链接是一张图片:数据库中的第一条记录是邮递员发送的,第二条记录是通过页面发送的。
要获取时间戳值并将其放入sql中,您可以简单地使用# timestamp . valueof()方法并将LocalDateTime放入方法的参数中。要获取时间,可以在后端使用= new Timestamp(System.currentTimeMillis())
数据类型错误
您错误地混合了错误的数据类型。
查看MySQL 8文档
参见Ole V.V.对问题的评论
MySQL 8中的TIMESTAMP
类型类似于SQL标准类型TIMESTAMP WITH TIME ZONE
。使用这种类型来记录时刻,时间轴上的特定点。此类型表示带有"在UTC中"看到的时间的日期,与UTC的小时-分钟-秒的偏移量为0。Java中的匹配类型是OffsetDateTime
,由JDBC规范指定。
Not a moment
MySQL 8中的DATETIME
类型类似于SQL标准类型TIMESTAMP WITHOUT TIME ZONE
。此类型表示带有时间的日期,但缺乏时区上下文或与UTC的偏移量。这个类型不能表示瞬间。匹配的Java类型为LocalDateTime
。
所以你使用Java LocalDateTime
和MySQL TIMESTAMP
是不匹配的。事实上,我无法想象调用LocalDateTime.now
是正确的事情。
您没有解释您的业务域。所以我不能告诉你哪种类型是合适的。但是我可以告诉你不要把错误的类型混在一起。
避免遗留日期时间类型
永远不要使用java.sql.Timestamp
、Calendar
或Date
类。这些在设计上有严重的缺陷。
这些类现在是遗留的,多年前被现代的java所取代。JSR 310中定义的time类。