New H2 version breaks liquibase - spring-boot

New version of H2 seems to have broken liquibase and JPA
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.h2test</groupId>
<artifactId>h2test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>h2test</name>
<description>h2test</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.7.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.210</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
classes:
package com.h2test.h2test.domain;
import lombok.Data;
import javax.persistence.*;
#Entity
#Data
public class Person {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column
private String firstName;
#Column
private String lastName;
}
package com.h2test.h2test.repository;
import com.h2test.h2test.domain.Person;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface PersonRepository extends JpaRepository<Person, Long> {
}
#RestController
public class SomethingController {
#Autowired
private PersonRepository personRepository;
#GetMapping
public String test() {
Person person = new Person();
person.setFirstName("first name");
person.setLastName("last name");
personRepository.save(person);
return "hello";
}
}
Liquibase file:
databaseChangeLog:
- changeSet:
id: createTable-example
author: liquibase-docs
changes:
- createTable:
tableName: person
columns:
- column:
name: id
type: int
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: first_name
type: varchar(50)
- column:
name: last_name
type: varchar(50)
constraints:
nullable: false
When triggering the REST ENDPOINT localhost:8080 that creates and saves the Person entity I get the following error:
org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "ID"; SQL statement:
insert into person (id, first_name, last_name) values (null, ?, ?) [23502-202]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:508) ~[h2-2.0.202.jar:2.0.202]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:477) ~[h2-2.0.202.jar:2.0.202]
This seems to only happen in the newer versions of H2. Any help is appreciated

Version 2.6.3 of spring-boot-starter-data-jpa has org.hibernate:hibernate-core:5.6.4.Final in its dependencies. This version doesn't support H2 2.x.y. You need to use 5.6.5:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.5.Final</version>
</dependency>
Also something is wrong with your classpath. You have H2 2.1.210 in dependencies, but exception in your question has error code 23502-202, it means it was produced by H2 2.0.202, you shouldn't use this outdated version, Hibernate ORM may have some problems with it.

Related

Not able to resolve javax.validation.constraints.Email; package

Belo is my whole Entity class code.
package com.email.esp.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
#Entity
#Table(name = "users", uniqueConstraints = { #UniqueConstraint(columnNames = { "email" }) })
public class User {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long userId;
#Column(nullable = false, length = 30)
private String name;
#Email(message = "Email is not valid", regexp = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")#(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])")
#NotEmpty(message = "Email cannot be empty")
private String email;
#Transient // meaning it will not be saved in DB
//#Size(min = 5, max = 15, message = "{register.password.size}")
private String plainPassword; // unencrytped
#Transient
private String repeatPassword;
#Column(nullable = true)
private String phone;
#Column(nullable = false, length = 500)
private String about;
#Column(nullable = true)
private String profilePic;
#Column(name="enabled")
private Boolean enabled = true;
}
I'm trying to use the spring Email core package in my spring boot project. I have added the dependency in my pom file but for some weird reason the import statement for import javax.validation.constraints.Email; is not getting resolved. Any idea why is this happening?? Here is the dependency , i have ...
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.9-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.email</groupId>
<artifactId>esp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>esp</name>
<description>Email Service Provider Using Spring Boot</description>
<properties>
<java.version>17</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-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
javax.validation.constraints.Email annotation depends on Jakarta Bean Validation providers for validation.
You could try to add spring-boot-starter-validation dependency to your pom.xml. This dependency contains all the required dependencies (including jakarta and hibernate-validator which is a compliant validator for this)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

Spring Data JPA intelligence not working in Intellij for OneToMany relation

I have setup new spring boot project with Spring Data JPA and would like to use auto-completion feature to find all records by nested property.
intellij hint for repository method is missing nested properties
I want to retrieve Customers that have matching zipCodes. Declared entities:
#Entity
#Getter
public class Customer {
#Id
private long id;
private String firstName;
#OneToMany(targetEntity = Address.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Address> addresses;
}
#Entity
#Getter
public class Address {
#Id
private long id;
private String streetName;
private String zipCode;
}
It should work out of the box like in article here: https://evonsdesigns.medium.com/spring-jpa-one-to-many-query-examples-281078bc457b
but it does not work. I tried it with following pom config:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.7</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Tried on IntelliJ IDEA 2021.3 (Ultimate Edition)

Error creating bean with name 'entityManagerFactory' error in a Spring boot web application

