1

This issues is around of an Spring MVC project where I indeed to use Hibernate with JPA. For an message is an entity class

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;
  }

}

To execute some simple queries is used a interface derived from 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);
}

By running project its obtain the error like

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

What is wrong with this interface and that rules must be respected in this type of interface?

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Mihai8
  • 3,113
  • 1
  • 21
  • 31

1 Answers1

0

Regarding save and delete, they are supported out of the box and already present in the interface:

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

Both defined on the CrudRepository interface which the JpaRepository extends.

And regarding update, well any update to the query after it has been fetched from database and put in the persistence context will be flushed to the database after the transaction is finished.

If you want to update explicitly then try this post: How do I update an entity using spring-data-jpa?

Update

And instead of this:

public StoredMessage findStoredMessageByMessageId(Long mesageId);

you would use the

StoredMessage findOne(long messageId)

also from the CrudRepository interface

And finally if you want to search for unique entity not by id but by some field, then use like this:

StoredMessage findOneStoredMessageByMessage(String message);
Community
  • 1
  • 1
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63