2021年5月4日星期二

I add to Spring-session but Spring JPA Crud Repository save() not saving entity

i was using Curdrepository it was working very well but this days i added spring-session
strangely Repository save was not working in Interceptor ,save method of other place is working well

Interceptor

@Component  @Slf4j  public class FooInterceptor implements HandlerInterceptor {    @Autowired    private Repository Repository;      @Override    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object     handler){      String id = Utility.getLoginUserId(request.getSession());      String faid = Utility.getFacilityId(request.getSession());        Dto dto = Dto.builder()           .Id(id)          .Faid(faid)          .build();        FooHistoryService service = new FooHistoryService (Repository);        service.addHistory(dto);    }  }  

service
@Service  public class FooHistoryService {    private final Repository repository;      @Autowired    public FooHistoryService(Repository repository) {      this.repository = repository;    }      @Transactional    public void addHistory(Dto dto) {      FooHistory entity = new FooHistory();      entity.setId(dto.Id());      entity.setFaid(dto.Faid());      // it was working very well      // but it was not wokring after i added spring-sesion      repository.save(entity);    }  }  

Entity
@Data  @Accessors  @Entity  @Table(name = "table")  public class FooHistory {     @Id   @SequenceGenerator(name = "foo_id_seq",   sequenceName = "foo_id_seq", allocationSize = 1)   @GeneratedValue(strategy = GenerationType.SEQUENCE,   generator = "foo_id_seq")   private int operationHistoryId;      @NotNull   private String Id;   @NotNull   private String faid;  }  

repository
@Repository  public interface Repository extends JpaRepository<FooHistory, Integer> {    }  

now days, i add to Spring-session

Configurer

@EnableJdbcHttpSession()  public class JdbcHttpSessionConfig {    // if below code comment it is working well   //  @Bean  //  public PlatformTransactionManager transactionManager(DataSource dataSource) {  //      return new DataSourceTransactionManager(dataSource);   //  }  }  

pom.xml

 <dependency>      <groupId>org.springframework.session</groupId>      <artifactId>spring-session-jdbc</artifactId>   </dependency>  

i try to @Transactional comment but it wasn't working well
i try to JdbcHttpSessionConfig comment @Bean it was working well. this is the log

select nextval ('foo_id_seq')  insert into table ......  

while, i don't comment @Bean it wasn't working well. this is the log no insert
select nextval ('foo_id_seq')  

what is the problem? https://stackoverflow.com/questions/67393954/i-add-to-spring-session-but-spring-jpa-crud-repository-save-not-saving-entity May 05, 2021 at 09:06AM

没有评论:

发表评论