I'm a beginner in Spring boot coding
When I run my spring boot web application, I got the following error.
Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.lang.ClassCastException: org.hibernate.mapping.SingleTableSubclass cannot be cast to org.hibernate.mapping.RootClass
I am using STS Eclipse and MySql Database
My Connection string in Application.Properties is
spring.datasource.url=jdbc:mysql://localhost:3306/dreamhospital?zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.main.banner-mode=off
server.port=8100
and this is my pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.DreamHospital</groupId>
<artifactId>DreamHospital</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DreamHospital</name>
<description>Demo project for Spring Boot</description>
<properties>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<java.version>1.8</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-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.16.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
here's my entities class:
this is patient entity:
#AllArgsConstructor
#NoArgsConstructor
#Getter
#Setter
#Entity
#DiscriminatorValue("patient")
public class patient implements Serializable{
#Id
private int id;
private String nom;
private String prenom;
#ManyToOne
#JoinColumn(name="idLit")
lit lit;
#ManyToOne
#JoinColumn(name="idM")
medecin medecin;
}
and this is User entity:
#Entity
#Inheritance(strategy=InheritanceType.SINGLE_TABLE)
#DiscriminatorColumn(name="fonction" , discriminatorType=DiscriminatorType.STRING,length=10)
public class User implements Serializable {
#Id
private String UserName;
private String Password;
#Column(name="fonction", insertable = false, updatable = false)
protected String fonction;
and this is medecin entity:
#Data
#Entity
#DiscriminatorValue("medecin")
public class medecin extends User implements Serializable {
#Id #GeneratedValue
private UUID idM;
private String nom;
private String specialite;
#OneToMany(mappedBy="idM")
List<patient> patients;
}
I tried many solutions proposed in stackoverflow but it didn't worked
Apparently there's problem with mappings of User and medecin, you cannot have #Id in superclass and another #Id in subclass. Removing #Id from idM in subclass (medecin) should prevent the exception.

Kotlin SpringBoot and JPA - Problem with relation OneToMany-ManyToOne

I am trying to create a relation OneToMany between two entities and always get an error creating bean with name 'entityManagerFactory':
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-11-23 12:17:20.082 ERROR 17130 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags: [com.moises.kotlinstoreapi.model.Customer.purchases, com.moises.kotlinstoreapi.model.Purchase.purchasesLines]
...
I have created the same type of relation with other two entities (Customer with Purchase) but in this case it doesn't run...
Product code:
package com.moises.kotlinstoreapi.model
import com.fasterxml.jackson.annotation.JsonManagedReference
import java.time.LocalDateTime
import javax.persistence.*
import javax.validation.constraints.NotBlank
import javax.validation.constraints.NotEmpty
import javax.validation.constraints.NotNull
#Entity
#Table(name="products")
data class Product (
#Id #GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0,
#NotBlank
val title: String = "",
#NotEmpty
val price: Double = 999.99,
#NotNull
val created_at: LocalDateTime = LocalDateTime.now(),
val update_at: LocalDateTime? = null,
/**
* Relations
*
*/
#OneToOne(mappedBy="product")
val purchaseLine: PurchaseLine? = null
)
Purchase code:
package com.moises.kotlinstoreapi.model
import com.fasterxml.jackson.annotation.JsonBackReference
import com.fasterxml.jackson.annotation.JsonManagedReference
import com.sun.istack.NotNull
import java.time.LocalDateTime
import javax.persistence.*
import javax.validation.constraints.NotEmpty
#Entity
#Table(name="purchases")
data class Purchase(
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private val id: Long=0,
#Column(name="notes", columnDefinition="TEXT")
val notes: String = "",
#NotNull
val created_at: LocalDateTime = LocalDateTime.now(),
val update_at: LocalDateTime? = null,
/**
* Relations
*
*/
//#ManyToOne(cascade = arrayOf(CascadeType.ALL),fetch= FetchType.EAGER)
//#JoinColumn(name="customer_id")
//#JsonBackReference
//val customer: Customer? = null,
#OneToMany(mappedBy="purchase", cascade=arrayOf(CascadeType.ALL), fetch=FetchType.EAGER)
#JsonManagedReference
var purchasesLines: List<PurchaseLine> = emptyList()
)
PurchaseLine code:
package com.moises.kotlinstoreapi.model
import com.fasterxml.jackson.annotation.JsonBackReference
import com.fasterxml.jackson.annotation.JsonManagedReference
import javax.persistence.*
import javax.validation.constraints.NotEmpty
#Entity
#Table(name="purchase_lines")
data class PurchaseLine(
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private val id: Long=0,
#NotEmpty
val quantity: Int = 1,
/**
* Relations
*
*/
#OneToOne(cascade = arrayOf(CascadeType.ALL))
#JoinColumn(name = "product_id")
#JsonManagedReference
val product: Product? = null,
#ManyToOne(cascade = arrayOf(CascadeType.ALL),fetch= FetchType.EAGER)
#JoinColumn(name="purchase_id")
#JsonBackReference
var purchase: Purchase? = null
)
I am usin IntelliJ with Maven and OpenJDK 11 in Ubuntu.
My pow.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.moises</groupId>
<artifactId>kotlin-store-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>kotlin-store-api</name>
<description>API store with Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<kotlin.version>1.3.50</kotlin.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-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
<plugin>jpa</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-noarg</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

Kotlin SpringBoot and JPA - How to create relations OneToOne?

I am trying to create a relation OneToOne between two entities and always get the error (or very similar, Error creating bean with name 'entityManagerFactory' always):
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.moises.kotlinstoreapi.model.Image.product, referenced property unknown: com.moises.kotlinstoreapi.model.Product.images
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1803) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
...
[spring-boot-devtools-2.2.1.RELEASE.jar:2.2.1.RELEASE]
Caused by: org.hibernate.AnnotationException: Unknown mappedBy in: com.moises.kotlinstoreapi.model.Image.product, referenced property unknown: com.moises.kotlinstoreapi.model.Product.images
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:169) ~[hibernate-core-5.4.8.Final.jar:5.4.8.Final]
I can create without problems the tables if I don't put the relations.
My code:
package com.moises.kotlinstoreapi.model
import com.fasterxml.jackson.annotation.JsonManagedReference
import javax.persistence.*
import javax.validation.constraints.NotBlank
#Entity
#Table(name="images")
data class Image (
#Id #GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0,
val alt: String = "",
#NotBlank
val src: String = "",
val tite: String = "",
/**
* Relations
*
*/
//#OneToOne(mappedBy="images")
#OneToOne(mappedBy = "images", cascade = [(CascadeType.ALL)], fetch= FetchType.EAGER)
#JsonManagedReference
val product: Product = Product()
)
package com.moises.kotlinstoreapi.model
import com.fasterxml.jackson.annotation.JsonManagedReference
import java.time.LocalDateTime
import javax.persistence.*
import javax.validation.constraints.NotBlank
import javax.validation.constraints.NotEmpty
import javax.validation.constraints.NotNull
#Entity
#Table(name="products")
data class Product (
#Id #GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0,
#NotBlank
val title: String = "",
#NotBlank
val description: String = "",
#NotEmpty
var price: Double = 999.99,
#NotNull
var created_at: LocalDateTime = LocalDateTime.now(),
var update_at: LocalDateTime? = null,
/**
* Relations
*
*/
//#OneToOne(mappedBy="products", cascade = arrayOf(CascadeType.ALL))
//#JoinColumn(name = "id")
#OneToOne(cascade = [(CascadeType.ALL)], fetch= FetchType.EAGER)
#JoinColumn(name = "image_id")
#JsonManagedReference
val image: Image? = null
//#OneToOne(mappedBy="products", orphanRemoval=true, cascade=arrayOf(CascadeType.PERSIST))
//private val image: Image?
//#OneToOne(cascade = arrayOf(CascadeType.ALL))
//#JoinColumn(name = "purchase_line_id")
//val purchaseLine: PurchaseLine ?= null
)
I am usin IntelliJ with Maven and OpenJDK 11 in Ubuntu.
My pow.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.moises</groupId>
<artifactId>kotlin-store-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>kotlin-store-api</name>
<description>API store with Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<kotlin.version>1.3.50</kotlin.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-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
<plugin>jpa</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-noarg</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
you had wrong mappedBy value in Image - mappedBy = "images" it should be "image":
#OneToOne(mappedBy = "image", cascade = [(CascadeType.ALL)], fetch= FetchType.EAGER)
#JsonManagedReference
val product: Product = Product()
because Product has field image:
#JsonManagedReference
val image: Image? = null

Resources