问题标签 [crud-repository]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
223 浏览

java - CrudRepository - Hibernate - 用手动设置 id 覆盖现有模型

我有一个问题,从现在开始我一直在尝试解决。

我必须使用 CrudRepository 和 Hibernate 在数据库中保存具有手动设置 ID 的模型。

但是id的手动设置总是被忽略。

是否有可能,强制

CrudRepository.save(模型 m)

用 UPDATE 持久化给定的模型?

查询总是产生 INSERT 语句,而不使用 id。

我必须手动执行此操作的原因是,标识符不是数据库 ID - 它是作为 UUID 在外部生成的 ID,在具有此模型条目的多个数据库中是唯一的。该模型通过 hazelcast-cluster 作为序列化对象共享。

举个例子:数据库已经包含一个 ID 为 1 的模型条目:

现在我需要更新它。我创建了一个新的模型版本

id 字段使用@Id 和@GeneratedValue 注解进行标记(对于id 为null 并且应该生成id 的情况)

我什至尝试将此字段仅更改为没有@GeneratedValue 的@Id 字段,并始终自行生成ID。它没有效果,它总是使用 INSERT 语句,从不 UPDATE。

我究竟做错了什么?

除了 id 之外,CrudRepository 是否有另一个标识符将模型声明为现有模型?

我很高兴得到任何帮助。

0 投票
1 回答
78 浏览

spring-boot - findAll() 返回 Iterable 的目的是什么在 CrudRepository 界面中

每当您有一个扩展 CrudRepository 的存储库时,例如:

并且您想使用该方法findAll()

你会得到一个可迭代的。显然,我可以将 Iterable 转换为列表,如下所示:

或者我可以覆盖findAll()存储库上的方法,甚至可以简单地使用JpaRepository,但我的问题是:

  1. 首先获得 Iterable 的目的是什么?通过将其转换为列表,我是否违背了使用 CrudRepository 的全部目的?
  2. 如果确实是将 Iterable 转换为 List 的好方法。实现这一目标的最有效方法是什么?

希望有人能给我一些见解。

0 投票
1 回答
316 浏览

spring-boot - 如何在 Spring Boot 的 @RestController 注释使用的方法中使用带参数的构造函数来创建请求处理程序

我买了这本新书,试图快速学习 Spring Boot。开始的时候很好,我很容易创建了一个 REST API。但后来我们添加了 CrudRepository,我发现书中描述的代码存在问题。此外,没有代码可供下载,因为作者从 Oreily 的 git repo 中删除了它以修复一些问题......

问题是,如果我尝试按照书中描述的那样构建代码(没有默认构造函数),我会收到一个 Java 错误,抱怨没有默认构造函数。如果我添加一个默认构造函数,它会构建,但 Spring 使用它而不是新的构造函数,这需要传递一个参数。因此,当我实际调用 API 时,例如调用 /coffees 端点时,我得到一个 java.lang.NullPointerException: null

那么 Spring 应该如何知道要使用哪个构造函数,以及它如何为这个参数传递值呢?

这是控制器:

这是我定义接口的地方:

这是主要课程- 为塞在底部的课程道歉。

0 投票
0 回答
29 浏览

java-8 - RxJava objects assing from findAll are null

I'm using RxJava2CrudRepository to retrieve records from a table using findAll method.

findAll return a Flowable<T> findAll();

Here is what I got.

At this point everything works fine and I get the records from the database.

I need to iterate response to do some logic with the data. When I run over using forEach I get the values from every record. ie:

Output:

But when I try to assing these values to a java bean the output is always null

Output:

I'm trying to add the data result from response to a set that is why I'm iterating. I don't know what I'm missing or if my approach is wrong.

0 投票
0 回答
36 浏览

spring-boot - CrudRepository 返回错误的结果集

我对 Hibernate 和 CrudRepository 有疑问。

我有一个控制器,它返回一组由视图获取的结果。我创建了一个视图,因为我认为这是管理我的数据库实体的最佳方式,它是这样创建的:

我已经实现了像这样对 View 建模的类:

我的 RepositoryService 看起来像这样:

最后,我有这样的存储库界面:

在不使用 CrudRepository 和在 Sql Manager 或 MySql Workbench 上使用普通 Select 的情况下,一切都可以完美运行。我在同一个 Giuld 中添加了 3 个公会成员,具有不同的名称和角色,但是我从控制器和方法 findMembersByGuildId(guildId) 获得的结果集如下:

