【问题标题】:Join and where clause using Criteria in Spring boot在 Spring Boot 中使用 Criteria 的 Join 和 where 子句
【发布时间】:2017-06-18 14:34:50
【问题描述】:

我正在尝试实现使用命名查询很容易实现的目标,但我想使用标准来实现。 下面是我的两张表。

Notification:
id
userId (foreign key to user) 
Notification
Visible

User:
userId,
name,
address

不,我想获取与用户(给定用户 ID)对应的可见通知(值设置为 1)

一种从用户获取通知并对其进行迭代以查看哪个通知可见的方法,但我不想这样做,因为这会不必要地从数据库中提取大量数据。

最好的方法应该是什么?

【问题讨论】:

    标签: spring-data-jpa criteria-api


    【解决方案1】:

    您可以使用 Spring data jpa 轻松实现这一目标

    创建一个扩展 JPA 存储库的接口

    @Repository
    @RepositoryRestResource
    public interface NotificationRepository extends JpaRepository<Notification,Long (datatype for primary key)> {
    
    List<Notification> findByUseridAndVisible(Long userid,int visible);
    }
    

    现在您可以创建服务类并直接使用此方法

    class service(){
    @Autowired
    NotificationRepository notificationrepository;
    
    public void method(){
    Long userid=85;
    int visible=1;
    List<Notification> getnotificationlist= notificationrepository.findByUseridAndVisible(userid,visible);
    }
    }
    

    希望这会对你有所帮助。

    【讨论】:

    • 我的模型中没有用户 ID,而是有用户对象(多对一关系)。如果我将用户 ID 替换为用户,则会出现异常“参数值 [1] 与预期类型 [java.lang.Boolean (n/a)] 不匹配”
    猜你喜欢
    • 2017-05-13
    • 1970-01-01
    • 2017-01-18
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    相关资源
    最近更新 更多