【发布时间】:2018-06-03 18:11:57
【问题描述】:
我有一个与 H2 RDBMS 交互的 Spring Boot RESTful 服务。 我决定通过添加 MongoDB 支持来扩展它并为每个数据源创建两个配置文件。
我的意图是使用通用实体,所以我开始将id 字段从Long 更改为BigInteger,因为如果我将String 用作id,它将导致SQL 插入的性能问题。
@Document
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
@EqualsAndHashCode
@ToString
public final class Book {
@org.springframework.data.annotation.Id
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private BigInteger id;
private String title;
@ManyToOne(optional = false)
private Author author;
}
当然还有相应的存储库。
public interface BookRepository extends CrudRepository<Book, BigInteger> {}
我还修改了 H2 表,将 BIGINT 类型替换为 BIGDECIMAL 并重新填充了我的表。我的属性中有spring.jpa.hibernate.ddl-aeuto=validate,并且Hibernate 验证抱怨将java.math.BigInteger 与来自db 的BIGINT 匹配。
毕竟,我调试了我的应用程序,但属于存储库的所有方法都返回空结果,即使findAll(),但我看到记录显示在表中,"SELECT * from 'table'(e.g. BOOK)" 返回所有记录。
你能澄清一些问题吗?
将 JPA 和 MongoDB 的实体组合在一个类中是一种好方法吗? Spring Data 可以与 BigInteger 一起正常工作吗?有什么提示吗?
更新: 我试图从头开始,我发现在使用 @Document 注释 CRUD 存储库注释实体后开始行为不端并且在注释 @Query(native=true) 的方法上出现 org.springframework.data.mapping.PropertyReferenceException 失败
【问题讨论】:
标签: java spring mongodb jpa spring-data-jpa