Hibernate:
select
nextval ('hibernate_sequence')
Hibernate:
insert
into
Conta
(agencia, banco, numero, titular, id)
values
(?, ?, ?, ?, ?)
package br.com.marlon.teste;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import br.com.marlon.financas.Conta;
public class TesteConta {
public static void main(String[] args) {
Conta conta = new Conta();
conta.setTitular("Marlon Mazzine");
conta.setBanco("NuBank");
conta.setAgencia("0001");
conta.setNumero("12345678");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("contas-postgres");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(conta);
em.getTransaction().commit();
em.close();
emf.close();
}
}
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="contas-postgres">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>br.com.marlon.financas.Conta</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/conta" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="1234" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
package br.com.marlon.financas;
import javax.persistence.*;
@Entity
public class Conta {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String titular;
private String numero;
private String banco;
private String agencia;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitular() {
return titular;
}
public void setTitular(String titular) {
this.titular = titular;
}
public String getNumero() {
return numero;
}
public void setNumero(String numero) {
this.numero = numero;
}
public String getBanco() {
return banco;
}
public void setBanco(String banco) {
this.banco = banco;
}
public String getAgencia() {
return agencia;
}
public void setAgencia(String agencia) {
this.agencia = agencia;
}
}