zoukankan      html  css  js  c++  java
  • springboot jpa---->总结一下遇到的问题

    Native Query throw exception

    • dto code
    import lombok.Value;
    
    @Value
    public class IdsOnly {
    
        Integer id;
        String otherId;
    }
    
    • repository
    public interface TestTableRepository extends JpaRepository<TestTable, Integer> {
    
        @Query(value = "select id, otherId from TestTable where CreationDate > ?1", nativeQuery = true)
        public Collection<IdsOnly> findEntriesAfterDate(Date creationDate);
    }
    
    • service
    List<IdsOnly> results = ttRepo.findEntriesAfterDate(theDate);  
    
    • exception
    org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [com.example.IdsOnly]
    

    解决方法有两种

    • 使用NativeQuery:将IdsOnly更换成接口,提供属性的get方法。
    public interface IdsOnly {
        Integer getId();
        String getOtherId();
    }
    
    • 不使用NativeQuery
    @Query("select new com.example.IdsOnly(t.id, t.otherId) from TestTable t where t.creationDate > ?1")
    

    Jpa中没有update方法

    jpa中只有save方法,如果你传递的对象的主键在数据库中存在,那么就是更新操作。否则就是插入操作。

    Delete operation

    JpaRepository Not supported for DML operations [delete query]
    
    • Repository: add @Modifying
    @Modifying
    void deleteByUserIdAndToolId(Integer userId, Integer toolId);
    
    • Service: add @Transactional
    @Transactional
    public void doDeleteUserTool(Integer userId, Integer toolId) {
        userToolMapper.deleteByUserIdAndToolId(userId, toolId);
    }
    

    To be continue

    Industry is the soul of business and the keystone of prosperity.
    

  • 相关阅读:
    万能分页方法!机会难得,在此送给大家
    Java长存!12个Java长久占居主要地位的原因
    angularjs1.x radio组checkbox组
    js动态显示vlc视频直播
    Lodop 分页详解,可详细了呢
    lodop 实现分页打印账单 最后一页右下角加入确认签字
    lodop分页,页眉页脚,foreach分页代码
    ajax最简单验证
    Httpcilent获取带验证码的网站内容
    设计模式(转载)
  • 原文地址:https://www.cnblogs.com/huhx/p/13228766.html
Copyright © 2011-2022 走看看