如何在查询方法中使用Spring Data中的'exists'关键字?
我想有这样的方法:
public interface ProfileRepository extends JpaRepository<Profile, Long> {
boolean existsByAttribute(String attribute);
}
Run Code Online (Sandbox Code Playgroud)
其中Attribute是Profile的字段.
假设我有一个类似的存储库:
public interface MyRepository extends PagingAndSortingRepository<MyEntity, String> {
@Query("....")
Page<MyEntity> findByCustomField(@Param("customField") String customField, Pageable pageable);
}
Run Code Online (Sandbox Code Playgroud)
这非常有效.但是,如果客户端发送已形成的请求(例如,搜索不存在的字段),则Spring将异常作为JSON返回.揭示@Query等等
// This is OK
http://example.com/data-rest/search/findByCustomField?customField=ABC
// This is also OK because "secondField" is a valid column and is mapped via the Query
http://example.com/data-rest/search/findByCustomField?customField=ABC&sort=secondField
// This throws an exception and sends the exception to the client
http://example.com/data-rest/search/findByCustomField?customField=ABC&sort=blahblah
Run Code Online (Sandbox Code Playgroud)
抛出并发送给客户端的异常示例:
{
message:null,
cause: {
message: 'org.hibernate.QueryException: could not resolve property: blahblah...'
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何处理这些例外情况?通常,我使用的@ExceptionHandler是我的MVC控制器,但我没有使用Data Rest API和客户端之间的层.我是不是该?
谢谢.
java exception-handling spring-data spring-data-jpa spring-data-rest
我有一个名为User的@Entity.它有一组变更集如下:
@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="user")
private Set<Changeset> changesets = new HashSet<Changeset>();
Run Code Online (Sandbox Code Playgroud)
我有一个UserRepository:
@Repository
@RestResource(path = "users", rel = "users")
public interface UserRepository extends JpaRepository<User, Long>{ }
Run Code Online (Sandbox Code Playgroud)
还有一个ChangesetRepository:
@Repository
@RestResource(path = "changesets", rel = "changesets")
public interface ChangesetRepository extends JpaRepository<Changeset, Long> { }
Run Code Online (Sandbox Code Playgroud)
调用GET http://localhost:8080/changesets/或http://localhost:8080/users/产生分页响应.
如果我打开GET,http://localhost:8080/users/1/changesets那么我将所有结果都放在一个数组中,并且不会发生分页.
有没有办法向Spring Data Rest表明我想通过其父用户访问它时以可分页的方式返回changesets集合?变更集集将快速增长,我宁愿不在单个页面中返回大量结果.
编辑:
正如Willie Wheeler所建议的那样,我将其添加到我的ChangesetRepository中以使其可搜索:
@RestResource(path = "byUser", rel = "byUser")
public Page<Changeset> findByUser(@Param("id") User user, Pageable p);
Run Code Online (Sandbox Code Playgroud)
我将关系保留为双向,但也可以通过使用更改集来隐藏用户更改集的链接@RestResource(exported=false).
旁注:似乎将关系设置为exported = false会隐藏链接,但实际上不会删除映射./ users/1/changesets未公布,但仍然有效.
spring hibernate spring-data spring-data-jpa spring-data-rest
我似乎无法在以下任何位置映射我的存储库:
@RepositoryRestResource(collectionResourceRel = "item", path = "item")
public interface ItemRepository extends PagingAndSortingRepository<Item, Long> {
Run Code Online (Sandbox Code Playgroud)
我以为我可以用:
path = "/some/other/path/item"
Run Code Online (Sandbox Code Playgroud)
但映射无法解决.我明白了:
HTTP ERROR 404
Problem accessing /some/other/path/item. Reason:
Not Found
Run Code Online (Sandbox Code Playgroud)
在spring-data中,javadoc path定义为:"用于导出此资源的路径段."
我究竟做错了什么?
我正在尝试使用PagingAndSortingRepository带有find MyEntity where field in fieldValues查询的spring ,如下所示:
@Repository
public interface MyEntity extends PagingAndSortingRepository<MyEntity, String> {
List<MyEntity> findByMyField(Set<String> myField);
}
Run Code Online (Sandbox Code Playgroud)
但没有成功.
我希望上面的函数返回其字段与其中一个字段值匹配的所有实体,但它只返回空结果.
即使它看起来像一个非常直接的能力,我在文档中找不到任何引用.
是/如何实现?
谢谢.
java spring spring-data spring-data-mongodb spring-data-commons
(我为这个问题做了一个SSCCE.)
我有2个简单的实体:Employee和Company.与默认获取策略(渴望)Employee有@ManyToOne关系Company.
我希望能够Employee在Company不更改的情况下加载the 而不更改在中定义的获取策略,Employee因为我只需要为一个用例执行此操作.
JPA的实体图似乎是为了这个目的.
所以我@NamedEntityGraph在课堂上定义了一个Employee:
@Entity
@NamedEntityGraph(name = "employeeOnly")
public class Employee {
@Id
private Integer id;
private String name;
private String surname;
@ManyToOne
private Company company;
//Getters & Setters
Run Code Online (Sandbox Code Playgroud)
和EmployeeRepository这样的:
public interface EmployeeRepository extends CrudRepository<Employee, Integer> {
@EntityGraph(value = "employeeOnly", type = EntityGraph.EntityGraphType.FETCH)
List<Employee> findByCompanyId(Integer companyId);
}
Run Code Online (Sandbox Code Playgroud)
尽管使用了@EntityGraph,我可以在日志中看到Company仍然由hibernate加载:
2016-11-07 23:16:08.738 …Run Code Online (Sandbox Code Playgroud) 我有一个使用Spring Boot 1.5.1和Spring Data Rest的数据库服务.我将我的实体存储在MySQL数据库中,并使用Spring的PagingAndSortingRepository通过REST访问它们.我发现这表明支持按嵌套参数排序,但我找不到按嵌套字段排序的方法.
我有这些课程:
@Entity(name = "Person")
@Table(name = "PERSON")
public class Person {
@ManyToOne
protected Address address;
@ManyToOne(targetEntity = Name.class, cascade = {
CascadeType.ALL
})
@JoinColumn(name = "NAME_PERSON_ID")
protected Name name;
@Id
protected Long id;
// Setter, getters, etc.
}
@Entity(name = "Name")
@Table(name = "NAME")
public class Name{
protected String firstName;
protected String lastName;
@Id
protected Long id;
// Setter, getters, etc.
}
Run Code Online (Sandbox Code Playgroud)
例如,使用该方法时:
Page<Person> findByAddress_Id(@Param("id") String id, Pageable pageable);
Run Code Online (Sandbox Code Playgroud)
并调用URI http:// localhost:8080/people/search/findByAddress_Id?id = …
我正在使用我的项目<version.org.aspecj>1.7.4</version.org.aspecj>和<vesrion.org.springframework>4.0.1.RELEASE</vesrion.org.springframework>版本.最近我将这两个版本升级到 <version.org.aspectj>1.8.9</version.org.aspectj>和
<version.org.springframework>4.3.11.RELEASE</version.org.springframework>.
我在我的项目中使用JPA存储库,当我尝试保存任何对象时,我收到的错误如"JTA EntityManager无法使用getTransaction()".
下面是我的persistence.xml和其他配置.请求您查看这些配置并分享您的输入.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="appPersistenceUnit"
transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/appDS</jta-data-source>
<!-- entities -->
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="jboss.entity.manager.factory.jndi.name"
value="persistence-units/fusePersistenceUnit"/>
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
<property name="hibernate.current_session_context_class"
value="jta"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.generate_statistics" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
<property name="hibernate.jdbc.batch_size" value="40"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.search.default.directory_provider" value="filesystem" />
<property name="hibernate.connection.url" …Run Code Online (Sandbox Code Playgroud) 此问题出现在Spring-Data发行版2中.在最新版本1.13.9(及更早版本)中,它运行正常.
控制器代码:
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
@RequestMapping(value = "sorttest", method = RequestMethod.GET)
public Page<Integer> getDummy() {
return new PageImpl<>(Collections.singletonList(1), new PageRequest(0, 5, new Sort("asdf")), 1);
}
}
Run Code Online (Sandbox Code Playgroud)
Spring-Data 2风格相同:
Pageable pageable = PageRequest.of(0, 10, new Sort(Sort.Direction.ASC, "asd"));
PageImpl<Integer> page = new PageImpl<Integer>(Lists.newArrayList(1,2,3), pageable, 3);
return page;
Run Code Online (Sandbox Code Playgroud)
组态:
@SpringBootApplication
@EnableWebMvc
@EnableSpringDataWebSupport
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
还尝试了简单的Spring应用程序,没有带有Java配置的Spring Boot以及XML配置.结果是一样的:
{
"content": …Run Code Online (Sandbox Code Playgroud) 我应该如何构建我的findBy方法名称,以便我可以实现where子句 -
statusCode != 'Denied'
Run Code Online (Sandbox Code Playgroud)
这是一个选择吗?
findByStatusCodeNotIn(List<String> statusCode);
Run Code Online (Sandbox Code Playgroud)
如果我只想传递一个String而不是一个列表怎么办?
谢谢