Tag: spring data jpa

Spring Field需要一个无法找到的类型的bean Spring JPA

这是我在论坛上的第一个问题,因为我几乎陷入了死胡同。 我使用spring开发一个宁静的Web服务,在该服务中我想使用spring数据在db中存储一些数据。 但是,按照网站上的教程和我的入门指南,我不断遇到类似的问题,似乎这些教程总是缺少一些东西。 这是我的代码。 Application.java package hello; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication(scanBasePackages=”hello.Application”) @EnableJpaRepositories(“hello.provScoreRepo”) @ComponentScan(“Controller”) @EntityScan(“hello.provScore”) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } provScore.java package hello; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class provScore { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long […]

使用TaskExecutor时数据插入问题

在我的Web服务应用程序中,我实现异步任务执行器以在数据库中插入对象。 但在第一次插入时,它会抛出exception。 org.springframework.orm.hibernate3.HibernateSystemException: A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance: 第二次刷新url时,对象成功插入数据库。 问题在于未插入对象的新请求的第一个实例。 XML文件 我使用了org.springframework.core.task.SimpleAsyncTaskExecutor / org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor以及子表列表是clear() , addall() 。 但没有运气。 如果我对任务执行程序标记进行注释,则会正确插入所有新请求对象。 在我的场景中,我必须实现异步任务执行器。 RptServiceImp.java public class RptServiceImp{ @javax.annotation.Resource private ApplicationEventPublisher applicationEventPublisher; @Transactional public void process(final RqstLoad rqstLoad ) { try { applicationEventPublisher.publishEvent(new LoggingEvt (this)); }catch(Exception e){ System.out.println(“e:: “+e); e.printStackTrace(); } […]

Java Spring JPA Repository

我是一个spring的菜鸟,我正在努力。 基本上在开始使用Spring与JPA一起开发我的服务器之前,我试图开始一个简单的例子,以便习惯这个框架。 我已经成功使Spring使用一些框架如Log4J,Swagger等。 现在我正在尝试使用JPA,我可以找到一些解决方案。 我看到了一些关于如何用它开发的博客,以及我选择创建Repository Interfece并extend Repository数千个选项。 您可以看到我的代码: package com.example.model; @Entity public class Person { @Id public Integer id; public String name; public Person(){} } package com.example.repository; public interface PersonRepository extends Repository { Collection findAll(); } package com.example.controller; @RestController public class PersonController { @Autowired private PersonRepository repo; @RequestMapping(value = “/persons”, method = RequestMethod.GET) public Collection […]

在spring-boot中侦听存储库事件

我正在尝试RepositoryEventListener在spring-boot应用程序中工作,但我想我做错了… 这是Listener中的代码 @SuppressWarnings(“rawtypes”) public class BeforeSaveEventListener extends AbstractRepositoryEventListener { @Override public void onBeforeSave(Object customer) { throw new RuntimeException(“++++ BEFORE SAVE EVENT ++++”); } } 这是我的Application类 @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(Application.class); springApplication.addListeners(new BeforeSaveEventListener()); springApplication.run(args); } } 在保存操作中,我可以看到这些事件被触发: Current Event is org.springframework.data.rest.core.event.BeforeCreateEvent received! Current Event is […]

如何使用Hibernate为Spring数据JPA的所有查找方法添加全局where子句?

我们正在使用Spring数据JPA和hibernate开发Web应用程序。 在应用程序中,每个实体都有一个compid字段。 这意味着每个数据库调用(Spring Data方法)都必须使用compid进行检查。 我需要一种方法,这个“compid =?” 检查每个查找方法是否自动注入。 因此,我们不必特别打扰编译检查。 这可能是从Spring Data JPA框架实现的吗?

使用Spring Boot 1.4从Spring Boot Test中排除Spring集成

我最近开始使用Spring Boot 1.4和新的Spring Boot Testfunction。 我正在使用spring-boot-starter-integration与Spring Boot 1.4.2.RELEASE并尝试使用新的@DataJpaTest测试我的存储库 当我运行我的测试时,我得到一个关于没有限定bean的exception。 有问题的bean是我的集成bean的handler 。 如何在JPA测试期间排除集成运行? 申请类别: import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource(“integration.xml”) public class AutomateResultConsumerApplication { public static void main(String[] args) { SpringApplication.run(AutomateResultConsumerApplication.class, args); } } 测试类: @RunWith(SpringRunner.class) @DataJpaTest public class SampleHistoryRepositoryTest { @Autowired private TestEntityManager entityManager; @Autowired private SampleHistoryRepository sampleHistoryRepository; @Test public void findAllByObjectIdAndField() throws […]

从属性文件加载JPA存储库查询

我需要从属性文件中加载存储库中的查询。 例如,这里: @Repository public interface StudentRepository extends JpaRepository { @Query(value=”SELECT * FROM student where year= :le”, native=true) public List getStudentsByLevel(@Param(“le”) int level); } 我需要从属性文件中加载”SELECT * FROM student where year= :le”字符串。 有什么办法吗?

修改本机查询不能有命名参数绑定?

我在JpaRepository中指定了以下修改本机查询。 public interface PlayerStatisticsRepository extends JpaRepository { @Modifying @Query(value = “INSERT INTO playerstatistics(enteredTheFieldAtInSeconds, leftTheFieldAtInSeconds, hometeam_name, matchday_matchdaynumber, playedFromTheFirstWhistleblow, player_id) VALUES (:enteredTheFieldAtInSeconds, :leftTheFieldAtInSeconds, :#{#match.primaryKey.homeTeam.name}, :#{#match.primaryKey.matchday.matchdayNumber}, :playedFromTheFirstWhistleblow, :#{#player.id})”, nativeQuery = true) void insertIntoPlayerStatistics(int enteredTheFieldAtInSeconds, int leftTheFieldAtInSeconds, boolean playedFromTheFirstWhistleblow, Match match, Player player); } 不幸的是,它以运行时的exception结束,说可能是match参数的绑定不存在,而它显然是。 org.springframework.dao.InvalidDataAccessApiUsageException: No parameter binding found for name match!; nested exception is java.lang.IllegalArgumentException: No parameter […]

Spring Data JPA Query中按子对象过滤时出错

我的代码结构如下所示。 文章: @Entity public class NewsArticle{ @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; [Other class properties such as title, publisher, publishedDate, etc.] @OneToMany(mappedBy = “article”) private Set userReadNewsArticles = new HashSet(); [Getters and Setters] } 用户阅读的文章: @Entity public class UserReadNewsArticle { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; private Long readAccountId; private Long readArticleId; @JsonIgnore @ManyToOne private Account account; […]

将动态数据源路由与spring-data-rest相结合

我正在使用动态数据源路由,如本博客文章中所示: http : //spring.io/blog/2007/01/23/dynamic-datasource-routing/ 这很好用,但是当我将它与spring-data-rest并浏览我生成的存储库时,我(正确地)得到了一个exception,我的查找键没有定义(我没有设置默认值)。 在与数据库建立任何连接之前,如何以及在何处可以挂钩Spring数据rest请求处理以基于’x’(用户授权,路径前缀或其他)设置lookup-key? 代码方面我的数据源配置主要匹配顶部的博客post,一些基本的实体类,生成的存储库和Spring Boot将所有内容组合在一起。 如果需要我可以发布一些代码,但没有什么可以看到的。