0

I'm getting the following exception on a .save() call in my Spring boot Application. I'm running Spring boot 2.2.5.RELEASE

Stack trace was too big to be included here so I've shared it at this link.

Here's the code:

Search search = new Search();

// ......Build Search Object here

searchRepository.save(search);

public interface searchRepository
    extends BaseJpaRepository<Search, Long>, JpaSpecificationExecutor<Search> {

}

@Data
@Entity
@Table(name = "search")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Search extends AbstractAuditingEntity implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  @NotNull
  @Column(name = "externalId")
  private Long externalId;

  @NotNull
  @Column(name = "search_term")
  private String searchTerm;

  @NotNull
  @Column(name = "filters")
  private String filters;

  @NotNull
  @Column(name = "results")
  private Long results;

  @Override
  public int hashCode() {
    return id != null ? id.hashCode() : IntegerUtils.ANSWER_TO_LIFE_THE_UNIVERSE_AND_EVERYTHING;
  }
}

I have ensured through debug mode on my IDE that all @NotNull fields do not contain Null. I have also added the following missing values in application.yml :

jpa:
   hibernate:
       naming:
           implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
           physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

Have looked at multiple other answers on stackoverflow, but none have anything regarding this error. I recently upgraded my spring boot and this error has been showing up since then. Moreover I am able to read/write to spring session table, so I am assuming that this is not a configuration issue?

What am I doing wrong?

Ching Ling
  • 301
  • 1
  • 5
  • 14
  • if you decide explicitly implement `hashCode()`, I guess you should do the same and for the `equals(Object other)` (`Search` class). Could you please provide a **full** stack trace. Did you see [this](https://stackoverflow.com/questions/20089031/could-not-extract-resultset-in-hibernate)? – SternK Jul 15 '20 at 08:52
  • I guess you're right. But I don't believe that could be what's causing this issue. And yes I did look at that answer but could not find a solution there. That one has the `org.hibernate.exception.SQLGrammarException`, which is not what I have, so it's a different problem. Also updated the question with the full stack trace. – Ching Ling Jul 15 '20 at 15:35
  • I found the answer. I'll be posting it in a second so it might help anyone else running into the same issue. – Ching Ling Jul 15 '20 at 15:58

1 Answers1

0

The reason this stack error was being thrown was because by default my hibernate transactions were set to be readOnly = true. The class I was calling .save() method from had the annotation @Transactional, and the method did not have any annotation. So .save() would try to change the model and would fail. So I decided to put @Transactional(readOnly=false) for my method, since it was writing to the database, and it seemed to have fixed the issue.

Ching Ling
  • 301
  • 1
  • 5
  • 14