Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hibernate with jbang fails with class loader issue #11913

Closed
maxandersen opened this issue Sep 4, 2020 · 14 comments · Fixed by #11957
Closed

hibernate with jbang fails with class loader issue #11913

maxandersen opened this issue Sep 4, 2020 · 14 comments · Fixed by #11957
Labels
area/hibernate-orm Hibernate ORM area/jbang Issues related to when using jbang.dev with Quarkus area/persistence OBSOLETE, DO NOT USE triage/invalid This doesn't seem right
Milestone

Comments

@maxandersen
Copy link
Member

Describe the bug
use this code:

//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS io.quarkus:quarkus-picocli:1.8.0.CR1
//DEPS io.quarkus:quarkus-hibernate-orm-panache:1.8.0.CR1
//DEPS io.quarkus:quarkus-jdbc-postgresql:1.8.0.CR1
//JAVA_OPTIONS -Djava.util.logging.manager=org.jboss.logmanager.LogManager
//Q:CONFIG quarkus.banner.enabled=false
//Q:CONFIG quarkus.log.level=WARN
//Q:CONFIG quarkus.datasource.db-kind = postgresql
//Q:CONFIG quarkus.datasource.username = sarah
//Q:CONFIG quarkus.datasource.password = connor
//Q:CONFIG quarkus.datasource.jdbc.url = jdbc:tc:postgresql:9.6.8:///databasename
//Q:CONFIG quarkus.hibernate-orm.database.generation = drop-and-create

import io.quarkus.hibernate.orm.panache.PanacheEntity;
import picocli.CommandLine;

import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.persistence.Entity;

import io.quarkus.runtime.annotations.QuarkusMain;
import io.quarkus.runtime.QuarkusApplication;

import java.time.LocalDate;
import java.util.List;

@QuarkusMain
@CommandLine.Command 
public class quarkusclidb implements Runnable, QuarkusApplication {

    @CommandLine.Option(names = {"-n", "--name"}, description = "Who will we greet?", defaultValue = "World")
    String name;

    @Inject
    CommandLine.IFactory factory; 

    private final GreetingService greetingService;

    public quarkusclidb(GreetingService greetingService) {
        this.greetingService = greetingService;
    }

    @Override
    public void run() {
        greetingService.sayHello(name);
    }

    @Override
    public int run(String... args) throws Exception {
        return new CommandLine(this, factory).execute(args);
    }

    public static void main(String... args) {
        io.quarkus.runtime.Quarkus.run(quarkusclidb.class, args);
    }

    @Entity
    static public class Person extends PanacheEntity {
        public String name;
        public LocalDate birth;
        public String status;

        public static Person findByName(String name){
            return find("name", name).firstResult();
        }

        public static List<Person> findAlive(){
            return list("status", "alive");
        }

        public static void deleteStefs(){
            delete("name", "Stef");
        }
    }
}

@Dependent
class GreetingService {
    void sayHello(String name) {
        System.out.println("Hello " + name + "!");
    }
}

build/run it with: jbang quarkusclidb.java

and you get:

[jbang] Post build with io.quarkus.launcher.JBangIntegration
Sep 05, 2020 12:42:05 AM org.jboss.threads.Version <clinit>
INFO: JBoss Threads version 3.1.1.Final
Sep 05, 2020 12:42:05 AM io.quarkus.agroal.deployment.AgroalProcessor build
WARN: The Agroal dependency is present but no JDBC datasources have been defined.
dev.jbang.ExitException: Issue running postBuild()
	at dev.jbang.IntegrationManager.runIntegration(IntegrationManager.java:116)
	at dev.jbang.cli.Run.build(Run.java:217)
	at dev.jbang.cli.Run.doCall(Run.java:97)
	at dev.jbang.cli.BaseCommand.call(BaseCommand.java:59)
	at dev.jbang.cli.BaseCommand.call(BaseCommand.java:13)
	at picocli.CommandLine.executeUserObject(CommandLine.java:1933)
	at picocli.CommandLine.access$1100(CommandLine.java:145)
	at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2332)
	at picocli.CommandLine$RunLast.handle(CommandLine.java:2326)
	at picocli.CommandLine$RunLast.handle(CommandLine.java:2291)
	at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2159)
	at picocli.CommandLine.execute(CommandLine.java:2058)
	at dev.jbang.Main.main(Main.java:14)
