3

I'm using Hibernate.6.2.5.Final and jpa:3.1.1 with postgres 9.6

There is a problem while generating the sql query for this named query that I declared in the jpa repository.

boolean existsPeriodicByIsbnAndTitle(@Param("isbn") String isbn, @Param("title") String title);

Spring is generating this sql and error:

JDBC exception executing SQL [select p1_0.idperiodic from periodic p1_0 where p1_1.isbn=? and p1_0.title=? fetch first ? rows only] [ERROR: missing FROM-clause entry for table "p1_1"

As you can see is trying to use an alias on the isbn field p1_1 that has not been declared.

Here are my class definitions:

@Getter
@Setter
@EqualsAndHashCode(of = {"id"})
@ToString(of = {"id", "group"})
@MappedSuperclass
public abstract class AbstractPublication {

    @Id
    @OrderBy
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "publication_id_sequence")
    private Long id;

    ....

}

@Getter
@Setter
@Entity
@Table(name = Book.BOOK_TABLE_NAME)
@DiscriminatorColumn(name = Book.BOOK_TYPE_COLUMN, discriminatorType = DiscriminatorType.STRING)
@SequenceGenerator(name = "publication_id_sequence", sequenceName = "book_id_sequence", allocationSize = 1)
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Book extends AbstractPublication {


    @Column(name = "ISBN", length = 50)
    private String isbn;

    ....
}


@Getter
@Setter
@Entity
@DiscriminatorOptions(force = true)
@DiscriminatorValue(value = "PERIODIC")
@Table(name = "PERIODIC")
@PrimaryKeyJoinColumn(name = "IDPERIODIC")
public class Periodic extends Book {

    @Column(name = "title")
    private String title;

    ...
}

I believe the error comes from the inheritence type JOINED between the class Book and the class Periodic. Hibernate knows he can't find the isbn in the Periodic table and then use a new alias, except that it forgets to JOIN with the table book.

I appreciate your help with this issue even though it seems more like an hibernate jpa internal problem.

(Update) Here is my build.gradle file:

plugins {
    // Apply the java plugin to add support for Java
    id 'java'
    // Apply the java-library plugin for API and implementation separation.
    id 'java-library'
    id "org.flywaydb.flyway" version "9.16.3"
    // Apply the application plugin to add support for building a CLI application.
    id 'application'
    id("org.springframework.boot") version "3.1.1"
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'xxxx'
sourceCompatibility = 17

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

ext {
    springBatchVersion = "5.0.1"
    testContainersVersion = "1.18.0"
    lombokVersion = '1.18.26'
}

dependencies {
    implementation("org.jetbrains:annotations:24.0.1")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-mail")
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-cache")
    implementation("org.springframework.boot:spring-boot-starter-websocket")
    implementation("org.springframework.boot:spring-boot-starter-validation")
    implementation("org.springframework.boot:spring-boot-starter-amqp")
    implementation("org.springframework.security:spring-security-ldap")
    implementation("org.springframework:spring-context-support")

    //http client 5 required
    implementation 'org.apache.httpcomponents.client5:httpclient5:5.2.1'

    // spring-batch
    implementation("org.springframework.boot:spring-boot-starter-batch") {
        exclude(module: 'spring-batch-infrastructure')
    }
    implementation("org.springframework.batch:spring-batch-infrastructure:${springBatchVersion}")

    // Logger
    implementation("ch.qos.logback:logback-classic:1.4.6")
    implementation("ch.qos.logback:logback-core:1.4.6")

    implementation("org.apache.commons:commons-compress:1.23.0")

    // Json
    implementation("org.json:json:20230227")
    implementation("com.jayway.jsonpath:json-path:2.8.0")
    implementation("io.rest-assured:json-schema-validator:5.3.0")

    // Freemarker ftlh templates
    implementation('org.freemarker:freemarker:2.3.32')

    //HTMl XML parser
    implementation("org.jsoup:jsoup:1.15.4")
    implementation("net.sourceforge.htmlcleaner:htmlcleaner:2.27")

    //PDF Creator
    implementation("org.xhtmlrenderer:flying-saucer-pdf-itext5:9.1.22")
    implementation('org.apache.pdfbox:pdfbox:2.0.28')

    implementation("net.sf.saxon:Saxon-HE:12.1") {
        exclude group: "net.sf.saxon", module: "xpath"
    }

    // https://mvnrepository.com/artifact/com.github.lookfirst/sardine
    implementation('com.github.lookfirst:sardine:5.10')

    //JACKSON LIBRARY
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.14.2'
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.14.2'
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.14.2'
    implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.14.2'
    implementation group: 'com.fasterxml.jackson.module', name: 'jackson-module-parameter-names', version: '2.14.2'
    implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-guava', version: '2.14.2'
    implementation group: 'com.fasterxml', name: 'classmate', version: '1.5.1'
    // https://mvnrepository.com/artifact/org.jsoup/jsoup
    implementation 'org.jsoup:jsoup:1.16.1' // needed for safe html annotation

    // apache commons
    implementation("org.apache.commons:commons-lang3:3.12.0")
    implementation("org.apache.commons:commons-csv:1.10.0")
    implementation("commons-beanutils:commons-beanutils:1.9.4")
    implementation 'org.apache.commons:commons-text:1.10.0'

    //google
    implementation 'com.google.guava:guava:31.1-jre'

    //apache excel
    implementation 'org.apache.poi:poi-ooxml:5.2.3'
    implementation 'org.apache.commons:commons-compress:1.23.0'

    //openapi
    implementation("org.springdoc:springdoc-openapi-ui:1.7.0")

    // Lombok
    annotationProcessor('org.springframework.boot:spring-boot-configuration-processor:3.0.5',
            "org.projectlombok:lombok:${lombokVersion}")
    compileOnly("org.projectlombok:lombok:${lombokVersion}")
    testAnnotationProcessor("org.projectlombok:lombok:${lombokVersion}")
    testCompileOnly("org.projectlombok:lombok:${lombokVersion}")

    testImplementation("org.dbunit:dbunit:2.6.0")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.springframework.batch:spring-batch-test")

    runtimeOnly("org.postgresql:postgresql:42.6.0")

    //Mockito
    testImplementation 'org.mockito:mockito-inline:5.2.0'
    // testContainers
    testImplementation("org.testcontainers:junit-jupiter:${testContainersVersion}")
    testImplementation("org.testcontainers:postgresql:${testContainersVersion}")
}

// Apply a specific Java toolchain to ease working on different environments.
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

application {
    // Define the main class for the application.
    mainClass = 'xxxx.Application'
    applicationDefaultJvmArgs = ['--add-opens', 'java.base/java.lang=ALL-UNNAMED',
                                 '--add-opens', 'java.base/java.util=ALL-UNNAMED',
                                 '--add-opens', 'java.base/java.io=ALL-UNNAMED']
}

tasks.register('copyResources', Copy) {
    from "upload"
    into "$buildDir/upload"
}

tasks.named('test') {
    // Use JUnit Platform for unit tests.
    useJUnitPlatform()
}

There are similar questions but it was on hibernate 5.4.4 and I'm using the latest one:

JPA/Hibernate: invalid COUNT query alias for JOINED inheritance JPA/Hibernate pagination - bad COUNT query for @Inheritance(strategy = InheritanceType.JOINED) with @Where

3
  • I tried your code with jpa with Postgres and jpa with mysql and it is generating the correct joined query. Can you please share your pom.xml as well?
    – rahulP
    Commented Jul 15, 2023 at 9:32
  • Thanks for trying @rahulP, I posted my dependencies file. There is also another detail that I didn't post before (now its available) about the classes definition, it's a 3 class dependency AbstractPublication -> Book -> Periodic Commented Jul 17, 2023 at 6:55
  • 2
    I can confirm this is an hibernate problem I downgraded to org.hibernate.orm:hibernate-core:6.1.7.Final and it works perfectly as expected. Commented Jul 17, 2023 at 8:21

0

Browse other questions tagged or ask your own question.