Skip to content

Commit 8e35496

Browse files
committed
Hibernate reactive example functional
1 parent a670b5b commit 8e35496

File tree

367 files changed

+27675
-169
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

367 files changed

+27675
-169
lines changed

querydsl-examples/querydsl-example-hibernate-reactive-quarkus/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
</dependencyManagement>
2525

2626
<dependencies>
27+
<dependency>
28+
<groupId>io.github.openfeign.querydsl</groupId>
29+
<artifactId>querydsl-hibernate-reactive</artifactId>
30+
<version>${project.version}</version>
31+
</dependency>
32+
2733
<dependency>
2834
<groupId>io.quarkus</groupId>
2935
<artifactId>quarkus-hibernate-reactive</artifactId>
@@ -59,11 +65,27 @@
5965
<groupId>org.apache.maven.plugins</groupId>
6066
<artifactId>maven-compiler-plugin</artifactId>
6167
<configuration>
68+
<annotationProcessorPaths>
69+
<annotationProcessorPath>
70+
<groupId>io.github.openfeign.querydsl</groupId>
71+
<artifactId>querydsl-apt</artifactId>
72+
<version>${project.version}</version>
73+
<classifier>jpa</classifier>
74+
</annotationProcessorPath>
75+
</annotationProcessorPaths>
6276

6377
<!-- the parameters=true option is critical so that RESTEasy works fine -->
6478
<parameters>true</parameters>
6579
</configuration>
80+
<dependencies>
81+
<dependency>
82+
<groupId>io.github.openfeign.querydsl</groupId>
83+
<artifactId>querydsl-apt</artifactId>
84+
<version>${project.version}</version>
85+
</dependency>
86+
</dependencies>
6687
</plugin>
88+
6789
<plugin>
6890
<!-- you need this specific version to integrate with the other build helpers -->
6991
<artifactId>maven-surefire-plugin</artifactId>

querydsl-examples/querydsl-example-hibernate-reactive-quarkus/src/main/java/com/querydsl/example/reactive/FruitsRoutes.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static io.quarkus.vertx.web.Route.HttpMethod.POST;
77
import static io.quarkus.vertx.web.Route.HttpMethod.PUT;
88

9+
import com.querydsl.jpa.hibernate.MutinyQueryFactory;
910
import io.quarkus.vertx.web.Body;
1011
import io.quarkus.vertx.web.Param;
1112
import io.quarkus.vertx.web.Route;
@@ -26,17 +27,25 @@ public class FruitsRoutes {
2627

2728
private static final Logger LOGGER = Logger.getLogger(FruitsRoutes.class.getName());
2829

29-
@Inject Mutiny.SessionFactory sf;
30+
private final Mutiny.SessionFactory sf;
31+
private final MutinyQueryFactory qf;
32+
33+
@Inject
34+
public FruitsRoutes(Mutiny.SessionFactory sf) {
35+
this.sf = sf;
36+
qf = new MutinyQueryFactory(sf);
37+
}
38+
39+
private static final QFruit f = new QFruit("f");
3040

3141
@Route(methods = GET, path = "/")
3242
public Uni<List<Fruit>> getAll() {
33-
return sf.withSession(
34-
session -> session.createNamedQuery(Fruit.FIND_ALL, Fruit.class).getResultList());
43+
return qf.withQuery(query -> query.select(f).from(f).orderBy(f.name.asc()).fetch());
3544
}
3645

3746
@Route(methods = GET, path = "/:id")
3847
public Uni<Fruit> getSingle(@Param Integer id) {
39-
return sf.withSession(session -> session.find(Fruit.class, id));
48+
return qf.withQuery(query -> query.select(f).from(f).where(f.id.eq(id)).fetchOne());
4049
}
4150

4251
@Route(methods = POST, path = "/")
@@ -56,32 +65,18 @@ public Uni<Fruit> update(@Body Fruit fruit, @Param Integer id) {
5665
return Uni.createFrom()
5766
.failure(new IllegalArgumentException("Fruit name was not set on request."));
5867
}
59-
return sf.withTransaction(
60-
(session, tx) ->
61-
session
62-
.find(Fruit.class, id)
63-
// If entity exists then update it
64-
.onItem()
65-
.ifNotNull()
66-
.invoke(entity -> entity.setName(fruit.getName())))
67-
// If entity not found, fail
68+
return qf.withUpdate(
69+
f, update -> update.where(f.id.eq(id)).set(f.name, fruit.getName()).execute())
6870
.onItem()
6971
.ifNull()
70-
.fail();
72+
.fail()
73+
.flatMap(a -> qf.withSelectFrom(f, select -> select.where(f.id.eq(id)).fetchOne()));
7174
}
7275

