【问题标题】:How to get Collection<? extends Long> from BooleanExpession in QueryDsl如何获得收藏<?从 QueryDsl 中的 BooleanExpession 扩展 Long>
【发布时间】:2017-07-24 04:53:41
【问题描述】:

我在 QueryDsl 中编写一个查询时遇到问题。我用sql写的。下面是它的样子:

select * from isa_proj.restaurant_table where isa_proj.restaurant_table.id not in 
    (SELECT restaurant_table_id FROM isa_proj.reservation r where r.restaurant_id = 1 AND
    reservation_begin not between CAST('2017-02-17 7:30' AS datetime) and CAST('2017-02-17 9:30' AS datetime) and
    reservation_expire not between CAST('2017-02-17 7:30' AS datetime) and CAST('2017-02-17 9:30' AS datetime));

我试过这个:

Predicate pred = QRestaurantTable.restaurantTable.id.eq(id).notIn(QReservation.reservation.restaurant.id.eq(id).and(
                QReservation.reservation.reservationBegin.notBetween(start, end)).and(
                QReservation.reservation.reservationEnd.notBetween(start, end)));

但我收到此错误消息:

The method notIn(Number...) in the type NumberExpression<Long> is not applicable for the arguments (BooleanExpression)

这是我第一次使用 QueryDsl。我不知道如何从以下位置获取 restaurant_table_id:

QReservation.reservation.restaurant.id.eq(id).and(
                QReservation.reservation.reservationBegin.notBetween(start, end)).and(
                QReservation.reservation.reservationEnd.notBetween(start, end))

...或者如何正确地做到这一点。

编辑: pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>restaurant</groupId>
    <artifactId>isa_proj</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>isa_proj</name>
    <url>http://maven.apache.org</url>
    <description>WEB application used for study purpose.</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- MYSQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>



    </dependencies>

    <build>
        <plugins>

            <!-- https://github.com/querydsl/apt-maven-plugin -->
            <!--Dependency: https://mvnrepository.com/artifact/com.querydsl/querydsl-apt  -->
            <!-- <plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>apt-maven-plugin</artifactId>
                <version>1.1.3</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>src/main/java/restaurant/jpa/domain/queries</outputDirectory>
                            <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>com.querydsl</groupId>
                        <artifactId>querydsl-apt</artifactId>
                        <version>4.1.4</version>
                    </dependency>
                </dependencies>
            </plugin> -->


            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

谢谢。米洛拉德析米克

【问题讨论】:

    标签: mysql hibernate predicate querydsl


    【解决方案1】:

    你可以试试这个:

    Predicate pred = QRestaurantTable.restaurantTable.id.notIn(new JPASubQuery()
                             .from(QReservation.reservation.restaurant)
                             .where(QReservation.reservation.restaurant.id.eq(id)
                                      .and(QReservation.reservation.reservationBegin.notBetween(start, end))
                                      .and(QReservation.reservation.reservationEnd.notBetween(start, end)))
                             .list(QReservation.reservation.restaurantTable.id));
    

    【讨论】:

    • 我在将 com.mysema.querydsl 中的任何内容添加到我的 pom 文件时遇到问题。我在我的 pom 中将 com.querydsl 用于 queryDsl。如果我尝试用 myshema 替换它,我会得到错误: Missing artifact com.myshema.querydsl:querydsl-jpa:jar:3.4.0 and can't build project。我试图更新和清理项目,但仍然是同样的错误。并且 com.querydsl 没有 JPASubQuery 类。所以我什至无法测试它。
    • 我编辑了帖子并修复了查询,我在 notIn() 之前缺少一个 .eq(id)。但还是和sql查询不一样。
    猜你喜欢
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    相关资源
    最近更新 更多