Tag: spring data jpa

如何在Spring中只实现CrudRepository的具体方法?

我正在使用spring-data-jpa的CrudRepository来定义一个实体的接口,然后使用所有标准的crud方法而不必显式提供一个实现,例如: public interface UserRepo extends CrudRepository { } 虽然现在我想在我的自定义实现中只覆盖save()方法。 我怎么能实现这个目标? 因为,如果我实现了UserRepo接口,我必须实现从接口CrudRepositoryinheritance的所有其他CRUD方法。 我不能编写自己的实现,它具有所有CRUD方法,但只重写一个而不必自己实现所有其他方法吗?

Controller中的Tune方法。 无法构建Hibernate SessionFactory

如何实现这个想法? 发送信息:{“idBanner”:2,“fullnameClient”:“Aria”}。 如果单击“不包含在数据库中”,则单击“保存”并发送电子邮件businesscenter。 否则,发送一条消息:“你回应了”。 错误可能是版本依赖? (实体):单击绑定Banner,Banner绑定Businesscentr。

在filter中访问spring boot数据jpa实体

目前我正在尝试将基于servlet的应用程序转换为带控制器的spring启动应用程序。 设置filter后,我在访问实体函数时遇到“无法初始化代理 – 无会话”-Exception。 (这里:用户方法“isAdmin”)。 我设置了这样的filter: public class AdminFilter implements Filter { @Autowired UserRepository userRepository; @Override @Transactional public void doFilter(ServletRequest aRequest, ServletResponse aResponse, FilterChain aFilterChain) throws IOException, ServletException { User u = userRepository.getOne(5l).orElse(null); System.out.println(u.isAdmin()); aFilterChain.doFilter(aRequest, aResponse); } @Override public void init(FilterConfig filterConfig) throws ServletException { SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, filterConfig.getServletContext()); } @Override public void destroy() { } } […]

如何创建复杂查询的请求? @Query jpql spring jpa

如何使用@Query在JpaRepository创建复杂查询的请求? 我担心实体之间的关系。 它们可能会影响请求的准备。 需要查询SQL,我的版本@Query:

根据spring jpa中的实体从多个表中获取数据

我有三个实体,如下所示: 注释: @Entity @Table(name = “comments”) public class CommentBean implements Serializable { @Id @Column(name = “id”) @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column(name = “commentId”) private long commentId; @Column(name = “topicId”) private String topicId; } 话题: @Entity @Table(name = “topics”) public class TopicBean implements Serializable { @Id @Column(name = “id”) @GeneratedValue(strategy = GenerationType.AUTO) private Long […]

ClassCastException:尝试迭代实体ID时,无法将Integer强制转换为Long

我的服务中有以下方法: public Set moveHoldedCardsToNewBoard(Board newBoard, Board prevBoard) { Set boardCards = new HashSet(); if (prevBoard != null) { List holdedCardIds = getExcludedCardIds(prevBoard); for (Long item: holdedCardIds) { } } 当我想循环holdedCardIds列表时,我收到: java.lang.ClassCastException:java.lang.Integer不能在这个地方for (Long item: holdedCardIds) { 为java.lang.Long – > for (Long item: holdedCardIds) { 我的getExcludedCardIds()看起来像: @Override public List getExcludedCardIds(Board board) { return boardCardRepository.getExcludedCardIds(board.getId()); } 库: @Repository […]

JpaRepository,@ Transaction和repository.saveAndFlush

我正在服务/存储库方法的第一次破解,并遇到了一个问题。 基本上我想在我的服务中做的是持久化我的实体,然后在同一个Service方法中使用它的ID。 最初我打算使用@GeneratedValue和Sequences,但放弃并决定手动刷新实体并获取ID,我认为这会更容易。 My Repository是一个使用Spring Data的接口,因此它支持手动刷新。 据我了解,它也用@Transactional注释。 我的服务方法也使用@Transactional注释。 我发现实体仅在返回Service方法时持久化,即使我在保存实体后立即刷新(或使用saveAndFlush)。 我认为冲洗会迫使数据库发生变化吗?

Spring JPA存储库:阻止保存更新

我的user数据库表如下所示: CREATE TABLE user ( username VARCHAR(32) PRIMARY KEY, first_name VARCHAR(256) NOT NULL, last_name VARCHAR(256) NOT NULL, password VARCHAR(32) NOT NULL, enabled BOOL ) ENGINE = InnoDB; 这是我的实体的字段定义: @Entity public class User implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(nullable = false) private String username; @Column(nullable = false) private String firstName; […]

Hibernate @Proxy(lazy = false)注释有什么作用?

在尝试序列化作为JPA实体的ESRBRating对象时,我遇到了两个不同的堆栈跟踪(见下文)。 我正在使用Spring Data JPA。 控制器调用服务,称为存储库的服务。 我能够通过在我的ESRBRating对象上添加@Proxy(lazy = false)来解决问题。 我的主要问题是@Proxy(lazy = false)实际上做了什么? 添加时为什么会起作用? 这是一个很好的解决方案,还是会产生诸如性能/内存问题等副作用? 作为参考,这是我的ESRBRating课程。 @Entity @Table(name = “esrb_rating”, schema = “igdb”) @JsonIgnoreProperties(ignoreUnknown = true) @Proxy(lazy = false) public class ESRBRating implements Serializable { private static final long serialVersionUID = 1L; @Id @NotNull @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = “id”, nullable = false) private Long id; @NotNull […]

Spring Data JPA存储库:派生查询中的IN子句不起作用

我有一个看起来像这样的存储库: public interface UserRepository extends JpaRepository { User findByEmailIgnoreCase(String email); @Query(“select u from User u where u.id in (:ids)”) Set getByIdInSet(@Param(“ids”) Set ids); } 当我调用getByIdInSet我收到以下错误: Caused by: java.lang.IllegalArgumentException: You have attempted to set a value of type class org.eclipse.persistence.indirection.IndirectSet for parameter ids with expected type of class java.lang.Long from query string select u from User […]