The working solution is available here. @Component
was missing in the entity.
Obviously, beans shouldn't been injected like done here, but instantiated (eg marriot = new Hotel("Marriot", 5, true);
) and persisted through the save
method (or saveAll
for a Collection)
The injection of entities for the sole purpose of initialisation is wrong and won't work:
The same instance will be reused for each Hotel.
@Autowired
private Hotel marriot, ibis, goldenTulip;
@Override
public void run(String... strings) throws Exception {
marriot.setName("Marriot");
marriot.setClassification(5);
marriot.setOpen(true);
ibis.setName("Ibis");
ibis.setClassification(3);
ibis.setOpen(false);
goldenTulip.setName("Golden Tulip");
goldenTulip.setClassification(4);
goldenTulip.setOpen(true);
List<Hotel> hotels = new ArrayList<>();
hotels.add(marriot);
hotels.add(ibis);
hotels.add(goldenTulip);
this.hotelRepository.saveAll(hotels);
}
Will result in one entity persisted, as all the 3 hotels are the same instance. As such, http://localhost:8080/hotels will return:
[{"id":1,"name":"Golden Tulip","classification":4,"open":true}]
While it with instantiation,
@Override
public void run(String... strings) throws Exception {
marriot = new Hotel("Marriot", 5, true);
ibis = new Hotel("Ibis", 3, false);
goldenTulip = new Hotel("Golden Tulip", 4, true);
List<Hotel> hotels = new ArrayList<>();
hotels.add(marriot);
hotels.add(ibis);
hotels.add(goldenTulip);
this.hotelRepository.saveAll(hotels);
}
it will return as it should the 3 entities:
[{"id":1,"name":"Marriot","classification":5,"open":true},{"id":2,"name":"Ibis","classification":3,"open":false},{"id":3,"name":"Golden Tulip","classification":4,"open":true}]
That's exactly what I wanted to see here, but forgot adding @Component
to the Entity class. Must not do that!
EDIT:
The reason for trying that was the use of a service layer:
public interface NoteService {
List<Note> loadAll();
}
@Service
public class NoteServiceImpl implements NoteService {
@Autowired
private NoteRepository noteRepository;
@Override
public List<Note> loadAll() {
return noteRepository.findAll();
}
}
Which ended up failing at runtime, Note
being not a managed bean.