Skip to content
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

Added support for deserializing object property values based on value class #510

Merged
merged 9 commits into from
Aug 2, 2024
Merged
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 .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ jobs:
- run:
name: 'Test'
command: |
./mvnw -ntp -B install -Pquickbuild -pl :querydsl-sql-spring,:querydsl-jpa-spring,:querydsl-kotlin-codegen,:querydsl-mongodb,:querydsl-r2dbc -am -T2
./mvnw -ntp -B install -Pquickbuild -pl :querydsl-sql-spring,:querydsl-jpa-spring,:querydsl-kotlin-codegen,:querydsl-mongodb,:querydsl-r2dbc,:querydsl-sql-json -am -T2
./mvnw -ntp -B verify -Pexamples -rf :querydsl-examples
- save-test-results
buildQuarkusExample:
Expand Down
4 changes: 4 additions & 0 deletions querydsl-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<packaging>pom</packaging>
<name>Querydsl - Examples</name>

<properties>
<main.java.version>17</main.java.version>
</properties>

<modules>
<module>querydsl-example-sql-guice</module>
<module>querydsl-example-sql-spring</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public Response toResponse(Exception exception) {
LOGGER.error("Failed to handle request", exception);

int code = 500;
if (exception instanceof WebApplicationException) {
code = ((WebApplicationException) exception).getResponse().getStatus();
if (exception instanceof WebApplicationException applicationException) {
code = applicationException.getResponse().getStatus();
}

ObjectNode exceptionJson = objectMapper.createObjectNode();
Expand Down
5 changes: 0 additions & 5 deletions querydsl-examples/querydsl-example-jpa-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
<artifactId>querydsl-example-jpa-spring</artifactId>
<name>Querydsl example - JPA Spring</name>

<properties>
<!-- to match spring JDK version -->
<main.java.version>17</main.java.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Customer(String firstName, String lastName) {

@Override
public String toString() {
return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
return "Customer[id=%d, firstName='%s', lastName='%s']".formatted(id, firstName, lastName);
}

public Long getId() {
Expand Down
9 changes: 0 additions & 9 deletions querydsl-examples/querydsl-example-r2dbc-sql-codegen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
<artifactId>querydsl-example-r2dbc-sql-codegen</artifactId>
<name>Querydsl example - R2DBC SQL Codegen</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- to match spring JDK version -->
<main.java.version>17</main.java.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
Expand All @@ -31,9 +24,7 @@
</dependencyManagement>

<dependencies>

<!-- spring -->

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
Expand Down
21 changes: 12 additions & 9 deletions querydsl-examples/querydsl-example-sql-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,8 @@
<artifactId>querydsl-example-sql-spring</artifactId>
<name>Querydsl example - SQL Spring</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- to match spring JDK version -->
<main.java.version>17</main.java.version>
</properties>

<dependencies>

<!-- spring -->

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
Expand Down Expand Up @@ -51,6 +42,11 @@
<artifactId>querydsl-sql</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.querydsl</groupId>
<artifactId>querydsl-sql-json</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.querydsl</groupId>
<artifactId>querydsl-sql-spring</artifactId>
Expand Down Expand Up @@ -90,6 +86,13 @@
<customType>com.querydsl.sql.types.LocalDateTimeType</customType>
<customType>com.querydsl.sql.types.LocalDateType</customType>
</customTypes>
<typeMappings>
<typeMapping>
<table>CUSTOMER_ADDRESS</table>
<column>ADDRESS</column>
<type>com.querydsl.example.dto.Address</type>
</typeMapping>
</typeMappings>
<packageName>com.querydsl.example.sql</packageName>
<targetFolder>${project.basedir}/target/generated-sources/java</targetFolder>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.querydsl.sql.H2Templates;
import com.querydsl.sql.SQLQueryFactory;
import com.querydsl.sql.SQLTemplates;
import com.querydsl.sql.spring.SpringConnectionProvider;
import com.querydsl.sql.spring.SpringExceptionTranslator;
import com.querydsl.sql.types.LocalDateTimeType;
import com.querydsl.sql.types.LocalDateType;
import io.github.openfeign.querydsl.sql.json.types.JSONType;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
Expand All @@ -25,7 +25,7 @@ public class JdbcConfiguration {

@Bean
public DataSource dataSource() {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
var dataSource = new SimpleDriverDataSource();
Class driver;
try {
driver = Class.forName(env.getRequiredProperty("jdbc.driver"));
Expand All @@ -46,17 +46,18 @@ public PlatformTransactionManager transactionManager() {

@Bean
public com.querydsl.sql.Configuration querydslConfiguration() {
SQLTemplates templates = H2Templates.builder().build();
com.querydsl.sql.Configuration configuration = new com.querydsl.sql.Configuration(templates);
var templates = H2Templates.builder().build();
var configuration = new com.querydsl.sql.Configuration(templates);
configuration.setExceptionTranslator(new SpringExceptionTranslator());
configuration.register(new LocalDateTimeType());
configuration.register(new LocalDateType());
configuration.register(new JSONType());
return configuration;
}

@Bean
public SQLQueryFactory queryFactory() {
SpringConnectionProvider provider = new SpringConnectionProvider(dataSource());
var provider = new SpringConnectionProvider(dataSource());
return new SQLQueryFactory(querydslConfiguration(), provider);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.querydsl.example.dao;

import static com.querydsl.core.types.Projections.bean;
import static com.querydsl.example.sql.QAddress.address;
import static com.querydsl.example.sql.QCustomer.customer;
import static com.querydsl.example.sql.QCustomerAddress.customerAddress;
import static com.querydsl.example.sql.QPerson.person;

import com.querydsl.core.group.GroupBy;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.QBean;
import com.querydsl.example.dto.Address;
import com.querydsl.example.dto.Customer;
import com.querydsl.example.dto.CustomerAddress;
import com.querydsl.example.dto.Person;
import com.querydsl.sql.SQLQueryFactory;
import com.querydsl.sql.dml.SQLInsertClause;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -30,7 +27,7 @@ public class CustomerDaoImpl implements CustomerDao {
customerAddress.addressTypeCode,
customerAddress.fromDate,
customerAddress.toDate,
bean(Address.class, address.all()).as("address"));
customerAddress.address);

final QBean<Customer> customerBean =
bean(
Expand All @@ -42,7 +39,7 @@ public class CustomerDaoImpl implements CustomerDao {

@Override
public Customer findById(long id) {
List<Customer> customers = findAll(customer.id.eq(id));
var customers = findAll(customer.id.eq(id));
return customers.isEmpty() ? null : customers.get(0);
}

Expand All @@ -52,14 +49,13 @@ public List<Customer> findAll(Predicate... where) {
.from(customer)
.leftJoin(customer.contactPersonFk, person)
.leftJoin(customer._customer3Fk, customerAddress)
.leftJoin(customerAddress.addressFk, address)
.where(where)
.transform(GroupBy.groupBy(customer.id).list(customerBean));
}

@Override
public Customer save(Customer c) {
Long id = c.getId();
var id = c.getId();

if (id == null) {
id =
Expand All @@ -81,16 +77,11 @@ public Customer save(Customer c) {
queryFactory.delete(customerAddress).where(customerAddress.customerId.eq(id)).execute();
}

SQLInsertClause insert = queryFactory.insert(customerAddress);
var insert = queryFactory.insert(customerAddress);
for (CustomerAddress ca : c.getAddresses()) {
if (ca.getAddress().getId() == null) {
ca.getAddress()
.setId(
queryFactory.insert(address).populate(ca.getAddress()).executeWithKey(address.id));
}
insert
.set(customerAddress.customerId, id)
.set(customerAddress.addressId, ca.getAddress().getId())
.set(customerAddress.address, ca.getAddress())
.set(customerAddress.addressTypeCode, ca.getAddressTypeCode())
.set(customerAddress.fromDate, ca.getFromDate())
.set(customerAddress.toDate, ca.getToDate())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.querydsl.example.dto;

import io.github.openfeign.querydsl.sql.json.*;
import lombok.Data;

@Data
public class Address {
public class Address implements JsonEntity {

private Long id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,12 @@ create table customer_order_delivery (
constraint order2_fk foreign key (order_id) references customer_order(id)
);

create table address (
id identity primary key,
street varchar(64),
zip varchar(64),
town varchar(64),
state varchar(64),
country varchar(3),
other_details varchar(64)
);

create table customer_address (
customer_id long,
address_id long,
address clob,
from_date date,
to_date date,
address_type_code varchar(12),

constraint customer3_fk foreign key (customer_id) references customer(id),
constraint address_fk foreign key (address_id) references address(id)
constraint customer3_fk foreign key (customer_id) references customer(id)
);
1 change: 1 addition & 0 deletions querydsl-libraries/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<module>querydsl-sql</module>
<module>querydsl-sql-spatial</module>
<module>querydsl-sql-spring</module>
<module>querydsl-sql-json</module>
<!-- Reactive SQL -->
<module>querydsl-r2dbc</module>

Expand Down
Loading
Loading