Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Conexão com o banco de dados -->
- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
- <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
- <property name="hibernate.connection.url" value="jdbc:mysql://localhost/loja" />
- <property name="hibernate.connection.user" value="root"/>
- <property name="hibernate.connection.password" value="root" />
- <!-- <property name="hibernate.hbm2ddl.auto" value="create" /> -->
- <property name="hibernate.hbm2ddl.auto" value="update"/>
- <!--Configuracoes de Debug-->
- <property name="hibernate.show_sql" value="true" />
- <property name="hibernate.format_sql" value="true" />
- <property name="use_sql_comments" value="true" />
- </properties>
- </persistence-unit>
- package com.exemplo.entidade;
- @Id
- @GeneratedValue(strategy=GenerationType.IDENTITY)
- private int id;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getNome() {
- return nome;
- }
- public void setNome(String nome) {
- this.nome = nome;
- }
- public int getIdade() {
- return idade;
- }
- public void setIdade(int idade) {
- this.idade = idade;
- }
- @Column
- private String nome;
- @Column
- private int idade;
- package com.exemplo.repositorio;
- EntityManagerFactory emf;
- EntityManager em;
- public RepositorioCliente() {
- emf = Persistence.createEntityManagerFactory("mohr") ;
- em = emf.createEntityManager();
- }
- public void salvar(Cliente cliente){
- em.getTransaction().begin();
- em.merge(cliente);
- em.getTransaction().commit();
- emf.close();
- }
- public void remover (Cliente c){
- em.getTransaction().begin();
- em.remove(c);
- em.getTransaction().commit();
- emf.close();
- }
- public List<Cliente> listarTodos(){
- em.getTransaction().begin();
- Query consulta = em.createQuery("select cliente from Cliente cliente");
- @SuppressWarnings("unchecked")
- List<Cliente> clientes = consulta.getResultList();
- em.getTransaction().commit();
- emf.close();
- return clientes;
- }
- package com.exemplo.testes;
- public static void main(String[] args) {
- RepositorioCliente repositorioCliente = new RepositorioCliente();
- Cliente cliente = new Cliente();
- cliente.setNome("Styles Stilinski");
- cliente.setIdade(18);
- repositorioCliente.salvar(cliente);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement