java - Hibernate 在一对多映射上添加 where 子句

标签 java database hibernate mapping

我的 .hbm 文件中有这个设置。

<set name="subTopicsTb" table="subtopics_tb" inverse="true" lazy="false" fetch="select">
        <key>
            <column name="topic_id" />
        </key>
        <one-to-many class="com.topics.model.SubTopics" />
    </set>

现在,默认情况下,hibernate 获取所有子主题,其中 topic_id 是 id。 我想过滤子主题。添加类似 where subTopics.date is not null 谢谢

最佳答案

这确实有效,因为 date 是您数据库中的列名。 set 中的 where 属性 appends raw sql 到你的查询,所以你必须指定它在你的数据库中并且 NOT HQL :

<set name="subTopicsTb" table="subtopics_tb" inverse="true" lazy="false" 
    fetch="select" where="date is not null">
    <key>
        <column name="topic_id" />
    </key>
    <one-to-many class="com.topics.model.SubTopics" />
</set>

关于java - Hibernate 在一对多映射上添加 where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14565241/

相关文章:

Hibernate 默认 Lob Lazy/Eager

java - Spring Boot 数据插入一行,其中包含已保存到数据库的数据

java - 错误 : no suitable constructor after update Android Studio from 0. 6.1 到 0.8.9

java - JdbcTemplate映射器空值

java - 使用 Java Mail API 时出现问题

php - 远程访问EC2上的mysql

java - Java 1.5 中@Retention 背后发生了什么

php - 将 html-php comboBox 与数据库同步

sql-server - 由多个用户编辑数据库记录

java - Hibernate saveOrUpdate 大数据