Я пытаюсь подключить базу данных MySQL с помощью Hibernate при создании небольшого ПО Micronaut C. Но я не могу создать соединение, и схема / объекты не создаются.
Пожалуйста, помогите мне, я застрял на этом
Я прошел через
http://guides.micronaut.io/micronaut-data-access-jpa-hibernate/guide/index.html
но бесполезно
Ниже приведен мой код:
зависимости файла build.gradle
dependencies {
compile "io.micronaut:micronaut-http-server-netty"
compile "io.micronaut.configuration:micronaut-hibernate-jpa"
annotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
annotationProcessor "io.micronaut:micronaut-inject-java"
annotationProcessor "io.micronaut:micronaut-validation"
annotationProcessor "io.micronaut.configuration:micronaut-openapi"
annotationProcessor "io.micronaut.data:micronaut-data-processor"
implementation platform("io.micronaut:micronaut-bom:$micronautVersion")
implementation "io.micronaut:micronaut-inject"
implementation "io.micronaut:micronaut-validation"
implementation "io.micronaut:micronaut-runtime"
implementation "io.micronaut.configuration:micronaut-jdbc-tomcat"
implementation "io.swagger.core.v3:swagger-annotations"
implementation "io.micronaut:micronaut-http-client"
implementation "io.micronaut.configuration:micronaut-hibernate-jpa" // <1>
implementation "io.micronaut.configuration:micronaut-jdbc-hikari" // <2>
implementation "io.micronaut:micronaut-http-server-netty"
implementation "io.micronaut.data:micronaut-data-hibernate-jpa"
implementation "javax.annotation:javax.annotation-api"
testAnnotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
testAnnotationProcessor "io.micronaut:micronaut-inject-java"
testImplementation platform("io.micronaut:micronaut-bom:$micronautVersion")
testImplementation "org.junit.jupiter:junit-jupiter-api"
testImplementation "io.micronaut.test:micronaut-test-junit5"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine"
runtimeOnly "ch.qos.logback:logback-classic:1.2.3"
compile group: 'mysql', name: 'mysql-connector-java', version:'8.0.13'}
файл application.yml:
micronaut:
application:
name: demoapp
server:
port: 8081
datasources:
default:
url: dbc:mysql://localhost:3306/Micronaut
username: root
password: password
driverClassName: "com.mysql.cj.jdbc.Driver"
dialect: MYSQL
jpa:
default:
packages-to-scan: "demoapp.entity"
properties:
hibernate:
hbm2ddl:
auto: update
show_sql: true
Контроллер
@Controller("/hello")
public class HelloController {
private GenreRepo genreRepo;
@Post(value = "/save")
public String saveGenre() {
Genre entity = new Genre();
entity.setId(Long.valueOf(10));
entity.setName("dempApp");
System.out.println("entity::"+ entity.getId());
genreRepo.save(entity);
return "save entity success" ;
}
}
Объект:
@Entity
@Table(name = "genre")
public class Genre {
public Genre() {}
public Genre(@NotNull String name) {
this.name = name;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@Column(name = "name", nullable = false, unique = true)
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
Ошибка:
13:33:01.881 [pool-1-thread-3] ERROR i.m.h.s.netty.RoutingInBoundHandler - Unexpected error occurred: null
java.lang.NullPointerException: null
at demoapp.controller.HelloController.saveGenre(HelloController.java:50)
at demoapp.controller.$HelloControllerDefinition$$exec4.invokeInternal(Unknown Source)
at io.micronaut.context.AbstractExecutableMethod.invoke(AbstractExecutableMethod.java:146)
at io.micronaut.context.DefaultBeanContext$BeanExecutionHandle.invoke(DefaultBeanContext.java:3016)
at io.micronaut.web.router.AbstractRouteMatch.execute(AbstractRouteMatch.java:228)
at io.micronaut.web.router.RouteMatch.execute(RouteMatch.java:122)
at io.micronaut.http.server.netty.RoutingInBoundHandler.lambda$buildResultEmitter$16(RoutingInBoundHandler.java:1460)
at io.reactivex.internal.operators.flowable.FlowableCreate.subscribeActual(FlowableCreate.java:71)
at io.reactivex.Flowable.subscribe(Flowable.java:14935)
at io.reactivex.internal.operators.flowable.FlowableMap.subscribeActual(FlowableMap.java:37)
at io.reactivex.Flowable.subscribe(Flowable.java:14935)
at io.reactivex.internal.operators.flowable.FlowableSwitchIfEmpty.subscribeActual(FlowableSwitchIfEmpty.java:32)
at io.reactivex.Flowable.subscribe(Flowable.java:14935)
at io.reactivex.Flowable.subscribe(Flowable.java:14882)
at io.micronaut.http.server.context.ServerRequestContextFilter$1.subscribe(ServerRequestContextFilter.java:66)
at io.reactivex.internal.operators.flowable.FlowableFromPublisher.subscribeActual(FlowableFromPublisher.java:29)
at io.reactivex.Flowable.subscribe(Flowable.java:14935)
at io.reactivex.Flowable.subscribe(Flowable.java:14882)
at io.reactivex.internal.operators.flowable.FlowableSubscribeOn$SubscribeOnSubscriber.run(FlowableSubscribeOn.java:82)
at io.reactivex.internal.schedulers.ExecutorScheduler$ExecutorWorker$BooleanRunnable.run(ExecutorScheduler.java:288)
at io.reactivex.internal.schedulers.ExecutorScheduler$ExecutorWorker.run(ExecutorScheduler.java:253)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:830)