Запросы почти работают(создет таблицы с уникальными значениями)

This commit is contained in:
Максим Яковлев 2024-04-15 13:02:13 +04:00
parent a7b9355739
commit e7f8b1ffca
8 changed files with 50 additions and 118 deletions

Binary file not shown.

View File

@ -1,104 +1,22 @@
2024-04-13 20:42:31.143812+04:00 jdbc[13]: exception
org.h2.jdbc.JdbcSQLSyntaxErrorException: Синтаксическая ошибка в выражении SQL "SELECT * FROM ORDERS ORDERS [*]USERS TYPES ORDERS_GAMES"
Syntax error in SQL statement "SELECT * FROM ORDERS ORDERS [*]USERS TYPES ORDERS_GAMES"; SQL statement:
SELECT * FROM ORDERS ORDERS USERS TYPES ORDERS_GAMES [42000-224]
2024-04-13 20:42:31.965718+04:00 jdbc[13]: exception
org.h2.jdbc.JdbcSQLSyntaxErrorException: Синтаксическая ошибка в выражении SQL "SELECT * FROM ORDERS ORDERS [*]USERS TYPES ORDERS_GAMES"
Syntax error in SQL statement "SELECT * FROM ORDERS ORDERS [*]USERS TYPES ORDERS_GAMES"; SQL statement:
SELECT * FROM ORDERS ORDERS USERS TYPES ORDERS_GAMES [42000-224]
2024-04-13 21:20:26.232539+04:00 jdbc[3]: exception
org.h2.jdbc.JdbcSQLSyntaxErrorException: Значения типов данных "BIGINT" и "ROW(C1 UNKNOWN, C2 UNKNOWN)" не сравнимы друг с другом
Values of types "BIGINT" and "ROW(C1 UNKNOWN, C2 UNKNOWN)" are not comparable; SQL statement:
select ge1_0.id,ge1_0.description,ge1_0.name,ge1_0.price,ge1_0.type_id from games ge1_0 left join types t1_0 on t1_0.id=ge1_0.type_id left join games_genres g1_0 on ge1_0.id=g1_0.game_entity_id where t1_0.id=? and g1_0.genres_id=(?,?) offset ? rows fetch first ? rows only [90110-224]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:644)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:489)
2024-04-15 11:25:18.220809+04:00 database: wrong user or password; user: "SA"
org.h2.message.DbException: Неверное имя пользователя или пароль
Wrong user name or password [28000-224]
at org.h2.message.DbException.get(DbException.java:223)
at org.h2.value.TypeInfo.checkComparable(TypeInfo.java:766)
at org.h2.expression.condition.Comparison.optimize(Comparison.java:167)
at org.h2.expression.condition.ConditionAndOr.optimize(ConditionAndOr.java:137)
at org.h2.expression.Expression.optimizeCondition(Expression.java:148)
at org.h2.command.query.Select.prepareExpressions(Select.java:1177)
at org.h2.command.query.Query.prepare(Query.java:218)
at org.h2.command.Parser.prepareCommand(Parser.java:489)
at org.h2.engine.SessionLocal.prepareLocal(SessionLocal.java:639)
at org.h2.engine.SessionLocal.prepareCommand(SessionLocal.java:559)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1166)
at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:93)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:316)
at com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:327)
at com.zaxxer.hikari.pool.HikariProxyConnection.prepareStatement(HikariProxyConnection.java)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:153)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:183)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:155)
at org.hibernate.sql.exec.spi.JdbcSelectExecutor.lambda$list$0(JdbcSelectExecutor.java:85)
at org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess.executeQuery(DeferredResultSetAccess.java:231)
at org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess.getResultSet(DeferredResultSetAccess.java:167)
at org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl.advanceNext(JdbcValuesResultSetImpl.java:218)
at org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl.processNext(JdbcValuesResultSetImpl.java:98)
at org.hibernate.sql.results.jdbc.internal.AbstractJdbcValues.next(AbstractJdbcValues.java:19)
at org.hibernate.sql.results.internal.RowProcessingStateStandardImpl.next(RowProcessingStateStandardImpl.java:66)
at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:180)
at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:33)
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.doExecuteQuery(JdbcSelectExecutorStandardImpl.java:209)
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.executeQuery(JdbcSelectExecutorStandardImpl.java:83)
at org.hibernate.sql.exec.spi.JdbcSelectExecutor.list(JdbcSelectExecutor.java:76)
at org.hibernate.sql.exec.spi.JdbcSelectExecutor.list(JdbcSelectExecutor.java:65)
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.lambda$new$2(ConcreteSqmSelectQueryPlan.java:137)
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.withCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:362)
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:303)
at org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:509)
at org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:427)
at org.hibernate.query.Query.getResultList(Query.java:120)
at org.springframework.data.jpa.repository.query.JpaQueryExecution$PagedExecution.doExecute(JpaQueryExecution.java:204)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:92)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:149)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:137)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:170)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:158)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:164)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:143)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:392)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:135)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:220)
at jdk.proxy2/jdk.proxy2.$Proxy124.findByTypeIdAndGenres(Unknown Source)
at com.example.demo.games.service.GameService.getAll(GameService.java:29)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:351)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:196)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:765)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:392)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:765)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:717)
at com.example.demo.games.service.GameService$$SpringCGLIB$$0.getAll(<generated>)
at com.example.demo.games.api.GameController.getAll(GameController.java:70)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:255)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:188)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:925)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:830)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1089)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
at org.h2.message.DbException.get(DbException.java:199)
at org.h2.message.DbException.get(DbException.java:188)
at org.h2.engine.Engine.openSession(Engine.java:154)
at org.h2.engine.Engine.openSession(Engine.java:222)
at org.h2.engine.Engine.createSession(Engine.java:201)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:343)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:125)
at org.h2.util.JdbcUtils.getConnection(JdbcUtils.java:288)
at org.h2.server.web.WebServer.getConnection(WebServer.java:811)
at org.h2.server.web.WebApp.login(WebApp.java:1039)
at org.h2.server.web.WebApp.process(WebApp.java:227)
at org.h2.server.web.WebApp.processRequest(WebApp.java:177)
at org.h2.server.web.JakartaWebServlet.doGet(JakartaWebServlet.java:129)
at org.h2.server.web.JakartaWebServlet.doPost(JakartaWebServlet.java:166)
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:590)
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149)
@ -133,3 +51,8 @@ select ge1_0.id,ge1_0.description,ge1_0.name,ge1_0.price,ge1_0.type_id from game
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:63)
at java.base/java.lang.Thread.run(Thread.java:1623)
Caused by: org.h2.jdbc.JdbcSQLInvalidAuthorizationSpecException: Неверное имя пользователя или пароль
Wrong user name or password [28000-224]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:522)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:489)
... 50 more