7376
@Route(methods = DELETE, path = "/:id")
74-
public Uni<Fruit> delete(@Param Integer id, HttpServerResponse response) {
75-
return sf.withTransaction(
76-
(session, tx) ->
77-
session
78-
.find(Fruit.class, id)
79-
// If entity exists then delete it
80-
.onItem()
81-
.ifNotNull()
82-
.call(
83-
entity -> session.remove(entity).invoke(() -> response.setStatusCode(204))))
84-
// If entity not found, fail
77+
public Uni<Integer> delete(@Param Integer id, HttpServerResponse response) {
78+
return qf.withDelete(f, delete -> delete.where(f.id.eq(id)).execute())
79+
.invoke(() -> response.setStatusCode(204))
8580
.onItem()
8681
.ifNull()
8782
.fail();

querydsl-libraries/querydsl-core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
<groupId>io.projectreactor</groupId>
2828
<artifactId>reactor-core</artifactId>
2929
</dependency>
30+
<dependency>
31+
<groupId>io.smallrye.reactive</groupId>
32+
<artifactId>mutiny</artifactId>
33+
<version>2.6.0</version>
34+
<optional>true</optional>
35+
</dependency>
3036

3137
<!-- alias dependencies -->
3238
<dependency>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.querydsl.core.dml.mutiny;
2+
3+
import io.smallrye.mutiny.Uni;
4+
5+
/**
6+
* Parent interface for DML clauses
7+
*
8+
* @param <C> concrete subtype
9+
*/
10+
public interface MutinyDMLClause<C extends MutinyDMLClause<C>> {
11+
12+
/**
13+
* Execute the clause and return the amount of affected rows
14+
*
15+
* @return amount of affected rows or empty if not available
16+
*/
17+
Uni<Integer> execute();
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.querydsl.core.dml.mutiny;
2+
3+
import com.querydsl.core.FilteredClause;
4+
5+
/**
6+
* {@code ReactiveDeleteClause} defines a generic interface for Delete clauses
7+
*
8+
* @param <C> concrete subtype
9+
*/
10+
public interface MutinyDeleteClause<C extends MutinyDeleteClause<C>>
11+
extends MutinyDMLClause<C>, FilteredClause<C> {}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.querydsl.core.dml.mutiny;
2+
3+
import com.querydsl.core.Query;
4+
import io.smallrye.mutiny.Uni;
5+
import java.util.List;
6+
7+
/**
8+
* {@code MutinyFetchable} defines default projection methods for {@link Query} implementations. All
9+
* Querydsl query implementations should implement this interface.
10+
*
11+
* @param <T> result type
12+
*/
13+
public interface MutinyFetchable<T> {
14+
15+
/**
16+
* Get the projection as a typed Flux.
17+
*
18+
* @return result
19+
*/
20+
Uni<List<T>> fetch();
21+
22+
/**
23+
* Get the first result of the projection.
24+
*
25+
* @return first result
26+
*/
27+
Uni<T> fetchFirst();
28+
29+
/**
30+
* Get the projection as a unique result.
31+
*
32+
* @return first result
33+
*/
34+
Uni<T> fetchOne();
35+
36+
/**
37+
* Get the count of matched elements
38+
*
39+
* @return row count
40+
*/
41+
Uni<Long> fetchCount();
42+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.querydsl.core.dml.mutiny;
2+
3+
import com.querydsl.core.ResultTransformer;
4+
import com.querydsl.core.SimpleQuery;
5+
import com.querydsl.core.Tuple;
6+
import com.querydsl.core.types.Expression;
7+
import org.reactivestreams.Publisher;
8+
9+
/**
10+
* {@code FetchableQuery} extends {@link MutinyFetchable} and {@link SimpleQuery} with projection
11+
* changing methods and result aggregation functionality using {@link ResultTransformer} instances.
12+
*
13+
* @param <T> element type
14+
* @param <Q> concrete subtype
15+
*/
16+
public interface MutinyFetchableQuery<T, Q extends MutinyFetchableQuery<T, Q>>
17+
extends SimpleQuery<Q>, MutinyFetchable<T> {
18+
19+
/**
20+
* Change the projection of this query
21+
*
22+
* @param <U>
23+
* @param expr new projection
24+
* @return the current object
25+
*/
26+
<U> MutinyFetchableQuery<U, ?> select(Expression<U> expr);
27+
28+
/**
29+
* Change the projection of this query
30+
*
31+
* @param exprs new projection
32+
* @return the current object
33+
*/
34+
MutinyFetchableQuery<Tuple, ?> select(Expression<?>... exprs);
35+
36+
/**
37+
* Apply the given transformer to this {@code ReactiveFetchableQuery} instance and return the
38+
* results
39+
*
40+
* @param <S>
41+
* @param transformer result transformer
42+
* @return transformed result
43+
*/
44+
<S> Publisher<S> transform(MutinyResultTransformer<S> transformer);
45+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.querydsl.core.dml.mutiny;
2+
3+
import com.querydsl.core.support.QueryBase;
4+
import com.querydsl.core.support.QueryMixin;
5+
import com.querydsl.core.types.SubQueryExpression;
6+
import org.reactivestreams.Publisher;
7+
8+
/**
9+
* {@code FetchableQueryBase} extends the {@link QueryBase} class to provide default implementations
10+
* of the methods of the {@link com.querydsl.core.dml.reactive.ReactiveFetchable} interface
11+
*
12+
* @param <T> result type
13+
* @param <Q> concrete subtype
14+
*/
15+
public abstract class MutinyFetchableQueryBase<T, Q extends MutinyFetchableQueryBase<T, Q>>
16+
extends QueryBase<Q> implements MutinyFetchable<T> {
17+
18+
public MutinyFetchableQueryBase(QueryMixin<Q> queryMixin) {
19+
super(queryMixin);
20+
}
21+
22+
public <T> Publisher<T> transform(MutinyResultTransformer<T> transformer) {
23+
return transformer.transform((MutinyFetchableQuery<?, ?>) this);
24+
}
25+
26+
@Override
27+
public final boolean equals(Object o) {
28+
if (o == this) {
29+
return true;
30+
} else if (o instanceof SubQueryExpression<?> s) {
31+
return s.getMetadata().equals(queryMixin.getMetadata());
32+
} else {
33+
return false;
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)