Skip to content

#1673 custom one to one store procedure test #1

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Hibernate Reactive has been tested with:
- CockroachDB 22.1
- MS SQL Server 2019
- Oracle 21.3
- [Hibernate ORM][] 6.2.4.Final
- [Hibernate ORM][] 6.2.5.Final
- [Vert.x Reactive PostgreSQL Client](https://vertx.io/docs/vertx-pg-client/java/) 4.4.3
- [Vert.x Reactive MySQL Client](https://vertx.io/docs/vertx-mysql-client/java/) 4.4.3
- [Vert.x Reactive Db2 Client](https://vertx.io/docs/vertx-db2-client/java/) 4.4.3
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
id 'java-library'
id 'maven-publish'
id 'com.diffplug.spotless' version '6.18.0'
id 'nu.studer.credentials' version '2.1'
id 'nu.studer.credentials' version '2.2'
id 'org.asciidoctor.jvm.convert' version '3.3.2' apply false
id 'io.github.gradle-nexus.publish-plugin' version '1.3.0'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
import org.hibernate.event.spi.FlushEvent;
import org.hibernate.event.spi.FlushEventListener;
import org.hibernate.reactive.event.ReactiveFlushEventListener;
import org.hibernate.reactive.logging.impl.Log;
import org.hibernate.reactive.session.ReactiveSession;
import org.hibernate.stat.spi.StatisticsImplementor;

import static java.lang.invoke.MethodHandles.lookup;
import static org.hibernate.reactive.logging.impl.LoggerFactory.make;
import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture;

/**
Expand All @@ -24,6 +27,8 @@
public class DefaultReactiveFlushEventListener extends AbstractReactiveFlushingEventListener
implements ReactiveFlushEventListener, FlushEventListener {

private static final Log LOG = make( Log.class, lookup() );

@Override
public CompletionStage<Void> reactiveOnFlush(FlushEvent event) throws HibernateException {
final EventSource source = event.getSession();
Expand Down Expand Up @@ -59,6 +64,6 @@ else if ( ((ReactiveSession) source).getReactiveActionQueue().hasAnyQueuedAction

@Override
public void onFlush(FlushEvent event) throws HibernateException {
throw new UnsupportedOperationException();
throw LOG.nonReactiveMethodCall( "reactiveOnFlush" );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.reactive;

import io.vertx.junit5.Timeout;
import io.vertx.junit5.VertxTestContext;
import jakarta.persistence.*;

import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.reactive.testing.DBSelectionExtension;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

import static java.util.concurrent.TimeUnit.*;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.*;
import static org.hibernate.reactive.testing.DBSelectionExtension.*;
import static org.junit.jupiter.api.Assertions.*;

@Timeout(value = 10, timeUnit = MINUTES)

public class CustomOneToOneStoredProcedureSqlTest extends BaseReactiveTest {

@RegisterExtension
public DBSelectionExtension dbSelection = runOnlyFor( POSTGRESQL );
private IndividualPerson individualPerson;
private DriverLicence driverLicence;
private static final String INITIAL_LICENCE_NO = "12545KLI12";
private static final String INSERT_DRIVER_LICENCE_SQL = "CREATE OR REPLACE FUNCTION PROC_INSERT_INDIVIDUAL_PERSON_DRIVER_LICENCE995 ( " +
" ID_PARAM IN bigint, " +
" TEXT_PARAM IN varchar(255), " +
" ID_PERSON_PARAM IN bigint " +
" ) " +
" RETURNS void AS " +
"$BODY$ " +
" BEGIN " +
" insert into DRIVER_LICENCE (individual_person_Id, id, licenceNo, updated) values (ID_PERSON_PARAM, ID_PARAM, TEXT_PARAM, localtimestamp); " +
" END; " +
"$BODY$ " +
"LANGUAGE plpgsql;";

private static final String DELETE_DRIVER_LICENCE_SQL = "CREATE OR REPLACE FUNCTION PROC_DELETE_INDIVIDUAL_PERSON_DRIVER_LICENCE928 ( " +
" ID_PARAM IN bigint" +
" ) RETURNS void AS " +
"$BODY$ " +
" BEGIN " +
" update DRIVER_LICENCE set deleted=localtimestamp where id=ID_PARAM; " +
" END; " +
"$BODY$ " +
"LANGUAGE plpgsql;";


@Override
protected Collection<Class<?>> annotatedEntities() {
return List.of( IndividualPerson.class, DriverLicence.class );
}

@BeforeEach
public void populateDb(VertxTestContext context) {
individualPerson = new IndividualPerson();
individualPerson.name = "Doruk";

driverLicence = new DriverLicence();
driverLicence.licenceNo = INITIAL_LICENCE_NO;
driverLicence.individualPerson = individualPerson;

test( context, openSession()
.thenCompose( s -> s
.createNativeQuery( INSERT_DRIVER_LICENCE_SQL ).executeUpdate()
.thenCompose( v -> s.createNativeQuery( DELETE_DRIVER_LICENCE_SQL ).executeUpdate() )
.thenCompose( v -> s.persist( individualPerson, driverLicence ) )
.thenCompose( v -> s.flush() )
)
);
}

@Test
public void testInsertStoredProcedureDriverLicence(VertxTestContext context) {
test( context, openSession().thenCompose( session -> session
.find( DriverLicence.class, driverLicence.id )
.thenAccept( Assertions::assertNotNull ) )
);
}


@Test
public void testDeleteStoredProcedure(VertxTestContext context) {
test( context, openSession()
.thenCompose( session -> session
.find( DriverLicence.class, driverLicence.id )
.thenCompose( session::remove )
.thenCompose( v -> session.flush() ) )
.thenCompose( v -> openSession() )
.thenCompose( session -> session.find( DriverLicence.class, driverLicence.id ) )
.thenAccept( foundRecord -> {
assertEquals( INITIAL_LICENCE_NO, foundRecord.licenceNo );
assertNotNull( foundRecord.deleted );
assertNotNull( foundRecord.updated );

} )
);
}

@Entity
@Table(name = "DRIVER_LICENCE")
@SQLInsert(sql = "SELECT PROC_INSERT_INDIVIDUAL_PERSON_DRIVER_LICENCE995( $1, $2, $3 );", callable = true)
@SQLDelete(sql = "SELECT PROC_DELETE_INDIVIDUAL_PERSON_DRIVER_LICENCE928( $1 );", callable = true)
public static class DriverLicence {
@GeneratedValue
@Id
long id;

@Basic(optional = false)
String licenceNo;

@OneToOne
@JoinColumn(name = "individual_person_Id")
IndividualPerson individualPerson;

@Column(insertable = false, updatable = false, nullable = false)
LocalDateTime updated;

@Column(insertable = false, updatable = false)
LocalDateTime deleted;

}

@Entity(name = "INDIVIDUAL_PERSON")
@Table(name = "INDIVIDUAL_PERSON")
public static class IndividualPerson {
@GeneratedValue
@Id
long id;

@Basic(optional = false)
String name;

}

}
Loading