0

No idea why this is not working !!

I'm trying to delete a credential from my database but the delete method is not helping ...

This is how I'm doing it:

So this is triggering when the user clicks a button in the page:

$("#credentialsTable").on('click',"button[id^='del-']",  (e) => {
    var credentialId = e.target.id;
        console.log('credId' + credentialId);
        $.post( "/fisicHost/" + credentialId + "/credentials", data => {
        console.log(data);
    });
});

The post is being handled by this controller's method:

@RestController
public class Controlador {

    @Autowired
    private FisicHostDao fisicHostDao;
    @Autowired
    private CredentialDao credentialDao;

    @RequestMapping(value = "/fisicHost/{id}/credentials", method = RequestMethod.POST)
    public String deleteCredential(@PathVariable(value = "id") String credId){
        String[] parts = credId.split("-");
        int id = Integer.parseInt(parts[1]);
        Credential c = credentialDao.getCredentialById(id);
        credentialDao.delete(c);
        return "justreturnsomething";
    }

}

This is the Credential class:

@Entity
public class Credential {

    @Id
    private int id;

    @JsonIgnore
    @ManyToOne(fetch= FetchType.EAGER)
    private FisicHost fisicHost;

    private String user;
    private String password;
    private String notes;
    private String role;

    public Credential(){

    }

    public Credential(int id, FisicHost fisicHost, String user, String password, String notes, String role) {
        this.id = id;
        this.fisicHost = fisicHost;
        this.user = user;
        this.password = password;
        this.notes = notes;
        this.role = role;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public FisicHost getFisicHost() {
        return fisicHost;
    }

    public void setFisicHost(FisicHost fisicHost) {
        this.fisicHost = fisicHost;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }
}

and this is the CredentialDao class:

@Repository public class CredentialDaoImpl implements CredentialDao {

@Autowired
private SessionFactory sessionFactory;

@Override
public List<Credential> getAllCredentialsByFisicHost(FisicHost fisicHost) {
    // Open a session
    Session session = sessionFactory.openSession();

    Criteria c = session.createCriteria(Credential.class).add(Restrictions.eq("fisicHost.id", fisicHost.getId()));

    List<Credential> allCredentials = c.list();

    // Close the session
    session.close();

    return allCredentials;
}

@Override
public Credential getCredentialByUser(String user) {
    Session session = sessionFactory.openSession();
    Credential credential = session.get(Credential.class, user);
    session.close();
    return credential;
}

@Override
public Credential getCredentialById(int id) {
    Session session = sessionFactory.openSession();
    Credential credential = session.get(Credential.class, id);
    session.close();
    return credential;
}

@Override
public void save(Credential credential) {
    Session session = sessionFactory.openSession();
    session.save(credential);
    session.close();
}

@Override
public void update(Credential credential) {
    Session session = sessionFactory.openSession();
    session.update(credential);
    session.close();
}

@Override
@Transactional
public void delete(Credential credential) {
    Session session = sessionFactory.openSession();
    session.delete(credential);
    session.close();
}

}

Ok, so I'm debuggin the program and I see that when I get to these lines in the controller:

    Credential c = credentialDao.getCredentialById(id);
    credentialDao.delete(c);

the credentials that are being loaded are the ones I want to, but the delete is not working ... the credential is not being erased from the DB.

This is a picture of the debugger:

enter image description here

I can see that the credentialDao sessionFactory is = null .... this is a bit weird, might this be the problem ?? If so, why is this a problem if the sessionFactory is annotated as @AutoWired in the CredentialDaoImpl class !

enter image description here

1 Answers1

0

If your sessionFactory is null and you are using @Autowiered annotation, that's means the spring context are not recognizing your dependence. To resolve, create a factory function that returns a new SessionFactory and include then as a Bean using @Bean annotation. That garantee spring have the dependence in the context. Ex:

@Bean
public HibernateJpaSessionFactoryBean sessionFactory() {
   return new HibernateJpaSessionFactoryBean();
}

Then you can use @Autowired to instanteate your object.

@Autowired
private SessionFactory sessionFactory;

Take a look on: https://stackoverflow.com/a/33881946/5192140. I think that mabe help you