是否可以使用Spring Data创建只读存储库?
我有一些实体链接到视图和一些子实体,我想为其提供一些存储库,其中包含一些方法findAll(),findOne()以及一些带有@Query注释的方法.我想避免提供类似的方法save(…),delete(…)因为它们没有任何意义,可能会产生错误.
public interface ContactRepository extends JpaRepository<ContactModel, Integer>, JpaSpecificationExecutor<ContactModel> {
List<ContactModel> findContactByAddress_CityModel_Id(Integer cityId);
List<ContactModel> findContactByAddress_CityModel_Region_Id(Integer regionId);
// ... methods using @Query
// no need to save/flush/delete
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我想在JBoss中编写一个简单的servlet,它将在Spring bean上调用一个方法.目的是允许用户通过点击URL来启动内部作业.
在servlet中获取对Spring bean的引用的最简单方法是什么?
JBoss Web服务允许您使用@Resource注释将WebServiceContext注入服务类.有没有类似的可用于普通的servlet?解决这一特殊问题的网络服务将使用大锤来粉碎坚果.
我有一个Products在我的MongoDB数据库中调用的集合,它由IProductPrice我的Java代码中的接口表示.以下存储库声明会导致Spring Date查看该集合db.collection: Intelliprice.iProductPrice.
我希望它将其配置为db.collection: Intelliprice.Products使用外部配置而不是放置@Collection(..)注释IProductPrice.这可能吗?我怎样才能做到这一点?
public interface ProductsRepository extends
MongoRepository<IProductPrice, String> {
}
Run Code Online (Sandbox Code Playgroud) 我正在处理一个奇怪的问题,我正在进行集成测试,调用我的控制器从数据库中获取一个不存在的对象.
public Optional<T> get(Long id) {
try {
return Optional.ofNullable(repository.getOne(id));
} catch(EntityNotFoundException e) {
return Optional.empty();
}
}
Run Code Online (Sandbox Code Playgroud)
什么时候getOne(…)找不到什么东西,我一直在期待,EntityNotFoundException但实际上什么都没有.如果我检查我的结果,我可以看到我有一个空实体,其中有一个处理程序链接"扔EntityNotFoundException"但我们没有进入catch并且我返回一个可选的这个奇怪的实体.
我无法理解这种行为.
我在spring webmvc项目中使用spring-data-jpa.我在我的一个实体的存储库上使用查询创建时遇到了问题.您可以在下面看到我的实体,我的存储库和例外.
我的实体,
@Entity
@Table(schema = "mainschema")
@XmlRootElement
public class Municipalperson implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(nullable = false)
private Integer id;
@Basic(optional = false)
@Column(name = "municipal_id", nullable = false)
private Integer municipal_id;
@Basic(optional = false)
@Column(nullable = false, length = 60)
private String firstname;
public Municipalperson(Integer id, Integer municipal_id, String firstname) {
this.id = id;
this.municipal_id = municipal_id;
this.firstname = firstname;
}
public Integer …Run Code Online (Sandbox Code Playgroud) 我使用Spring Boot的Spring Data JPA启动程序(1.4.1).它包含Spring Data JPA 1.10.3.但是,我需要使用@DomainEvents此版本弹簧数据中尚不存在的注释.当我尝试添加最新版本的Spring Data JPA时,我的应用程序运行时会出错.
我的pom例子:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
...
</dependencies>
Run Code Online (Sandbox Code Playgroud)
当我尝试添加最新版本的Spring Data JPA时:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.11.6.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
Run Code Online (Sandbox Code Playgroud)
当我启动我的应用程序时,我得到了erorrs.像这样的错误:
Caused by: java.lang.NoSuchMethodException: org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_121]
at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_121]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
... 53 common frames omitted
Run Code Online (Sandbox Code Playgroud)
如何使用较新版本的Spring Data JPA?我需要@DomainEvents在我的应用程序中.谢谢!
spring dependency-management maven spring-data-jpa spring-boot
是否可以直接指定projection何时调用数据存储库方法?这是存储库代码 - 注意我不想通过REST公开它,而是希望能够从服务或控制器中调用它:
@RepositoryRestResource(exported = false)
public interface UsersRepository extends PagingAndSortingRepository<User, Long> {
@Query(value = "SELECT u FROM User u WHERE ....")
public Page<User> findEmployeeUsers(Pageable p);
}
Run Code Online (Sandbox Code Playgroud)
然后在控制器中我这样做:
@PreAuthorize(value = "hasRole('ROLE_ADMIN')")
@RequestMapping(value = "/users/employee")
public Page<User> listEmployees(Pageable pageable) {
return usersRepository.findEmployeeUsers(pageable);
}
Run Code Online (Sandbox Code Playgroud)
有没有什么方法来指定projection的findEmployeeUsers,当它被称为直接像上面的方法?
我意识到上面的代码对于某些人来说可能看起来很奇怪......可以通过REST公开存储库并将其放入@PreAuthorize存储库中.思想控制器是进行安全检查的最佳位置 - 测试更自然,更简单.
那么,可以projection以某种方式将事物传递到直接调用的存储库方法中吗?
我想创建一个规范,将一个用户对象的组ID与一个id列表相匹配.我正在考虑使用isMember(就像在代码中一样),但该方法不会采用该列表.
public static Specification<User> matchCompanyIdsList(final List<Long> groupIds){
return new Specification<User>() {
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder builder){
final Path<Group> group = root.<Group> get("group");
return builder.isMember(company.<Long>get("id"), companyIds);
}
};
}
Run Code Online (Sandbox Code Playgroud)
如果我不在,我怎么办呢?
我试图"真正"理解Spring Framework.我对Spring Core(DI)和Spring MVC有了一些了解.
对于数据部分,我现在专注于Spring Data JPA.据我所知,JPA是一个标准规范,有多个实现,Hibernate是着名的.
现在,当我启动Spring Data JPA时,我的印象是Spring Data JPA是JPA规范的独立实现.原来我错了.
如果我理解正确,Spring Data JPA是Spring提供的抽象层,它在内部使用其他JPA提供程序(示例Hibernate),所以通常它是这样的:
Application ---> Spring Data JPA --> Hiberate --> JDBC ----> DB
我的理解是否正确?如果是这样没有Spring Data JPA误导?它本身不是JPA提供者,它只是一个抽象层,它在其他层之上工作JPA provider.
我不确定我是否真的了解Spring框架,或者它是一个复杂的框架?
有人可以帮我理解吗?
我正在关注spring-data-rest指南使用REST访问JPA数据.当我发布一条新记录时,它会被插入(响应是201).这很好,但是有没有办法配置REST MVC代码来返回新创建的对象?我宁愿不必发送搜索请求来查找新实例.
spring ×8
spring-data ×7
java ×5
jpa ×5
criteria-api ×1
hibernate ×1
java-ee ×1
jboss ×1
maven ×1
mongodb ×1
repository ×1
rest ×1
servlets ×1
spring-boot ×1