事实上,结果似乎复制了其中一个记录结果的确切时间。我希望有人能给我帮助!

提前致谢

0 投票
1 回答
20 浏览

spring-boot - 打包工作的 Spring Tool Suite 应用程序时出错

我在 Spring Tools Suite 上编写了一个 Spring Boot 应用程序,该应用程序在 STS 上运行良好。这是 Spring In Action Fifth Edition 中的一个示例(顺便说一下,本书的代码有很多错误)。但是当我试图将它打包到一个 jar 文件中时,Maven 突然开始抛出编译错误。基本上消息是:

它应该在 Spring Data JPA 上我们可以使用我们不需要实现的接口(CrudRepository),它将为我们提供很多 CRUD 方法。这个事实以及它在 STS 上运行顺利的事实(并且没有抱怨任何未实现的方法)让我感到迷茫。任何帮助将不胜感激。

0 投票
0 回答
47 浏览

jpa - JPA CrudRepository 上删除 DataIntegrityViolationException 错误消息

我有一个 Spring Boot 应用程序,它在 h2 数据库中有 2 个表:Computer和一个Processor

计算机和处理器之间存在单向、多对一的关系,因此许多计算机可以拥有一个处理器。在架构中,关系上有一个外键约束,因此不允许您删除某些计算机正在使用的处理器。

我希望能够向用户显示有用的错误消息,例如:“处理器'M1'不能被删除,因为它被计算机'MacBook,MacBook Pro'使用。 ”但是我能够成功捕获DataIntegrityViolationException此错误不包含足够的信息,我无法生成所需的错误消息。它只提到违反了定义的 foreignKeyConstraint。

因此,作为一种解决方法,当引发DataIntegrityViolationException时,我会捕获它并查询数据库以获取特定实例,为什么不能删除处理器并以这种方式生成错误消息。我可以很容易地不在模式中添加 FK 约束,只需检查服务中的数据库,以确保要删除的处理器未在数据库中的任何计算机中使用,并生成适当的错误。但是,以编程方式检查 FK 约束感觉就像是代码异味。

有没有办法让我在数据库级别使用 FK 约束并使用DataIntegrityViolationException生成有用的错误消息?

提前感谢您的帮助!!

0 投票
0 回答
76 浏览

hibernate - Spring JPA Naming troubles

I have an issue which is driven me crazy. I have two apps (Taco Cloud example apps from Spring in Action 5, book) The jpa is working OK. It contain some scripts to generate database schema. One of the generated tables is named taco_order_tacos, it is all-key entity and the generated ids are : order_id and taco_id. Exactly same script and same Entity objects fail in another project, because CrudRepository looks for a tacos_id (with s at the end of taco word, plural) when try to save an object (Order). By the way, both projects are using same spring-boot-starters dependencies. Of course adding "s" to taco_id (tacos_id) on failing project's taco_order_tacos table, it is working fine, but I need to understand what is going on. Thanks in advance for any help.

Here is error stack:

EDITION TO ADD SOME CODE:

Here is the class which is creating the conflict, but is exactly same class on OK project. As you can see, there are no columns/table renaming, so IMO, there is no point to try to make an insert on Tacos_ID, when table name is Taco:

0 投票
3 回答
188 浏览

java - 我正在尝试使用 spring boot 执行一个简单的 crud 操作 findall(),它没有任何错误或警告,但给出了 NULL 异常

这是 TopicService.java 的一部分

我在这一行得到一个 NULL 指针异常 TopicRepo.findAll().forEach(topics::add);

私有主题Repo TopicRepo;这是我自动连接依赖项的地方

topicRepo.java,这是我扩展 crud 存储库的地方

这就是我的 application.properties 文件中的内容

这是我的 Topic.java 的一部分

0 投票
2 回答
92 浏览

spring-boot - 如何为自动生成的 ID 执行 findById

我有一个场景,我正在消费一个事件并将详细信息保存在数据库中。现在存储在数据库中的记录具有 id 字段 autogenerated @GeneratedValue(strategy = GenerationType.IDENTITY)

在我的测试用例中,我需要检查数据是否存储在数据库中并且是否符合预期。但我不确定我将如何findById()处理 SpringBoot Crud/JPA Repository,因为我不知道生成了什么值。

任何帮助,将不胜感激。