jordan.baucke
jordan.baucke

Reputation: 4328

Spring Data JPA, findOne null, @Query returns record

I have a Spring Data JPA ORM implemented from the JHipster Generator w/ 2nd level cache using EHCache. (I think)

I have copied data from one database table to the other (directly in SQL using flyway):

CREATE TABLE lobby (LIKE command_center INCLUDING ALL);
INSERT INTO lobby SELECT * FROM command_center;

And created a new entity:

@Entity
@Table(name = "lobby")
@Document(indexName = "lobby")
public class Lobby {
      ...

Lobby lobby = lobbyRepository.findOne(id) (returns null even though the record is in the database).

However, if I create a query:

@Query("SELECT lobby FROM Lobby lobby WHERE id = ?1)
public getOneById(Long id)

Returns the record?

This may not be the case but I cannot think of anything else that might be causing this. If I create new Lobby records they are returned correctly by findOne

Also findAll returns all the records correctly.

Upvotes: 1

Views: 2868

Answers (2)

jordan.baucke
jordan.baucke

Reputation: 4328

I found the problem! When I copied the table, I had a @NotNull constraint on one of the Foreign Key relationships, but did not create corresponding records.

Upvotes: 1

Henri
Henri

Reputation: 5711

I don't see the entity as cacheable here (no annotation about that), so unless the query is cacheable, the cache can't be involved here.

And then, even if it was, to be involved, you would need to have queried the table, that would then return null and then add the entry through SQL to finally query again and get the result from the cache (null).

But this is not possible since you are filling the table at creation.

Bottom line, unless you have a higher level of cache, it's not the cache. And I don't have enough information to tell you what it is.

Upvotes: 1

Related Questions