Tag: 春天

Mybatis将COALESCE(int,0)映射到java.lang.Long

假设我有一个包含两个cols name (varchar(64))& value (int(11))的数据库table ,当我使用: @Select("SELECT table.name table.value FROM table") fun list(): List<Item> table.value将table.value映射到java.lang.Integer ,但如果我使用COALESCE将默认值应用于0: @Select("SELECT table.name COALESCE(table.value, 0) FROM table") fun list(): List<Item> table.value将table.value映射到java.lang.Long 。 为什么?

选择在休眠

我怎样才能将ST_Distance(u.position,st_sets(st_makepoint(:lat,:lng),4326))添加到Hibernate的select语句中? @Query("""SELECT u FROM Driver u ORDER BY ST_Distance(u.position, st_setsrid(st_makepoint(:lat, :lng), 4326)) """) fun findNearest(@Param("lat") lat: Double, @Param("lng") lng: Double): List<Driver> 当我尝试这个: @Query("""SELECT u, ST_Distance(u.position, st_setsrid(st_makepoint(:lat, :lng), 4326)) as position FROM Driver u ORDER BY ST_Distance(u.position, st_setsrid(st_makepoint(:lat, :lng), 4326)) """) fun findNearest(@Param("lat") lat: Double, @Param("lng") lng: Double): List<Driver> 我得到了以下错误: Caused by: java.lang.IllegalStateException: No data type […]

Spring Data JPA:处理其他实体的继承属性

情况:(班级为实体) class1具有class2类型的属性 subclass1 (从class1继承)使用类型subclass2 (从class2继承)的属性 subclass2有一个class2没有的property2 我怎么能实现这个在subclass1Repository中: findByProperty_property2(xx) 很明显,我得到“无属性”属性2“找到类型class1 ,遍历路径: class2 .property2。 JPA显然不知道属性的类型是subclass2 。 @Entity open class Class1 constructor( @ManyToOne property: Class2 ) @Entity open class Class 2 constructor() { } @Entity open class subclass1 constructor( property: Subclass2 ):Class1(property = property) @Entity open class subclass2 constructor( property2: Double ):Class2(..) interface subclass1Repository: JpaRepository<Subclass1, Long>{ fun findByProperty_property2(prop2:Double): […]

如何判断我的Spring启动应用程序是否处于调试模式?

我想修改我的应用程序的工作方式,取决于–debug开关是否存在。 我在@Configuration文件中试过这个: @Value("\${debug}") lateinit var debug: String 但春天说 无法在值“$ {debug}”中解析占位符“调试” 我如何查询–debug选项的状态?

Hibernate保存具有空的父ID的子实体

Hibernate不想保存子实体的ID。 我有以下表格: @Entity @Table(name = "ct_orders") data class Order( @Id @Column(name = "id") @GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY) val id: Int = 0, @OneToMany(fetch = FetchType.LAZY, cascade = arrayOf(CascadeType.ALL), mappedBy = "order") val route: List<Route>? = null, … ) @Entity @Table(name = "ct_routes") @JsonIgnoreProperties("id", "order") data class Route( @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.IDENTITY) val id: […]

如何在基于Spring的强类型语言中正确地使用PATCH – example

