来自JpaRepository接口的查询

时间:2017-01-24 20:07:54

标签: hibernate spring-data-jpa java-ee-7

这个问题是一个Spring MVC项目,我确实将Hibernate与JPA一起使用。 对于消息是实体类

import java.io.Serializable;
import java.time.LocalDate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "StoredMessage")
public class StoredMessage implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long messageId;
    @Column(nullable = false)
    private Long userId;
    @Column(nullable = false)
    private String message;
    @Column(nullable = false)
    private LocalDate date;
    @ManyToOne
    private User user;

    public StoredMessage() {
       super();
    }

   public StoredMessage(Long messageId, Long userId, String message, LocalDate date) {
      super();
      this.messageId = messageId;
      this.userId = userId;
      this.message = message;
      this.date = date;
  }

  public Long getMessageId() {
      return messageId;
  }

  public void setId(Long messageId) {
      this.messageId = messageId;
  }

  public Long getUserId() {
      return userId;
  }

  public void setUserId(Long userId) {
      this.userId = userId;
  }

  public String getMessage() {
      return message;
  }

  public void setMessage(String message) {
      this.message = message;
  }

  public LocalDate getDate() {
      return date;
  }

  public void setDate(LocalDate date) {
      this.date = date;
  }

}

要执行一些简单查询,请使用从JpaRepository派生的接口。

import org.springframework.data.jpa.repository.JpaRepository;

import free.oauth.model.StoredMessage;

public interface MessageRepository extends JpaRepository<StoredMessage,  Long> {

    public void saveStoredMessage(StoredMessage message);

    public void delete(Long MessageId);

    public boolean findByMessageId(Long mesageId);

    public StoredMessage findStoredMessageByMessageId(Long mesageId);

    public boolean update(StoredMessage mesage);
}

通过运行项目,获取错误,如

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property saveStoredMessage found for type StoredMessage!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76)
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:235)
at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373)
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:353)
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:84)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:63)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:103)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:214)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:77)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:435)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:220)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:266)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:252)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 56 more

此界面有什么问题,在这种类型的界面中必须遵守规则?

1 个答案:

答案 0 :(得分:0)

关于保存和删除,它们是开箱即用的,并且已经存在于界面中:

void delete(T entity)
<S extends T> S save(S entity)

两者都在JpaRepository扩展的CrudRepository接口上定义。

关于更新,在从数据库中提取并放入持久化上下文后,对查询的任何更新都将在事务完成后刷新到数据库。

如果您想明确更新,请尝试以下帖子: How do I update an entity using spring-data-jpa?

<强>更新

而不是这个:

public StoredMessage findStoredMessageByMessageId(Long mesageId);

你会使用

StoredMessage findOne(long messageId)

也来自CrudRepository接口

最后,如果你想通过id而不是某个字段搜索唯一的实体,那么请使用如下:

StoredMessage findOneStoredMessageByMessage(String message);