Error calling save method using JSF Hibernate JPA An established connection has been overridden by the software on the host computer

Asked

Viewed 32 times

-1

I am trying to save the data from my form but when I click save of error saying that "An established connection was annulled by the software on the computer". But when I test the save method using Junit works norlmally . Someone might be helping me ?

These are my statements

That is the part of the entity

@Entity
@Table(name = "tb_login")
public class LoginEntity { // This the beginning of the class from the entity LoginEntity

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;

@Column(name = "name", nullable = false)
private String name;

@Column(name = "cpf", nullable = false, unique = true)
private String cpf;

@Column(name = "login", nullable = false)
private String login;

@Column(name = "password", nullable = false)
private String password;

// This the end of the class from the entity LoginEntity

This is the DAO part

public class LoginDAO {

public void save(LoginEntity loginEntity) {

    try {
        EntityManager entityManager = HibernateUtil.getEntityManager();
        EntityTransaction entityTransaction = entityManager.getTransaction();

        entityTransaction.begin();
        entityManager.persist(loginEntity);
        entityTransaction.commit();

        entityManager.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

This is Bean’s part

@ViewScoped
@ManagedBean(name = "MB_LoginBean")
public class LoginBean {

// here is the beginning of the atributes from the class LoginBean
private LoginEntity loginEntity = new LoginEntity();

private LoginDAO dao = new LoginDAO();

// here is the end of the attributes from the class LoginBean

public void save() {

    dao.save(loginEntity);

}

This is my test using Junit

@Test
public void salvar() {

    LoginDAO dao = new LoginDAO();
    LoginEntity loginEntity = new LoginEntity();

    loginEntity.setName("Teste salvar 5");
    loginEntity.setCpf("0175472");
    loginEntity.setPassword("145258");
    loginEntity.setLogin("admin5");

    dao.save(loginEntity);
}

}
  • I was able to resolve, I was using Sessionfactory for the database connection and using Entimanager commands, so I created my connection to the database using persistence.xml and used the Entitymanger commands and it worked all right .

1 answer

0

I was able to resolve, I was using Sessionfactory for the database connection and using Entimanager commands, so I created my connection to the database using persistence.xml and used the Entitymanger commands and it worked all right

Browser other questions tagged

You are not signed in. Login or sign up in order to post.