Spring Boot connecting to PostgreSQL without any issue but is not creating any tables - postgresql

I have a simple spring boot application with two entities, CustomUser and Role, the application connects to the PostgreSQL database without any issues but no tables are created.
application.yml:
server:
port: 8082
spring:
application:
name: user-service
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/sdm-user-db
username: root
password: password
# JPA properties
jpa:
hibernate:
ddl-auto: create
show-sql: true
database: postgresql
database-platform: org.hibernate.dialect.PostgreSQLDialect
open-in-view: false
generate-ddl: true
zipkin:
base-url: http://localhost:9411/
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
hostname: localhost
CustomUser:
#AllArgsConstructor
#NoArgsConstructor
#Getter
#Setter
#Entity
public class CustomUser implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(generator = "UUID")
#GenericGenerator(
name = "UUID",
strategy = "org.hibernate.id.UUIDGenerator"
)
#Column(name = "id", updatable = false, nullable = false, columnDefinition = "VARCHAR(36)")
#Type(type = "uuid-char")
private UUID id;
private String name;
#Column(unique=true)
private String email;
private String password;
private String hashKey;
#ManyToMany(fetch = FetchType.EAGER)
private Collection<Role> roles = new ArrayList<Role>();
}
Role:
#AllArgsConstructor
#NoArgsConstructor
#Getter
#Setter
#Entity
public class Role implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
private String name;
}
pom.xml database and jpa dependencies are included
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
The application runs without any issues so I have not much to debug with.
pg admin showing empty tables for the database
here I even try the connection with the database with the intelij plugin
successful connection test with intelij plugin
then I tried a simple commandLineRunner where I save one role into the database, it fails and here is the caused by part of the error log
Caused by: org.postgresql.util.PSQLException: ERROR: relation "role" does not exist
Position : 13
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2675) ~[postgresql-42.3.5.jar:42.3.5]
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2365) ~[postgresql-42.3.5.jar:42.3.5]
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:355) ~[postgresql-42.3.5.jar:42.3.5]
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:490) ~[postgresql-42.3.5.jar:42.3.5]
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:408) ~[postgresql-42.3.5.jar:42.3.5]
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:167) ~[postgresql-42.3.5.jar:42.3.5]
at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:135) ~[postgresql-42.3.5.jar:42.3.5]
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-4.0.3.jar:na]
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-4.0.3.jar:na]
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
... 67 common frames omitted

If you are using Hibernate, setting spring.jpa.hibernate.ddl-auto to update is likely to help.

Related

Micronaut: JPA batch update operations is not working for postgresql

I'm trying to run batch operations in my Micronaut app.
Currently, I can run insert operations in batch (with reWriteBatchedInserts option activated) but the configuration is not working for updates.
Something special about my app is that I want to set the id manually.
This is my entity:
#Data
#Entity
#SuperBuilder
#NoArgsConstructor
#Table(name = "PRODUCT")
public class ArticleItem implements Cloneable, Serializable {
#Id
#Column
private Long id;
The service:
#Singleton
#Slf4j
public class ArticleServiceImpl implements ArticleService {
#Transactional
public List<GenericSingleResponse> internalSaveArticles(
List<ArticleItem> articleItemList,
Integer erp, String traceId, String json
) {
articlePort.saveAll(insertList);
The port:
#Singleton
public class ArticlePortImpl implements ArticlePort {
private final EntityManager entityManager;
#Transactional
public void updateAll(List<ArticleItem> articleItemList) {
articleItemList.forEach(f -> entityManager.merge(f));
}
The configuration:
datasources:
default:
url: jdbc:postgresql://localhost:5432/postgres?reWriteBatchedInserts=true
jpa:
default:
properties:
hibernate:
hbm2ddl:
auto: none
jdbc:
batch_size: 100
batch_versioned_data: true
order_inserts: true
order_updates: true
batch_versioned_data: true
show_sql: false
id:
optimizer:
pooled:
preferred: pooled-lo

Micronaut: configure JPA batch operations

I'm trying to configure my Micronaut project to insert/update using batch operations (create one single insert with 50 items included instead of 50 different inserts).
I cannot find a more detailed manual so the configuration that I'm using is this one:
datasources:
default:
url: jdbc:postgresql:///postgres?cloudSqlInstance=uri&socketFactory=com.google.cloud.sql.postgres.SocketFactory&ipTypes=PRIVATE
username: ****
password: ****
driverClassName: org.postgresql.Driver
jpa:
default:
properties:
hibernate:
hbm2ddl:
auto: none
jdbc:
batch_size: 50
order_inserts: true
order_updates: true
batch_versioned_data: true
show_sql: false
This is the repo:
import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.GenericRepository;
#Repository
public interface ArticleExportPort extends GenericRepository<ArticleExport, Long> {
void insertMany(Iterable<ArticleExport> articleExports);
List<ArticleExport> findAll();
}
And this is the Entity:
#Data
#Entity
#Table(name = "PRODUCT_EXPORT")
public class ArticleExport {
#Id
private Long Id;
#NotNull
#Column(name = "PRODUCT_ID",nullable = false)
private Long productId;
}
The database is a PostgreSQL located in Google Cloud but also testing in local it doesn't work.
Any suggestion?
Thanks!

jsonb type data saving with postgres db and spring boot getting JpaSystemException after java 8 to java 11 migration

I'm trying to save jsonb type data to a Postgres db table using the following library
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.14</version>
</dependency>
In java 8 it worked without any issue, But due to requirement I had to migrate the service to java 11 but after the migration when I tried to save jsonb to the table I got the following error.
org.springframework.orm.jpa.JpaSystemException: java.lang.IllegalArgumentException: The given byte array cannot be transformed to Json object; nested exception is org.hibernate.HibernateException: java.lang.IllegalArgumentException: The given byte array cannot be transformed to Json object.
NOTE - Hibernate versions are the same in both java 8 and java 11
version: 5.4.20.Final in both
Following is the entity which trying to save
#Builder(toBuilder = true)
#Data
#NoArgsConstructor
#AllArgsConstructor
#Entity
#Table(name = "test")
#TypeDefs({
#TypeDef(name = "jsonb", typeClass = JsonBinaryType.class),
})
public class Test extends Auditable {
#Id
#Column(name = "id", updatable = false, nullable = false, unique = true)
private UUID id;
#Type(type = "jsonb")
#Column(name = "data", columnDefinition = "jsonb")
private RequestEventDto data;
}
following is the RequestEventDto
import lombok.Builder;
import lombok.Data;
import java.util.List;
import java.util.Map;
#Data
#Builder
public class RequestEventDto {
private String requestId;
#Builder.Default
private String applicationId = "program1";
private String entityType;
private List<Map<String, Object>> listEntities;
}
Can you help with this problem?
Issue fixed with adding the following annotations to the RequestEventDto
#NoArgsConstructor
#AllArgsConstructor
public class RequestEventDto {
It seems it is due to the constructor not there when serialization happening.

Micronaut - not possible to use PersistenceContext

I'am new to micronaut and I try to follow this little project. However I would like it to work with postgres.
My application.yml looks like this:
micronaut:
application:
name: hello-world
datasources:
default:
url: 'jdbc:postgresql://localhost:5432/test'
username: test
password: test
driver-class-name: org.postgresql.Driver
jpa:
default:
properties:
hibernate:
hbm2ddl:
auto: update
show_sql: true
I have access to the database via intellij.
In the pom.xml I have the following dependencies:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.8</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micronaut.configuration</groupId>
<artifactId>micronaut-jdbc-hikari</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micronaut.configuration</groupId>
<artifactId>micronaut-hibernate-jpa</artifactId>
</dependency>
Additionally it is mentioned in the link that one needs annotionProcessor, so I added this to my build profile:
<annotationProcessorPaths>
<path>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</path>
</annotationProcessorPaths>
So now everytime I try to do the following:
#PersistenceContext
private EntityManager entityManager;
I get the following error:
Caused by: io.micronaut.context.exceptions.NoSuchBeanException: No bean of type [javax.persistence.EntityManager] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
However I already have annotation processing enabled. And I also have a #Entity-class:
#Entity
#Table(name = "users")
public class User {
#NotBlank
#Column(name = "name")
private String userName;
public User() {
}
public User(#NotBlank final String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public void setUserName(final String userName) {
this.userName = userName;
}
}
What exactly am I missing in my setup?
There were multiple issues with my setup.
datasource and jpa need to be at root level
The #Entity needs a #Id
So in the end the application.yml looks like this:
micronaut:
application:
name: hello-world
datasources:
default:
url: 'jdbc:postgresql://localhost:5432/test'
username: test
password: test
driver-class-name: org.postgresql.Driver
jpa:
default:
packages-to-scan:
- 'test'
properties:
hibernate:
hbm2ddl:
auto: update
show_sql: true
And the #Entity-Class needs an #Id-Attribute:
#Entity
#Table(name = "users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#NotBlank
#Column(name = "user_name")
private String userName;
public User() {
}
public User(#NotBlank final String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public void setUserName(final String userName) {
this.userName = userName;
}
#Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
'}';
}
}

Hibernate Postgres Slow Insert

I try to insert data into Postgres using JPA/Hibernate/Postgres.
The data is parsed from a CSV File and then should be saved into a postgres database. The code that persists the data looks as follows:
#Autowired
KundeRepository repo;
#Transactional
public void safe(Kunde kd) {
repo.save(kd);
}
public void safeAll(Iterable<Kunde> kt) {
repo.save(kt);
repo.flush();
}
The entity look as follows
public class account implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
}
#ManyToOne
Kunde kunde;
#OneToMany
List<TransaktionsGruppe> gruppen;
#Entity
#Table(name = "kunde")
#NoArgsConstructor
public class Kunde implements Serializable {
public static final String kundennummerKey = "KUNDENNUMMER";
private static final long serialVersionUID = 1L;
#Id
#Getter
#Setter
private String id;
#OneToMany
List<Account> accounts;
#Entity
#Table(name = "transaktionsgruppe")
public class Transaktionsgruppe implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#ManyToOne
Account acc;
private String bezeichnung;
When I now pass a Collection to the safeAll method the inserts are really slow. Especially it seems to call the hibernate sequence for every insert. Is there a way to speed things up?
My configuration looks as follows:
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: dburl
username: user
password: pw
hikari:
validation-timeout: 10000
health-check-properties: {"connectivityCheckTimeoutMs","1000"}
jpa:
show-sql: true
properties:
hibernate.cache.use_second_level_cache: false
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: false
hibernate.jdbc.batch.size: 100
hibernate.order_inserts: true+
Currently show-sql is enabled. It is about 60000 Entities overall and it takes more than 20 Minutes. the entitties are rather small in size
Your safeAll() method is not annotated with #Transactional so Spring opens and closes a transaction for each item of your list. By annotating it, Spring will open and close one single transaction for the whole list.