据我所知: PUT – 用其整个表示更新对象(替换) 修补程序 – 仅使用给定字段更新对象(更新) 我正在使用Spring来实现一个非常简单的HTTP服务器。 当用户想要更新他的数据时,他需要为某个端点(比如: api/user )创建一个HTTP PATCH 。 他的请求体通过@RequestBody映射到DTO,如下所示: class PatchUserRequest { @Email @Length(min = 5, max = 50) var email: String? = null @Length(max = 100) var name: String? = null … } 然后我使用这个类的一个对象来更新(修补)用户对象: fun patchWithRequest(userRequest: PatchUserRequest) { if (!userRequest.email.isNullOrEmpty()) { email = userRequest.email!! } if (!userRequest.name.isNullOrEmpty()) { name […]

通过外部配置通知命名策略

注意:这是一个Spring / Hibernate的问题。 我碰巧使用Kotlin,但也欢迎Java的答案。 JPA实体必须有一个常量表和目录名称: User.kt : @Entity @Table(name="users", catalog="auth") data class User( @Id val username: String, var password: String ) 这对我来说是个坏消息。 我的MySQL数据库托管同一个应用程序堆栈的几个不同的实例。 结果是:我有各种各样的模式,名字由环境隔开: SHOW DATABASES; sandbox_billing sandbox_auth staging_billing staging_auth preproduction_billing preproduction_auth 每个应用程序上下文都有一个数据源,可以访问两个目录: *_billing (用于域实体)和*_auth (用于身份验证信息)。 我的应用程序有一个可配置的映射com.stackoverflow.persistence.catalog-overrides ,其中可以指定用于该特定应用程序实例的目录名称: application.yml : spring: datasource: url: 'jdbc:mysql://mycooldatabase.com:3306' com.stackoverflow: persistence: catalog-overrides: billing: sandbox_billing auth: sandbox_auth 这个配置被填充到POKO中: PersistenceProperties.kt : @Component […]

Spring Security,Method Security注解(@Secured)不工作(java config)

我正在尝试使用@Secured(“ADMIN”)(没有任何XML,只有java配置,Spring Boot)来设置方法安全注释。 但通过角色访问不起作用。 安全配置: @Configuration @EnableWebSecurity public class AppSecurityConfiguration extends WebSecurityConfigurerAdapter{ ….. @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/api/**").fullyAuthenticated().and() .addFilterBefore(tokenAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); } ….. } 我想限制访问控制器的方法: @RestController @RequestMapping("/api/groups") public class GroupController { @Autowired private GroupService groupService; @Secured("ADMIN") @RequestMapping public List<Group> list() { return groupService.findAll(); } } 通过url限制访问正在工作,与: .antMatchers("/api/**").hasAuthority("ADMIN") 也许我忘了指定,我想限制的角色? UPD:按照规则,在控制层或服务层中的哪个层必须是@PreAuthorize("hasRole('ADMIN')") ?

将thymeleaf版本2.14迁移到3.0不能找到消息属性

Thymeleaf版本2.14正确拾取message.properties文件。 迁移到3.0之后,它给错误了hello.MESSAGE_en_US ??。 但在javacode通过自动装配messsagesource messageSource.getMessage( "hello.MESSAGE",null, Locale.getDefault()) ->Hello App 项目结构 src/main/ -java -com.cando -controllers -resources -messages_en.properties -spring.properties -webapp -WEB-INF -templates -index.html 的index.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <title>Helllo</title> </head> <body> <p th:text="#{hello.MESSAGE}">Hello App!</p> </body> </html> messages_en.properties hello.MESSAGE=Hello App 配置 @Bean public ResourceBundleMessageSource messageSource() { ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource(); resourceBundleMessageSource.setBasename("messages"); resourceBundleMessageSource.setBasename("spring"); return resourceBundleMessageSource; }

使用@ExceptionHandler处理spring安全认证异常

我使用Spring MVC的@ControllerAdvice和@ExceptionHandler来处理REST API的所有异常。 它适用于web mvc控制器抛出的异常,但对于Spring安全自定义筛选器引发的异常无效,因为它们在调用控制器方法之前运行。 我有一个自定义的春季安全过滤器,执行基于令牌的身份验证: public class AegisAuthenticationFilter extends GenericFilterBean { … public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { try { … } catch(AuthenticationException authenticationException) { SecurityContextHolder.clearContext(); authenticationEntryPoint.commence(request, response, authenticationException); } } } 有了这个自定义的入口点: @Component("restAuthenticationEntryPoint") public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint{ public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authenticationException) throws […]