小编Lex*_*int的帖子

使用@Transactional(readOnly = true)有什么优势?

我是新手,据我所知,@Transactional只需确保@Transactional将带有注释的类或方法的所有内部工作都包装在一个事务中,并且来自外部源的所有调用将创建一个新事务,但是为什么我们实际上需要这些注释?下面的存储库以及readOnly = true在常见情况下使用它的优势是什么?这是使用SpringHibernatehttps://github.com/spring-projects/spring-petclinic)的Spring pet-clinic示例应用程序。

/**
 * Repository class for <code>Pet</code> domain objects All method names are compliant with Spring Data naming
 * conventions so this interface can easily be extended for Spring Data See here: http://static.springsource.org/spring-data/jpa/docs/current/reference/html/jpa.repositories.html#jpa.query-methods.query-creation
 *
 * @author Ken Krebs
 * @author Juergen Hoeller
 * @author Sam Brannen
 * @author Michael Isvy
 */
public interface PetRepository extends Repository<Pet, Integer> {

    /**
     * Retrieve all {@link PetType}s from …
Run Code Online (Sandbox Code Playgroud)

spring hibernate transactions spring-transactions spring-data

4
推荐指数
1
解决办法
1810
查看次数

为什么 Spring 在绑定用 @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) 注释的 LocalDateTime 时忽略输入字符串的时间偏移?

我有一个Activity带有该字段的对象:

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime start;
Run Code Online (Sandbox Code Playgroud)

我通过发送表单将此对象绑定到控制器方法:

@RequestMapping(value = "update", method = RequestMethod.POST)
public String submitUpdateActivityForm(Activity activity) {
      activityRepository.save(activity);
      return "successPage;
}
Run Code Online (Sandbox Code Playgroud)

我正在使用 Spring Boot 1.5.1、Spring MVC 4.3.6,并且在我的 Web 应用程序中,我希望从任何时区的客户端接收时间,但LocalDateTime始终保持UTC。但是当 Spring 将对象从表单绑定到控制器中的请求参数时,它完全忽略了 type 属性的输入字符串的时间偏移LocalDateTime

我认为根据@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)Spring 的文档会找到字符串中的偏移量并将输入日期时间从给定的时区转换为我的服务器的时区(即 UTC)然后绑定。

例如:我希望将字符串2017-05-31T12:00-03:00转换为LocalDateTime '2017-05-31T15:00'2017-05-31T12:00Z在不更改的情况下解释为LocalDateTime '2017-05-31T12:00'. 不幸的是,LocalDateTime无论时间偏移如何,我总是得到。

这是正确的行为@DateTimeFormat还是我做错了什么?我应该实现 SpringConverter还是扩展PropertyEditorSupport

这是处理时间的正确方法吗?我想接受任何类型的日期,但将它们保留在 UTC 中,LocalDateTime因为这样我就可以轻松地将它们发送到客户端,在那里我可以将它们从 UTC 转换为客户端的本地时区并显示。

spring datetime spring-mvc java-time

2
推荐指数
1
解决办法
1508
查看次数