View File

@ -61,15 +61,15 @@ public class DemoApplication implements CommandLineRunner {
genres2.add(genre2);
final var game1 = gameService.create(new GameEntity(type1,"Game1",2100.0,"good game", genres1));
// final var game2 = gameService.create(new GameEntity( type2, "Game2", 1200.0,"bad game", genres2));
// final List<GameEntity> games = new ArrayList<GameEntity>();
// games.add(game1);
// final var game2 = gameService.create(new GameEntity( type2, "Game2", 1200.0,"bad game", genres1));
final List<GameEntity> games = new ArrayList<GameEntity>();
games.add(game1);
// games.add(game2);
// var user1 = userService.create(new UserEntity( "login1", "email@mail.com", "qwerty123"));
var user1 = userService.create(new UserEntity( "login1", "email@mail.com", "qwerty123"));
// var user2 = userService.create(new UserEntity( "login2", "email@gmail.com", "qwerty1234"));
// orderService.create(1, new OrderEntity(user1,games));
orderService.create(6, new OrderEntity(user1,games));
// orderService.create(1, new OrderEntity(user1,games));
}
}

View File

@ -1,7 +1,5 @@
package com.example.demo.games.api;
import java.util.List;
import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
@ -63,9 +61,9 @@ public class GameController {
@GetMapping
public PageDto<GameDto> getAll(
@RequestParam(name = "typeId", defaultValue = "0") Long typeId,
@RequestParam(name = "typeId", defaultValue = "0") long typeId,
//@RequestParam(name = "genres", defaultValue = "") List<Long> genres,
@RequestParam(name = "genres", defaultValue = "") Long genre,
@RequestParam(name = "genreId", defaultValue = "0") long genre,
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size){
return PageDtoMapper.toDto(gameService.getAll(typeId, genre, page, size), this::toDto);

View File

@ -10,6 +10,7 @@ import com.example.demo.types.model.TypeEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
@ -24,10 +25,11 @@ public class GameEntity extends BaseEntity{
private Double price;
@Column(nullable = false)
private String description;
@OneToMany
@OneToMany(fetch=FetchType.EAGER)
@Column(unique = false)
private final List<GenreEntity> genres = new ArrayList<>();
@OneToOne
@JoinColumn(name = "typeId", nullable = false)
@JoinColumn(name = "typeId", nullable = false, unique = false)
private TypeEntity type;
public GameEntity(){

View File

@ -8,10 +8,11 @@ import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.example.demo.games.model.GameEntity;
import com.example.demo.genres.model.GenreEntity;
public interface GameRepository extends CrudRepository<GameEntity, Long>, PagingAndSortingRepository<GameEntity, Long> {
Optional<GameEntity> findOneById(Long id);
Page<GameEntity> findByTypeIdAndGenres(long typeId, Long genres, Pageable pageable);
Optional<GameEntity> findOneById(long id);
Page<GameEntity> findByTypeIdAndGenres(long typeId, GenreEntity genre, Pageable pageable);
Page<GameEntity> findByTypeId(long typeId, Pageable pageable);
Page<GameEntity> findByGenres(Long genres, Pageable pageable);
Page<GameEntity> findByGenres(GenreEntity genre, Pageable pageable);
}

View File

@ -11,14 +11,17 @@ import org.springframework.data.domain.Pageable;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.games.model.GameEntity;
import com.example.demo.games.repository.GameRepository;
import com.example.demo.genres.model.GenreEntity;
import com.example.demo.genres.service.GenreService;
@Service
public class GameService {
private final GameRepository repository;
private final GenreService genreService;
public GameService(GameRepository repository, GenreService genreService){
this.repository = repository;
this.genreService = genreService;
}
// @Transactional(readOnly = true)
@ -39,14 +42,18 @@ public class GameService {
//getAll.stream().filter(game -> game.getType().getId().equals(typeId)).toList();
@Transactional(readOnly = true)
public Page<GameEntity> getAll(Long typeId, Long genre, int page, int size){
public Page<GameEntity> getAll(long typeId, long genre, int page, int size){
final Pageable pageRequest = PageRequest.of(page, size);
GenreEntity NeedGenre = null;
if(genre != 0){
NeedGenre = genreService.get(genre);
}
if(!Objects.equals(typeId, 0L) && !Objects.equals(genre, 0L)){
return repository.findByTypeIdAndGenres(typeId, genre, pageRequest);
return repository.findByTypeIdAndGenres(typeId, NeedGenre, pageRequest);
}
if(Objects.equals(typeId, 0L) && !Objects.equals(genre, 0L)){
return repository.findByGenres(genre, pageRequest);
return repository.findByGenres(NeedGenre, pageRequest);
}
if(!Objects.equals(typeId, 0L) && Objects.equals(genre, 0L)){
return repository.findByTypeId(typeId, pageRequest);

View File

@ -11,6 +11,7 @@ import com.example.demo.games.model.GameEntity;
import com.example.demo.users.model.UserEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
@ -24,7 +25,7 @@ public class OrderEntity extends BaseEntity{
@ManyToOne
@JoinColumn(name = "userId", nullable = false)
private UserEntity user;
@OneToMany
@OneToMany(fetch=FetchType.EAGER)
private Set<GameEntity> games = new HashSet<>();
public OrderEntity(){