Caused by: java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at dev.jbang.IntegrationManager.runIntegration(IntegrationManager.java:89)
	... 12 more
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
	at io.quarkus.launcher.JBangIntegration.postBuild(JBangIntegration.java:85)
	... 17 more
Caused by: java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at io.quarkus.launcher.JBangIntegration.postBuild(JBangIntegration.java:77)
	... 17 more
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
	[error]: Build step io.quarkus.hibernate.orm.deployment.HibernateOrmProcessor#parsePersistenceXmlDescriptors threw an exception: java.lang.NoClassDefFoundError: org/xml/sax/SAXException
	at io.quarkus.hibernate.orm.deployment.HibernateOrmProcessor.parsePersistenceXmlDescriptors(HibernateOrmProcessor.java:225)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at io.quarkus.deployment.ExtensionLoader$2.execute(ExtensionLoader.java:936)
	at io.quarkus.builder.BuildContext.run(BuildContext.java:277)
	at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
	at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2046)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1578)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1452)
	at java.base/java.lang.Thread.run(Thread.java:834)
	at org.jboss.threads.JBossThread.run(JBossThread.java:479)
Caused by: java.lang.ClassNotFoundException: org.xml.sax.SAXException
	at io.quarkus.launcher.RuntimeLaunchClassLoader.findClass(RuntimeLaunchClassLoader.java:25)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	at io.quarkus.bootstrap.classloading.QuarkusClassLoader.loadClass(QuarkusClassLoader.java:406)
	at io.quarkus.bootstrap.classloading.QuarkusClassLoader.loadClass(QuarkusClassLoader.java:363)
	... 13 more

	at io.quarkus.bootstrap.JBangBuilderImpl.postBuild(JBangBuilderImpl.java:81)
	... 22 more
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
	[error]: Build step io.quarkus.hibernate.orm.deployment.HibernateOrmProcessor#parsePersistenceXmlDescriptors threw an exception: java.lang.NoClassDefFoundError: org/xml/sax/SAXException
	at io.quarkus.hibernate.orm.deployment.HibernateOrmProcessor.parsePersistenceXmlDescriptors(HibernateOrmProcessor.java:225)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at io.quarkus.deployment.ExtensionLoader$2.execute(ExtensionLoader.java:936)
	at io.quarkus.builder.BuildContext.run(BuildContext.java:277)
	at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
	at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2046)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1578)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1452)
	at java.base/java.lang.Thread.run(Thread.java:834)
	at org.jboss.threads.JBossThread.run(JBossThread.java:479)
Caused by: java.lang.ClassNotFoundException: org.xml.sax.SAXException
	at io.quarkus.launcher.RuntimeLaunchClassLoader.findClass(RuntimeLaunchClassLoader.java:25)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	at io.quarkus.bootstrap.classloading.QuarkusClassLoader.loadClass(QuarkusClassLoader.java:406)
	at io.quarkus.bootstrap.classloading.QuarkusClassLoader.loadClass(QuarkusClassLoader.java:363)
	... 13 more

	at io.quarkus.bootstrap.app.CuratedApplication.runInCl(CuratedApplication.java:132)
	at io.quarkus.bootstrap.app.CuratedApplication.runInAugmentClassLoader(CuratedApplication.java:82)
	at io.quarkus.bootstrap.JBangBuilderImpl.postBuild(JBangBuilderImpl.java:78)
	... 22 more
Caused by: java.lang.RuntimeException: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
	[error]: Build step io.quarkus.hibernate.orm.deployment.HibernateOrmProcessor#parsePersistenceXmlDescriptors threw an exception: java.lang.NoClassDefFoundError: org/xml/sax/SAXException
	at io.quarkus.hibernate.orm.deployment.HibernateOrmProcessor.parsePersistenceXmlDescriptors(HibernateOrmProcessor.java:225)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at io.quarkus.deployment.ExtensionLoader$2.execute(ExtensionLoader.java:936)
	at io.quarkus.builder.BuildContext.run(BuildContext.java:277)
	at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
	at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2046)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1578)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1452)
	at java.base/java.lang.Thread.run(Thread.java:834)
	at org.jboss.threads.JBossThread.run(JBossThread.java:479)
