知乎专栏 |
{"status":false,"code":"HttpMessageNotWritableException","data":null,"reason":"Could not write JSON: failed to lazily initialize a collection of role: cn.netkiller.domain.Lora.keyword: could not initialize proxy - no Session"}⏎
@ElementCollection(fetch = FetchType.EAGER) private Set<String> keyword = new HashSet<String>();
查询 version 表中的最有一条记录,提示 Query did not return a unique result: 2 results were returned
package cn.netkiller.domain; import jakarta.persistence.*; import lombok.Data; import org.hibernate.annotations.Comment; import org.hibernate.annotations.DynamicInsert; import org.hibernate.annotations.DynamicUpdate; import java.io.Serial; import java.io.Serializable; import java.util.Date; @Entity @Table @DynamicUpdate @DynamicInsert @Data @Comment("版本表") public class Version implements Serializable { @Serial public static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", unique = true, nullable = false, insertable = false, updatable = false, columnDefinition = "int unsigned") @Comment("主键") private Integer id; @Comment("产品") @Column(columnDefinition = "enum('Phone','Tablet','Badges','Watch') DEFAULT 'Tablet'") private String product; @Comment("版本") @Column(length = 5) private String version; @Comment("下载地址") private String url; @Column(insertable = false, updatable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP") @Comment("创建时间") private Date ctime; @Column(nullable = true, insertable = false, updatable = false, columnDefinition = "TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP") @Comment("修改时间") private Date mtime; }
接口定义 findOneBy 查询 version 表中存在多条记录,我们只要最后一条,所以不能使用 findOneBy,需要使用 findFirstBy
package cn.netkiller.repository; import cn.netkiller.domain.Version; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; import java.util.Optional; @Repository public interface VersionRepository extends CrudRepository<Version, Integer> { Iterable<Version> findByProduct(String tablet); Optional<Version> findOneByProductOrderByIdDesc(String tablet); }
package cn.netkiller.repository; import cn.netkiller.domain.Version; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; import java.util.Optional; @Repository public interface VersionRepository extends CrudRepository<Version, Integer> { Iterable<Version> findByProduct(String tablet); Optional<Version> findFirstByProductOrderByIdDesc(String tablet); }
@GetMapping("/version/tablet/latest") public AigcResponse tabletLatest() { Optional<Version> optional = versionRepository.findFirstByProductOrderByIdDesc("Tablet"); if (optional.isPresent()) { Version version = optional.get(); log.debug(version.toString()); return new AigcResponse(version); } return new AigcResponse(null); }
org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query
package cn.aigcsst.repository; import cn.aigcsst.domain.Picture; import io.lettuce.core.dynamic.annotation.Param; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import java.util.Optional; @Repository public interface PictureRepository extends JpaRepository<Picture, Long> { @Query("UPDATE Picture SET share = :status WHERE id=:id") @Modifying int updateShareStatus(@Param("id") Long id, @Param("status") boolean status); }
Service
public int share(Long id, boolean status) { return pictureRepository.updateShareStatus(id, status); }
解决方案,再 Server 方法增加事务注解
@Transactional(rollbackFor = Exception.class) public int share(Long id, boolean status) { return pictureRepository.updateShareStatus(id, status); }