Hi Michael,
These are the Details:
I have a class named "AppAdmin" which is annotated with @NodeEntity.
I wanted to create a service ("InitModel "), that checks on server start-up if the DB contains an AppAdmin object, and if not, create one and persist it.
Code:
@Service
public class InitModel {
@Autowired
private AppAdminDao appAdminDao;
/**
* Create an AppAdmin, if there isn't such already
*/
@PostConstruct
private void initAppAdmin() {
AppAdmin admin = appAdminDao.getAppAdmin();
if (admin == null) {
admin = new AppAdmin("admin", "admin@gmail.com", "123456"); //(username, email, password)
admin.persist(); //this line throws NullPointerException
}
}
}
This is the getAppAdmin method:
Code:
@Autowired
private IAppAdminRepo appAdminRepo; //IAppAdminRepo is an interface that extends GraphRepository,RelationshipOperationsRepository
/**
*
* @return the appAdmin or null if there is no appAdmin
*/
public AppAdmin getAppAdmin() {
AppAdmin admin = null;
Collection<AppAdmin> admins = IteratorUtil.asCollection(appAdminRepo.findAll());
List<AppAdmin> adminsList = new ArrayList<AppAdmin>(admins);
if (adminsList.size() == 0) {
log.warn("The DB doesn't contain an App-Admin!");
return null;
} else {
admin = adminsList.get(0);
if (admins.size() > 1) {
log.error("The DB contains more than one App-Admin, using the first one (" + admin.getUsername() + ")");
}
}
return admin;
}
I also tried to add @DependsOn({ "neo4jNodeBacking", "transactionManager" } to InitModel service, but it didn't help.
When I changed the InitModel class to be a @Controller instead of @Service, everything worked well, I guess that's because the controllers are the last to be loaded.