Caused by: java.lang.ClassNotFoundException: org.xml.sax.SAXException
	at io.quarkus.launcher.RuntimeLaunchClassLoader.findClass(RuntimeLaunchClassLoader.java:25)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	at io.quarkus.bootstrap.classloading.QuarkusClassLoader.loadClass(QuarkusClassLoader.java:406)
	at io.quarkus.bootstrap.classloading.QuarkusClassLoader.loadClass(QuarkusClassLoader.java:363)
	... 13 more

	at io.quarkus.deployment.jbang.JBangAugmentorImpl.accept(JBangAugmentorImpl.java:119)
	at io.quarkus.deployment.jbang.JBangAugmentorImpl.accept(JBangAugmentorImpl.java:36)
	at io.quarkus.bootstrap.app.CuratedApplication.runInCl(CuratedApplication.java:129)
	... 24 more
Caused by: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
	[error]: Build step io.quarkus.hibernate.orm.deployment.HibernateOrmProcessor#parsePersistenceXmlDescriptors threw an exception: java.lang.NoClassDefFoundError: org/xml/sax/SAXException
	at io.quarkus.hibernate.orm.deployment.HibernateOrmProcessor.parsePersistenceXmlDescriptors(HibernateOrmProcessor.java:225)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at io.quarkus.deployment.ExtensionLoader$2.execute(ExtensionLoader.java:936)
	at io.quarkus.builder.BuildContext.run(BuildContext.java:277)
	at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
	at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2046)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1578)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1452)
	at java.base/java.lang.Thread.run(Thread.java:834)
	at org.jboss.threads.JBossThread.run(JBossThread.java:479)
Caused by: java.lang.ClassNotFoundException: org.xml.sax.SAXException
	at io.quarkus.launcher.RuntimeLaunchClassLoader.findClass(RuntimeLaunchClassLoader.java:25)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	at io.quarkus.bootstrap.classloading.QuarkusClassLoader.loadClass(QuarkusClassLoader.java:406)
	at io.quarkus.bootstrap.classloading.QuarkusClassLoader.loadClass(QuarkusClassLoader.java:363)
	... 13 more

	at io.quarkus.builder.Execution.run(Execution.java:116)
	at io.quarkus.builder.BuildExecutionBuilder.execute(BuildExecutionBuilder.java:79)
	at io.quarkus.deployment.QuarkusAugmentor.run(QuarkusAugmentor.java:149)
	at io.quarkus.deployment.jbang.JBangAugmentorImpl.accept(JBangAugmentorImpl.java:97)
	... 26 more
Caused by: java.lang.NoClassDefFoundError: org/xml/sax/SAXException
	at io.quarkus.hibernate.orm.deployment.HibernateOrmProcessor.parsePersistenceXmlDescriptors(HibernateOrmProcessor.java:225)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at io.quarkus.deployment.ExtensionLoader$2.execute(ExtensionLoader.java:936)
	at io.quarkus.builder.BuildContext.run(BuildContext.java:277)
	at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
	at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2046)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1578)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1452)
	at java.base/java.lang.Thread.run(Thread.java:834)
	at org.jboss.threads.JBossThread.run(JBossThread.java:479)
Caused by: java.lang.ClassNotFoundException: org.xml.sax.SAXException
	at io.quarkus.launcher.RuntimeLaunchClassLoader.findClass(RuntimeLaunchClassLoader.java:25)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	at io.quarkus.bootstrap.classloading.QuarkusClassLoader.loadClass(QuarkusClassLoader.java:406)
	at io.quarkus.bootstrap.classloading.QuarkusClassLoader.loadClass(QuarkusClassLoader.java:363)
	... 13 more

Probably similar issue as in #11542 where we found places TCCL was not put as a fallback for class.forname.

@maxandersen maxandersen added the kind/bug Something isn't working label Sep 4, 2020
@quarkusbot
Copy link

/cc @quarkusio/devtools
/cc @gsmet, @Sanne

