Commit edecbb9c authored by Norbert Komiňák's avatar Norbert Komiňák
Browse files

Updated queryDSL to 5.0.0

parent d3292320
......@@ -11,6 +11,10 @@
<artifactId>flea-market-manager-persistence</artifactId>
<properties>
<querydsl.version>5.0.0</querydsl.version>
</properties>
<dependencies>
<!-- Persistence -->
<dependency>
......@@ -29,14 +33,16 @@
<!-- QueryDSL -->
<dependency>
<groupId>com.mysema.querydsl</groupId>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>3.7.4</version>
<version>${querydsl.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>3.7.4</version>
<version>${querydsl.version}</version>
</dependency>
</dependencies>
......@@ -51,8 +57,9 @@
<goals>
<goal>process</goal>
</goals>
<configuration><outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
......
package cz.fi.muni.pa165.flea.market.manager;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
......@@ -10,6 +11,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
@SpringBootApplication
......@@ -42,4 +44,9 @@ public class PersistenceConfig {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder.setType(EmbeddedDatabaseType.H2).build();
}
@Bean
public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) {
return new JPAQueryFactory(entityManager);
}
}
package cz.fi.muni.pa165.flea.market.manager.dao;
import com.mysema.query.jpa.impl.JPAQuery;
import cz.fi.muni.pa165.flea.market.manager.dao.domain.DomainDaoImpl;
import cz.fi.muni.pa165.flea.market.manager.dao.exception.DaoException;
import cz.fi.muni.pa165.flea.market.manager.entity.MarketEvent;
......@@ -24,11 +23,11 @@ public class MarketEventDao extends DomainDaoImpl<MarketEvent, QMarketEvent> {
public List<MarketEvent> findAllByMarketLocation(@NonNull String marketLocationId) {
try {
JPAQuery query = new JPAQuery(entityManager);
return query
.from(qEntity)
return queryFactory
.selectFrom(qEntity)
.where(qEntity.location.id.eq(marketLocationId))
.list(qEntity);
.fetch();
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
......
package cz.fi.muni.pa165.flea.market.manager.dao;
import com.mysema.query.jpa.impl.JPAQuery;
import cz.fi.muni.pa165.flea.market.manager.dao.domain.DomainDaoImpl;
import cz.fi.muni.pa165.flea.market.manager.dao.exception.DaoException;
import cz.fi.muni.pa165.flea.market.manager.entity.MarketLocation;
......@@ -22,11 +21,10 @@ public class MarketLocationDao extends DomainDaoImpl<MarketLocation, QMarketLoca
public MarketLocation findByAddress(@NonNull String address) {
try {
JPAQuery query = new JPAQuery(entityManager);
return query
.from(qEntity)
return queryFactory
.selectFrom(qEntity)
.where(qEntity.address.eq(address))
.singleResult(qEntity);
.fetchFirst();
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
......
package cz.fi.muni.pa165.flea.market.manager.dao;
import com.mysema.query.jpa.impl.JPAQuery;
import cz.fi.muni.pa165.flea.market.manager.dao.domain.DomainDaoImpl;
import cz.fi.muni.pa165.flea.market.manager.dao.exception.DaoException;
import cz.fi.muni.pa165.flea.market.manager.entity.QStand;
......@@ -24,11 +23,10 @@ public class StandDao extends DomainDaoImpl<Stand, QStand> {
public List<Stand> findAllBySellerId(@NonNull String sellerId) {
try {
JPAQuery query = new JPAQuery(entityManager);
return query
.from(qEntity)
return queryFactory
.selectFrom(qEntity)
.where(qEntity.seller.id.eq(sellerId))
.list(qEntity);
.fetch();
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
......@@ -36,11 +34,10 @@ public class StandDao extends DomainDaoImpl<Stand, QStand> {
public List<Stand> findAllByEventId(@NonNull String eventId) {
try {
JPAQuery query = new JPAQuery(entityManager);
return query
.from(qEntity)
return queryFactory
.selectFrom(qEntity)
.where(qEntity.event.id.eq(eventId))
.list(qEntity);
.fetch();
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
......@@ -48,12 +45,10 @@ public class StandDao extends DomainDaoImpl<Stand, QStand> {
public List<Stand> findAllByStandType(@NonNull String standTypeId) {
try {
JPAQuery query = new JPAQuery(entityManager);
return query
.from(qEntity)
return queryFactory
.selectFrom(qEntity)
.where(qEntity.standType.id.eq(standTypeId))
.list(qEntity);
.fetch();
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
......
package cz.fi.muni.pa165.flea.market.manager.dao;
import com.mysema.query.jpa.impl.JPAQuery;
import cz.fi.muni.pa165.flea.market.manager.dao.domain.DomainDaoImpl;
import cz.fi.muni.pa165.flea.market.manager.dao.exception.DaoException;
import cz.fi.muni.pa165.flea.market.manager.entity.QUser;
......@@ -21,11 +20,11 @@ public class UserDao extends DomainDaoImpl<User, QUser> {
public User findByEmail(@NonNull String email) {
try {
JPAQuery query = new JPAQuery(entityManager);
return query
.from(qEntity)
.where(qEntity.email.eq(email))
.singleResult(qEntity);
QUser user = QUser.user;
return queryFactory
.selectFrom(user)
.where(user.email.eq(email))
.fetchOne();
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
......
package cz.fi.muni.pa165.flea.market.manager.dao.domain;
import com.mysema.query.jpa.impl.JPAQuery;
import com.mysema.query.types.path.EntityPathBase;
import com.querydsl.core.types.dsl.EntityPathBase;
import com.querydsl.jpa.impl.JPAQueryFactory;
import cz.fi.muni.pa165.flea.market.manager.dao.exception.DaoException;
import cz.fi.muni.pa165.flea.market.manager.entity.domain.DomainEntity;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.NoRepositoryBean;
import javax.persistence.EntityManager;
......@@ -24,6 +25,9 @@ public abstract class DomainDaoImpl<ENTITY extends DomainEntity, Q_ENTITY extend
@PersistenceContext
protected EntityManager entityManager;
@Autowired
public JPAQueryFactory queryFactory;
public DomainDaoImpl(Class<ENTITY> entityClass, Q_ENTITY qEntity) {
this.entityClass = entityClass;
this.qEntity = qEntity;
......@@ -51,8 +55,7 @@ public abstract class DomainDaoImpl<ENTITY extends DomainEntity, Q_ENTITY extend
@Override
public List<ENTITY> findAll() {
try {
JPAQuery query = new JPAQuery(entityManager);
return query.from(qEntity).list(qEntity);
return queryFactory.selectFrom(qEntity).fetch();
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
......
package cz.fi.muni.pa165.flea.market.manager;
import com.github.javafaker.Faker;
import com.mysema.query.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import cz.fi.muni.pa165.flea.market.manager.dao.MarketEventDao;
import cz.fi.muni.pa165.flea.market.manager.entity.MarketEvent;
import cz.fi.muni.pa165.flea.market.manager.entity.MarketLocation;
......@@ -38,13 +38,13 @@ public class MarketEventDaoTest {
private final Faker faker = new Faker();
private final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private JPAQuery query;
private JPAQueryFactory queryFactory;
private MarketEvent marketEvent;
@BeforeEach
public void initialize() {
initializeData();
query = new JPAQuery(entityManager);
queryFactory = new JPAQueryFactory(entityManager);
}
@AfterEach
......@@ -74,9 +74,9 @@ public class MarketEventDaoTest {
marketEventDao.create(event);
List<MarketEvent> allEvents = query
.from(QMarketEvent.marketEvent)
.list(QMarketEvent.marketEvent);
List<MarketEvent> allEvents = queryFactory
.selectFrom(QMarketEvent.marketEvent)
.fetch();
Assertions.assertEquals(allEvents.size(), 2);
......@@ -122,7 +122,7 @@ public class MarketEventDaoTest {
marketEvent.setName(faker.artist().name());
marketEventDao.update(marketEvent);
List<MarketEvent> allEvents = query.from(QMarketEvent.marketEvent).list(QMarketEvent.marketEvent);
List<MarketEvent> allEvents = queryFactory.selectFrom(QMarketEvent.marketEvent).fetch();
Assertions.assertEquals(allEvents.size(), 1);
assertEqualsByFields(marketEvent, allEvents.get(0));
......@@ -139,7 +139,7 @@ public class MarketEventDaoTest {
public void delete() {
marketEventDao.delete(marketEvent);
List<MarketEvent> allEvents = query.from(QMarketEvent.marketEvent).list(QMarketEvent.marketEvent);
List<MarketEvent> allEvents = queryFactory.selectFrom(QMarketEvent.marketEvent).fetch();
Assertions.assertEquals(allEvents.size(), 0);
}
......
package cz.fi.muni.pa165.flea.market.manager;
import com.github.javafaker.Faker;
import com.mysema.query.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import cz.fi.muni.pa165.flea.market.manager.dao.MarketLocationDao;
import cz.fi.muni.pa165.flea.market.manager.entity.MarketEvent;
import cz.fi.muni.pa165.flea.market.manager.entity.MarketLocation;
......@@ -38,13 +38,13 @@ public class MarketLocationDaoTest {
private final Faker faker = new Faker();
private final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private JPAQuery query;
private JPAQueryFactory queryFactory;
private MarketLocation marketLocation;
@BeforeEach
public void initialize() {
initializeData();
query = new JPAQuery(entityManager);
queryFactory = new JPAQueryFactory(entityManager);
}
@AfterEach
......@@ -74,7 +74,7 @@ public class MarketLocationDaoTest {
marketLocationDao.create(location);
List<MarketLocation> allLocations = query.from(QMarketLocation.marketLocation).list(QMarketLocation.marketLocation);
List<MarketLocation> allLocations = queryFactory.selectFrom(QMarketLocation.marketLocation).fetch();
Assertions.assertEquals(allLocations.size(), 2);
Optional<MarketLocation> result = allLocations
......@@ -121,7 +121,7 @@ public class MarketLocationDaoTest {
marketLocation.setName(faker.animal().name());
marketLocationDao.update(marketLocation);
List<MarketLocation> allLocations = query.from(QMarketLocation.marketLocation).list(QMarketLocation.marketLocation);
List<MarketLocation> allLocations = queryFactory.selectFrom(QMarketLocation.marketLocation).fetch();
Assertions.assertEquals(allLocations.size(), 1);
SoftAssertions.assertSoftly(softAssertions -> softAssertions.assertThat(marketLocation).isEqualTo( allLocations.get(0)).usingRecursiveComparison()); }
......@@ -137,7 +137,7 @@ public class MarketLocationDaoTest {
public void delete() {
marketLocationDao.delete(marketLocation);
List<MarketLocation> allLocations = query.from(QMarketLocation.marketLocation).list(QMarketLocation.marketLocation);
List<MarketLocation> allLocations = queryFactory.selectFrom(QMarketLocation.marketLocation).fetch();
Assertions.assertEquals(allLocations.size(), 0);
}
......
package cz.fi.muni.pa165.flea.market.manager;
import com.github.javafaker.Faker;
import com.mysema.query.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import cz.fi.muni.pa165.flea.market.manager.dao.StandDao;
import cz.fi.muni.pa165.flea.market.manager.entity.MarketEvent;
import cz.fi.muni.pa165.flea.market.manager.entity.Stand;
......@@ -103,25 +103,25 @@ class StandDaoTest {
@Transactional
public void findAll() {
List<Stand> foundExamples = standDao.findAll();
Assertions.assertEquals(jpaQuery().from(stand).count(), foundExamples.size());
Assertions.assertEquals(jpaQueryFactory().selectFrom(stand).stream().count(), foundExamples.size());
initDatabase();
foundExamples = standDao.findAll();
Assertions.assertEquals(jpaQuery().from(stand).count(), foundExamples.size());
Assertions.assertEquals(jpaQueryFactory().from(stand).stream().count(), foundExamples.size());
}
@Test
@Transactional
public void create() {
Assertions.assertEquals(0, jpaQuery().from(stand).count());
Assertions.assertEquals(0, jpaQueryFactory().from(stand).stream().count());
Stand exampleStand = generateRandomStand();
standDao.create(exampleStand);
SoftAssertions.assertSoftly(softAssertions -> softAssertions.assertThat(exampleStand).isEqualTo(jpaQuery().from(stand).singleResult(stand)).usingRecursiveComparison());
SoftAssertions.assertSoftly(softAssertions -> softAssertions.assertThat(exampleStand).isEqualTo(jpaQueryFactory().selectFrom(stand).fetchFirst()).usingRecursiveComparison());
}
@Test
......@@ -146,7 +146,7 @@ class StandDaoTest {
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(exampleStandSellerStands.size()).isEqualTo(3);
softAssertions.assertThat(exampleStandSellerStands).isEqualTo(jpaQuery().from(stand).where(stand.seller.id.eq(exampleStandSeller.getId())).list(stand)).usingRecursiveComparison();
softAssertions.assertThat(exampleStandSellerStands).isEqualTo(jpaQueryFactory().selectFrom(stand).where(stand.seller.id.eq(exampleStandSeller.getId())).fetch()).usingRecursiveComparison();
});
}
......@@ -186,7 +186,7 @@ class StandDaoTest {
exampleStand.setSeller(createUser());
exampleStand = standDao.update(exampleStand);
Stand updatedStand = jpaQuery().from(stand).where(stand.id.eq(exampleStand.getId())).singleResult(stand);
Stand updatedStand = jpaQueryFactory().selectFrom(stand).where(stand.id.eq(exampleStand.getId())).fetchFirst();
Stand finalExampleStand = exampleStand;
SoftAssertions.assertSoftly(softAssertions -> softAssertions.assertThat(finalExampleStand).isEqualTo(updatedStand).usingRecursiveComparison());
......@@ -208,7 +208,7 @@ class StandDaoTest {
standDao.delete(exampleStand);
Assertions.assertNull(jpaQuery().from(stand).where(stand.id.eq(exampleStand.getId())).singleResult(stand));
Assertions.assertNull(jpaQueryFactory().selectFrom(stand).where(stand.id.eq(exampleStand.getId())).fetchFirst());
}
@Test
......@@ -226,7 +226,7 @@ class StandDaoTest {
entityManager.persist(exampleStand);
String exampleStandSellerId = exampleStand.getSeller().getId();
List<Stand> sellerStands = jpaQuery().from(stand).where(stand.seller.id.eq(exampleStandSellerId)).list(stand);
List<Stand> sellerStands = jpaQueryFactory().selectFrom(stand).where(stand.seller.id.eq(exampleStandSellerId)).fetch();
SoftAssertions.assertSoftly(softAssertions -> softAssertions.assertThat(sellerStands).isEqualTo(standDao.findAllBySellerId(exampleStandSellerId)).usingRecursiveComparison());
}
......@@ -241,12 +241,12 @@ class StandDaoTest {
String standTypeId = expectedStand.getStandType().getId();
List<Stand> foundStands = jpaQuery().from(stand).where(stand.standType.id.eq(standTypeId)).list(stand);
List<Stand> foundStands = jpaQueryFactory().selectFrom(stand).where(stand.standType.id.eq(standTypeId)).fetch();
SoftAssertions.assertSoftly(softAssertions -> softAssertions.assertThat(foundStands).isEqualTo(standDao.findAllByStandType(standTypeId)).usingRecursiveComparison());
}
private JPAQuery jpaQuery() {
return new JPAQuery(entityManager);
private JPAQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(entityManager);
}
}
package cz.fi.muni.pa165.flea.market.manager;
import com.github.javafaker.Faker;
import com.mysema.query.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import cz.fi.muni.pa165.flea.market.manager.dao.StandTypeDao;
import static cz.fi.muni.pa165.flea.market.manager.entity.QStandType.standType;
import cz.fi.muni.pa165.flea.market.manager.entity.StandType;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Assertions;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;
......@@ -34,6 +36,13 @@ class StandTypeDaoTest {
@PersistenceContext
private EntityManager entityManager;
private JPAQueryFactory queryFactory;
@BeforeEach
public void init() {
queryFactory = new JPAQueryFactory(entityManager);
}
public void initDatabase() {
List.of(generateRandomStandType(), generateRandomStandType(), generateRandomStandType()).forEach(entityManager::persist);
}
......@@ -52,25 +61,25 @@ class StandTypeDaoTest {
@Transactional
public void findAll() {
List<StandType> foundExamples = standTypeDao.findAll();
Assertions.assertEquals(jpaQuery().from(standType).count(), foundExamples.size());
Assertions.assertEquals(queryFactory.selectFrom(standType).stream().count(), foundExamples.size());
initDatabase();
foundExamples = standTypeDao.findAll();
List<StandType> finalFoundExamples = foundExamples;
SoftAssertions.assertSoftly(softAssertions -> softAssertions.assertThat(jpaQuery().from(standType).list(standType)).isEqualTo(finalFoundExamples).usingRecursiveComparison());
SoftAssertions.assertSoftly(softAssertions -> softAssertions.assertThat(queryFactory.selectFrom(standType).fetch()).isEqualTo(finalFoundExamples).usingRecursiveComparison());
}
@Test
@Transactional
public void create() {
Assertions.assertEquals(0, jpaQuery().from(standType).count());
Assertions.assertEquals(0, queryFactory.from(standType).stream().count());
StandType standType1 = generateRandomStandType();
standTypeDao.create(standType1);
SoftAssertions.assertSoftly(softAssertions -> softAssertions.assertThat(standType1).isEqualTo(jpaQuery().from(standType).singleResult(standType)).usingRecursiveComparison());
SoftAssertions.assertSoftly(softAssertions -> softAssertions.assertThat(standType1).isEqualTo(queryFactory.selectFrom(standType).fetchFirst()).usingRecursiveComparison());
}
@Test
......@@ -111,10 +120,10 @@ class StandTypeDaoTest {
exampleStandType.setPriceInCents(faker.number().randomNumber());
standTypeDao.update(exampleStandType);
StandType updatedStandType = jpaQuery()
.from(standType)
StandType updatedStandType = queryFactory
.selectFrom(standType)
.where(standType.id.eq(exampleStandType.getId()))
.singleResult(standType);
.fetchFirst();
SoftAssertions.assertSoftly(softAssertions -> softAssertions.assertThat(standTypeDao.update(exampleStandType)).isEqualTo(updatedStandType).usingRecursiveComparison());
}
......@@ -136,7 +145,7 @@ class StandTypeDaoTest {
standTypeDao.delete(exampleStandType);
Assertions.assertNull(
jpaQuery().from(standType).where(standType.id.eq(exampleStandType.getId())).singleResult(standType)
queryFactory.selectFrom(standType).where(standType.id.eq(exampleStandType.getId())).fetchFirst()
);
}
......@@ -145,8 +154,4 @@ class StandTypeDaoTest {
public void deleteNullArgument() {
assertThrows(NullPointerException.class, () -> standTypeDao.delete(null));
}
private JPAQuery jpaQuery() {
return new JPAQuery(entityManager);
}
}
package cz.fi.muni.pa165.flea.market.manager;
import com.github.javafaker.Faker;
import com.mysema.query.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import cz.fi.muni.pa165.flea.market.manager.dao.UserDao;
import cz.fi.muni.pa165.flea.market.manager.entity.QUser;
import cz.fi.muni.pa165.flea.market.manager.entity.User;
import cz.fi.muni.pa165.flea.market.manager.enums.UserRole;
import org.junit.jupiter.api.AfterEach;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
......@@ -23,6 +18,8 @@ import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
/**
* @author Lucia Lopuchova, xlopuch@mail.muni.cz
*/
......@@ -38,14 +35,14 @@ public class UserDaoTest {
private final Faker faker = new Faker();
private JPAQuery query;
private JPAQueryFactory queryFactory;
private User user;
@BeforeEach
public void init() {
user = createTestUser();
query = new JPAQuery(entityManager);
queryFactory = new JPAQueryFactory(entityManager);
}
@Test
......@@ -53,7 +50,7 @@ public class UserDaoTest {