I’ve been working on a website and want to ask why the newly created records that I save to a database via Spring JPA don't appear in other queries within the server. The records themselves are saved just fine and do appear in the database itself but not when the server looks for them. Older records that were not made by the server do appear just fine.
For example, a new book is added with the ID of 601 and it simply will not appear in the results of a any API that is run on the server. Even when it is searched for specifically, the server returns a 404 Error.
I've consulted a number of articles on the matter including Spring's own documentation but I haven't been able to find an answer on my own. I'll go into the details of my attempts below.
Articles and Attempts
While I've consulted a large amount of material on the matter, I'll trim it down to only a few that I think are most relevant.
The the most promising suggestion that I received was to force a refresh of the Entity's cache and the most cited way to do this was with Entity Manager's em.refresh();
method. Judging by the documentation, it does sound like it should do the job quite well. Sadly, this is not a method available in standard JpaRepository libraries since JpaRepository is an abstraction over Entity Manager so I've been working to implement Entity Manager into my code.
The primary source I used was here sicne it provided a way to implement a refresh ability in any repository that needed it and I followed the code given in the answer very closely but it had no effect. It was a four year old answer so it might be possible that it's out of date. I can't quite tell.
I've tried a few different ideas but this one has had the most promise.
Code:
Entity
@Entity
public class Book {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
int bookID;
@NotEmpty
String title;
String isbn;
Integer series_part;
Integer edition;
int format;
int pages;
int authorID;
int publisherID;
int seriesID;
int genreID;
int languageID;
int copyright;
Service Implementation
@Service
public class BookServiceImpl implements BookService{
@Autowired BookRepository bookRepository;
@Override
public Book createBook(Book book) {
Book savedBook=bookRepository.save(book);
bookRepository.refresh(savedBook);
return savedBook;
}
}
Service Controller
@RestController
@RequestMapping(path = "/books")
@CrossOrigin(origins = "http://localhost:8080")
public class BookServiceController {
@Autowired BookService bookServiceImpl;
@PostMapping(path = "/create")
public ResponseEntity<Book> createBook(@RequestBody Book book) {
try {
Book newBook = bookServiceImpl.createBook(book);
return new ResponseEntity<>(newBook, HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
Get APIs for Book
@GetMapping(path = "/all")
@Transactional(readOnly = false)
public ResponseEntity<Object> getBooks() {
return new ResponseEntity<>(bookServicesImpl.getBooks(), HttpStatus.OK);
}
@GetMapping(path = "/ten")
public ResponseEntity<Object> getTenBooks() {
return new ResponseEntity<>(bookServicesImpl.getTenBooks(), HttpStatus.OK);
}
@GetMapping(path = "/specific/{id}")
public ResponseEntity<Object> getSpecificBook(@PathVariable("id") int id) {
return new ResponseEntity<>(bookServicesImpl.getSpecificBook(id), HttpStatus.OK);
}
@GetMapping(path = "/search/{term}")
@Transactional(readOnly = false)
public ResponseEntity<Object> searchForBook(@PathVariable("term") String term) {
return new ResponseEntity<>(bookServicesImpl.searchForBook(term), HttpStatus.OK);
}
Generic Repository that contains refresh method
I copied it quite faithfully (possibly too faithfully).
public class ElementRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID>
implements ElementRepository<T, ID> {
private final EntityManager entityManager;
public ElementRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
}
@Override
@Transactional
public void refresh(T t) {
entityManager.refresh(t);
}
These seemed like the most relevant classes to include but I'll include any others that might be needed.
I apologize for the frequency of questions but I fully admit to being a novice of JPA and a lot of this (including large sections of the documentation) goes over my head from time to time.
Thanks in advance for any help at all!