@quarkusbot quarkusbot added area/hibernate-orm Hibernate ORM area/jbang Issues related to when using jbang.dev with Quarkus area/persistence OBSOLETE, DO NOT USE labels Sep 4, 2020
@Sanne
Copy link
Member

Sanne commented Sep 7, 2020

Probably similar issue as in #11542 where we found places TCCL was not put as a fallback for class.forname.

Could you expand on this? Why should we use such a fallback? One of the design goals in Quarkus was to use a flat classloader model (with the obvious exception of build time and dev mode).

@Sanne
Copy link
Member

Sanne commented Sep 7, 2020

@maxandersen we will need a way to have integration tests run with jbang. Do you have a way?

@Sanne
Copy link
Member

Sanne commented Sep 7, 2020

Looks like jbang is running it on JDK11 by default?

In that case you need to adjust the dependencies, add

//DEPS javax.xml.bind:jaxb-api:2.3.1
//DEPS org.glassfish.jaxb:jaxb-runtime:2.3.1

@maxandersen
Copy link
Member Author

Probably similar issue as in #11542 where we found places TCCL was not put as a fallback for class.forname.

Could you expand on this? Why should we use such a fallback? One of the design goals in Quarkus was to use a flat classloader model (with the obvious exception of build time and dev mode).

@stuartwdouglas should comment as he was the one suggesting the way to integrate.

I.e. afaik I don't do anything much different from maven/gradle in that I just create a class loader with all the jars and then run from there.

I'm equally surprised it's needed and I can see I'm bumping into it basically everywhere so I'm wondering if there is a better way for jbang to call out to the spi ...

@Sanne
Copy link
Member

Sanne commented Sep 7, 2020

Also, see my complaints at : https://github.com/quarkusio/quarkus/wiki/Why-Dropping-Java-8#classpath-cleanup :)

I feel like we should add the JDK11 specific dependencies, and stop supporting Java 8 as this is really getting in the way of people having fun with this all.

@Sanne
Copy link
Member

Sanne commented Sep 7, 2020

I'll close this, I'll leave it to you @maxandersen to think if we can improve the user experience or tooling around this - personally I feel these requirements are already documented, and the pain points tracked on the wiki.

In particular I'd highlight this section of the above-linked wiki:

We all strife to make reasonable defaults, but dependency management isn’t currently able to deal on a per-platform dependency switch.

That's perhaps something we can improve on, especially via jbang?

@Sanne Sanne closed this as completed Sep 7, 2020
@Sanne Sanne added triage/invalid This doesn't seem right and removed kind/bug Something isn't working labels Sep 7, 2020
@maxandersen
Copy link
Member Author

@maxandersen we will need a way to have integration tests run with jbang. Do you have a way?

I run integration tests in jbang itself with karate. Works pretty nice.

Missing piece is install of jbang so it will be accessible in path but that should be pretty straight forward.

Actually - can we assume access to docker? Then we can use the jbang docker image.

@maxandersen
Copy link
Member Author

Looks like jbang is running it on JDK11 by default?

it runs with users default jvm ... only gets jdk11 if it can't find anything else.

@maxandersen
Copy link
Member Author

In that case you need to adjust the dependencies, add

i added those but not seeing a difference in behavior - it worked for you ?

@maxandersen
Copy link
Member Author

reopening until I can actually run it ;)

and yes; if we can fix some of this in jbang thats great - not sure how though ...

@maxandersen maxandersen reopened this Sep 7, 2020
@maxandersen
Copy link
Member Author

tried with java 8 and 11 and with and without the updated deps - same result. Did you get different result?

@Sanne
Copy link
Member

Sanne commented Sep 7, 2020

hum, sorry yes it seemed to work but I forgot to use --verbose. It actually still fails.

stuartwdouglas added a commit to stuartwdouglas/quarkus that referenced this issue Sep 7, 2020
stuartwdouglas added a commit to stuartwdouglas/quarkus that referenced this issue Sep 7, 2020
stuartwdouglas added a commit to stuartwdouglas/quarkus that referenced this issue Sep 7, 2020
@maxandersen
Copy link
Member Author

with #11957 this updated (as the original had bugs/was incomplete) works:

