Tag: spring data jpa

使用SpringData创建只读存储库

是否可以使用Spring Data创建只读存储库? 我有一些实体链接到视图和一些子实体,我想为其提供一些存储库,其中包含一些方法,如findAll() , findOne()和一些带有@Query注释的方法。 我想避免提供像save(…)和delete(…)因为它们毫无意义并且可能会产生错误。 public interface ContactRepository extends JpaRepository, JpaSpecificationExecutor { List findContactByAddress_CityModel_Id(Integer cityId); List findContactByAddress_CityModel_Region_Id(Integer regionId); // … methods using @Query // no need to save/flush/delete } 谢谢!

转换Spring Data JPA Page内容的类型

我正在使用Spring Data JPA,我有一个使用JPASpecificationExecutor的PagingAndSortingRepository JPASpecificationExecutor 。 我将Specification和Pageable实例传递给此存储库的.findAll()方法以获取Page 。 但是,我的Contact实体有很多额外的字段和映射,我不需要在前端。 所以,我有一个只包含必要字段的ContactDto ,我有一个可以从Contact转换为ContactDto 。 private ContactDto convertToContactDto(Contact contact) { //do the conversion } 如何使用此转换方法将Page转换为Page ? 我可以获取Page的内容并像这样进行转换。 final Page contactPage = pagingAndSortingContactRepository .findAll(ContactSpecification.findByFirstNmLike(firstNm), pageable); final Collection contactDtos = contactPage.getContent() .stream() .map(this::convertToContactDto) .collect(Collectors.toList()); 但是我留下了Collection而不是Page ,我不知道如何将该Collection到Page的内容中。 有没有办法做到这一点? 或者是否有另一种方法来调用Page实例本身的转换?

无法自动配置DataSource:未指定“spring.datasource.url”

我已经使用Web,MongoDB和JPA依赖项从SPRING INITIALIZR创建了一个基本的Spring启动应用程序。 当我尝试运行spring boot应用程序时,我收到以下exception: Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug’ enabled. 2018-03-25 16:27:02.807 ERROR 16256 — [ restartedMain] osbdLoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Failed to auto-configure a DataSource: ‘spring.datasource.url’ is not specified and no embedded datasource could be auto-configured. Reason: Failed to determine a […]

如何记录Spring Data JPA存储库方法的执行时间?

我有简单的Spring Data JPA存储库。 public interface UserRepository extends JpaRepository{ … } 有没有办法监视Spring生成的方法的执行时间(例如findOne(…) )?

如何在Spring Data中精美地更新JPA实体?

所以我查看了有关使用Spring Data的JPA的各种教程,这在很多场合都有所不同,我不太确定正确的方法是什么。 假设有以下实体: package stackoverflowTest.dao; import javax.persistence.*; @Entity @Table(name = “customers”) public class Customer { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = “id”) private long id; @Column(name = “name”) private String name; public Customer(String name) { this.name = name; } public Customer() { } public long getId() { return id; } public String getName() { return […]

LocalDateTime到ZonedDateTime

我有Java 8 Spring网络应用程序,它将支持多个区域。 我需要为客户位置制作日历活动。 因此,假设我的网站和Postgres服务器托管在MST时区(但我想如果我们去云端,它可能就在任何地方)。 但客户是在美国东部时间。 所以,按照我读到的一些最佳实践,我想我会以UTC格式存储所有日期时间。 数据库中的所有日期时间字段都声明为TIMESTAMP。 所以这是我如何采用LocalDateTime并转换为UTC: ZonedDateTime startZonedDT = ZonedDateTime.ofLocal(dto.getStartDateTime(), ZoneOffset.UTC, null); //appointment startDateTime is a LocalDateTime appointment.setStartDateTime( startZonedDT.toLocalDateTime() ); 现在,例如,当搜索日期时,我必须从请求的日期时间转换为UTC,获取结果并转换为用户时区(存储在数据库中): ZoneId userTimeZone = ZoneId.of(timeZone.getID()); ZonedDateTime startZonedDT = ZonedDateTime.ofLocal(appointment.getStartDateTime(), userTimeZone, null); dto.setStartDateTime( startZonedDT.toLocalDateTime() ); 现在,我不确定这是正确的方法。 我也想知道是因为我从LocalDateTime到ZonedDateTime,反之亦然,我可能会失去任何时区信息。 这是我所看到的,这对我来说似乎不正确。 当我从UI收到LocalDateTime时,我得到了这个: 2016-04-04T08:00 ZonedDateTime = dateTime=2016-04-04T08:00 offset=”Z” zone=”Z” 然后,当我将转换后的值分配给我的约会LocalDateTime时,我存储: 2016-04-04T08:00 我觉得因为我存储在LocalDateTime中我失去了转换为ZonedDateTime的时区。 我应该让我的实体(约会)使用ZonedDateTime而不是LocalDateTime,以便Postgres不会丢失这些信息吗? —————- 编辑 —————- 在Basils出色的答案之后,我意识到我有一个不关心用户时区的奢侈 […]

如何检索给定域类的spring数据存储库实例?

给出某些类Bar中所有spring数据存储库的列表: @Autowired private List repositories; 如何在上面的列表中找到现有域类Foo的存储库? 假设存在以下内容: @Entity public class Foo { … } 和 public interface FooRepository extends JpaRepository {}

Spring Data JPA – “无法初始化代理 – 无会话” – 使用标记为事务的方法

我有一个模型有一个相当大的子实体图和hibernate最终制作大约9个语句懒洋洋地获取所需的所有数据,但约4级深我得到一个“无法初始化代理 – 没有会话”错误,我是不知道为什么。 调节器 @Transactional(readOnly = true) @RequestMapping(value = “/v2/plans”, method = RequestMethod.GET) public @ResponseBody List show(HttpServletRequest request) throws Exception { List planPresenters = new ArrayList(); CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery planQuery = criteriaBuilder.createQuery(Plan.class); Root root = planQuery.from(Plan.class); if (request.getParameter(“region”) != null || request.getParameter(“group”) != null) { List criteria = new ArrayList(); if (request.getParameter(“region”) != […]

Spring Data Repository不会删除ManyToOne Entity

我目前正在尝试使用Spring Data存储库来删除我的一些实体。 删除调用在没有任何exception/错误消息的情况下工作,但之后不会删除该实体。 这些是我的实体: public class Board implements Serializable { @Id @GeneratedValue(generator = “uuid2”) @GenericGenerator(name = “uuid2”, strategy = “uuid2”) @Column(columnDefinition = “BINARY(16)”) private UUID uuid; @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL, orphanRemoval = true, mappedBy = “board”) private List posts = new ArrayList(); } 和 public class Post implements Serializable { @Id @GeneratedValue private long id; @ManyToOne(optional = […]

创建bean’entalManagerFactory’时出错,嵌套HibernateException:无法获取默认的Bean Validation工厂

关于这个话题已经提出了很多问题,但似乎没有人能解决我的问题。 我尝试使用Maven,Spring,Hibernate和JPA与Mysql 5.5的示例项目。 这是一个测试桌面应用程序。 我不知道,我错在哪里。 mvn clean install正在提供Build Success。 但是当我跑步时,我会遇到exception。 需要帮助。 请参阅下面的代码。 > ************** BEGINNING PROGRAM ************** Feb 20, 2015 6:36:44 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@758fc9: startup date [Fri Feb 20 18:36:44 IST 2015]; root of context hierarchy Feb 20, 2015 6:36:45 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource […]