Bom, estou criando um crud de clientes utilizando o servlet mais não está sendo criado o banco o que pode está acontecendo. Vou deixar to codigo abaixo:
Codigo:
@Entity
@Table(name = "clients")
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nome;
private Login login;
public Client(String nome, Login login) {
this.nome = nome;
this.login = login;
}
public Long getId() {
return id;
}
public String getNome() {
return nome;
}
public Login getLogin() {
return login;
}
}
public class Login {
private String email;
private String senha;
public Login(String email, String senha) {
super();
this.email = email;
this.senha = senha;
}
public String getEmail() {
return email;
}
public String getSenha() {
return senha;
}
}
public class ClientRepository {
public void cadastrarCliente(Client client) throws SQLException {
EntityManager manager = JPAUtil.getEntityManager();
EntityTransaction transaction = manager.getTransaction();
try {
transaction.begin();
manager.persist(client);
transaction.commit();
} catch (Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
throw new SQLException(e);
} finally {
JPAUtil.closeEntityManager();
}
}
}
public class ClientService {
private ClientRepository repository = new ClientRepository();
public void cadatrarCliente(Client client) throws SQLException {
String nome = client.getNome();
String email = client.getLogin().getEmail();
String senha = client.getLogin().getSenha();
String senhaHash = BCrypt.hashpw(senha, BCrypt.gensalt());
Login login = new Login(email, senhaHash);
Client clientCadastrado = new Client(nome, login);
repository.cadastrarCliente(clientCadastrado);
}
}
public class CadastrarClientesBean implements TipoAcao {
private HttpServletRequest req;
private HttpServletResponse resp;
public CadastrarClientesBean(HttpServletRequest req, HttpServletResponse resp) {
super();
this.req = req;
this.resp = resp;
}
@Override
public void execute() throws ServletException, IOException, SQLException {
String nome = this.req.getParameter("nome");
String email = this.req.getParameter("email");
String senha = this.req.getParameter("senha");
Login login = new Login(email, senha);
Client client = new Client(nome, login);
ClientService service = new ClientService();
service.cadatrarCliente(client);
}
}
public interface TipoAcao {
void execute() throws ServletException, IOException, SQLException ;
}
@WebServlet(urlPatterns = "/client")
public class ClienteController extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String action = req.getParameter("acao");
String fqn = "br.com.client.bean." + Character.toUpperCase(action.charAt(0)) + action.substring(1);
try {
Class<?> classe = Class.forName(fqn);
Constructor<?> constructor = classe.getDeclaredConstructor(HttpServletRequest.class,
HttpServletResponse.class);
TipoAcao tipoAcao = (TipoAcao) constructor.newInstance(req, resp);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException
| IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new ServletException(e);
}
}
}
public class JPAUtil {
private static final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("Client");
public static EntityManager getEntityManager() {
return entityManagerFactory.createEntityManager();
}
public static void closeEntityManager() {
entityManagerFactory.close();
}
}