//usr/bin/env jbang "$0" "$@" ; exit $?
/**
 * Run this with `jbang -Dquarkus.container-image.build=true build quarkus.java`
 * and it builds a docker image.
 */
//DEPS io.quarkus:quarkus-picocli:${q.v:1.8.0.CR1}
//DEPS io.quarkus:quarkus-hibernate-orm-panache:${q.v:1.8.0.CR1}
//DEPS io.quarkus:quarkus-jdbc-postgresql:${q.v:1.8.0.CR1}
//DEPS org.testcontainers:postgresql:1.14.3
//DEPS org.postgresql:postgresql:42.1.4
//JAVA_OPTIONS -Djava.util.logging.manager=org.jboss.logmanager.LogManager
//Q:CONFIG quarkus.banner.enabled=false
//Q:CONFIG quarkus.log.level=WARN
//Q:CONFIG quarkus.datasource.db-kind=postgresql
//Q:CONFIG quarkus.datasource.username=sarah
//Q:CONFIG quarkus.datasource.password=connor
//Q:CONFIG quarkus.datasource.jdbc.url=jdbc:postgresql:databasename
//Q:CONFIG quarkus.hibernate-orm.database.generation=drop-and-create

import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.PortBinding;
import com.github.dockerjava.api.model.Ports;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
import org.testcontainers.containers.PostgreSQLContainer;
import picocli.CommandLine;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.control.ActivateRequestContext;
import javax.inject.Inject;
import javax.persistence.Entity;
import javax.transaction.Transactional;

import io.quarkus.runtime.annotations.QuarkusMain;
import io.quarkus.runtime.QuarkusApplication;

import java.time.LocalDate;
import java.time.Month;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

@QuarkusMain
@CommandLine.Command 
public class quarkusclidb implements Runnable, QuarkusApplication {

    @CommandLine.Option(names = {"-n", "--name"}, description = "Who will we greet?", defaultValue = "World")
    String name;

    @Inject
    CommandLine.IFactory factory; 

    private final GreetingService greetingService;

    public quarkusclidb(GreetingService greetingService) {
        this.greetingService = greetingService;
    }

    @Override
    @ActivateRequestContext
    @Transactional
    public void run() {
       greetingService.sayHello(name);
    }

    @Override
    public int run(String... args) throws Exception {
        return new CommandLine(this, factory).execute(args);
    }

    public static void main(String... args) {
        int hostPort = 5432;
        int containerExposedPort = 5432;

        Consumer<CreateContainerCmd> cmd = e -> e.withPortBindings(new PortBinding((Ports.Binding.bindPort(hostPort)),new ExposedPort(containerExposedPort)));

        var dbcontainer = new PostgreSQLContainer("postgres:11.1").withDatabaseName("databasename")
        .withUsername("sarah").withPassword("connor").withCreateContainerCmdModifier(cmd);

        dbcontainer.start();
        io.quarkus.runtime.Quarkus.run(quarkusclidb.class, args);
    }

    @Entity
    static public class Person extends PanacheEntity {
        public String name;
        public LocalDate birth;
        public String status;

        public static Person findByName(String name){
            return find("name", name).firstResult();
        }

        public static List<Person> findAlive(){
            return list("status", "alive");
        }

        public static void deleteStefs(){
            delete("name", "Stef");
        }
    }

    @Dependent
    static public class GreetingService {
        void sayHello(String name) {

            // creating a person
            Person person = new Person();
            person.name = "Stef";
            person.birth = LocalDate.of(1910, Month.FEBRUARY, 1);
            person.status = "alive";

            // persist it
            person.persist();

            List<Person> allPersons = Person.listAll();

            allPersons.forEach(System.out::println);

            long countAll = Person.count();

            System.out.println("Person count:" + countAll);

        }
    }
}


@gsmet gsmet added this to the 1.8.0.Final milestone Sep 8, 2020
gsmet pushed a commit to gsmet/quarkus that referenced this issue Sep 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/hibernate-orm Hibernate ORM area/jbang Issues related to when using jbang.dev with Quarkus area/persistence OBSOLETE, DO NOT USE triage/invalid This doesn't seem right
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants