Home > Software engineering >  Join Table from different database using Herbernate in quarkus
Join Table from different database using Herbernate in quarkus

Time:06-21

There is two tables in two database. I set my configuration based on Hibernate ORM Documentation.

Error message is "java.lang.IllegalStateException: No entities were found. Did you forget to annotate your Panache Entity classes with '@Entity'?" when I call it.

However, it is work when two tables in one database.

package wsi.model.entity.database01;

import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import wsi.model.entity.database02.Address;

import javax.persistence.*;

@Entity
@Table(name = "users")
@Cacheable
public class Users {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;

    @OneToOne(cascade = CascadeType.ALL)
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name = "address_id", referencedColumnName = "id")
    private Address address;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}
    package wsi.model.entity.database02;
    
    import javax.persistence.*;
    
    @Entity
    @Table(name = "address")
    public class Address {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "id")
        private int id;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    }
# development database configuration

quarkus.datasource.db-kind = mysql
quarkus.datasource.username = root
quarkus.datasource.password =
quarkus.datasource.jdbc.url = jdbc:mysql://localhost:3306/database01?useSSL=false&autoreconnect=true&zeroDateTimeBehavior=convertToNull

quarkus.datasource."database02".db-kind = mysql
quarkus.datasource."database02".username = root
quarkus.datasource."database02".password =
quarkus.datasource."database02".jdbc.url = jdbc:mysql://localhost:3306/database02?useSSL=false&autoreconnect=true&zeroDateTimeBehavior=convertToNull
quarkus.hibernate-orm."database02".datasource=database02
quarkus.hibernate-orm."database02".packages=wsi.model.entity.database02
plugins {
    id 'java'
    id 'io.quarkus'
}

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
    implementation 'io.quarkus:quarkus-agroal:2.10.0.Final'
    implementation 'io.quarkus:quarkus-resteasy-reactive:2.10.0.Final'
    implementation 'io.quarkus:quarkus-resteasy-reactive-jackson:2.10.0.Final'
    implementation 'io.quarkus:quarkus-hibernate-orm:2.10.0.Final'
    implementation "io.quarkus:quarkus-hibernate-orm-panache:2.10.0.Final"
    implementation 'io.quarkus:quarkus-jdbc-mysql:2.10.0.Final'
    implementation 'io.quarkus:quarkus-hibernate-validator:8.0.0.Alpha3'
    implementation("org.jboss.logmanager:log4j2-jboss-logmanager:1.1.1.Final")
    testImplementation 'io.quarkus:quarkus-junit5:2.10.0.Final'

}

group 'org.WSE'
version '1.0.0-SNAPSHOT'

java {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
}

compileJava {
    options.encoding = 'UTF-8'
    options.compilerArgs << '-parameters'
}

compileTestJava {
    options.encoding = 'UTF-8'
}

CodePudding user response:

It think that, because you are using the packages property, you need to set it for the default persistenceUnit as well (if you want to use the same entities):

quarkus.hibernate-orm.packages=wsi.model.entity.database01
  • Related