Empresa.java
package br.com.diego.db;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Empresa {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String razaoSocial;
private String nomeFantasia;
private String telefone;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNomeFantasia() {
return nomeFantasia;
}
public void setNomeFantasia(String nomeFantasia) {
this.nomeFantasia = nomeFantasia;
}
public String getRazaoSocial() {
return razaoSocial;
}
public void setRazaoSocial(String razaoSocial) {
this.razaoSocial = razaoSocial;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
}
Main.java
package br.com.diego.db;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class Main {
public static void main(String args[]) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("1");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Empresa e = new Empresa();
e.setRazaoSocial("Diego ME");
e.setNomeFantasia("Diego Informatica");
e.setTelefone("00 0000-0000");
em.persist(e);
tx.commit();
}
}
persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0" 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_1_0.xsd">
<persistence-unit name="1">
<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
<!-- All persistence classes must be listed -->
<class>br.com.diego.db.Chamados</class>
<class>br.com.diego.db.Empresa</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<!-- Provider-specific connection properties -->
<property name="toplink.jdbc.driver" value="org.postgresql.Driver" />
<property name="toplink.jdbc.url" value="jdbc:postgresql://127.0.0.1:5432/estudo" />
<property name="toplink.jdbc.user" value="estudo" />
<property name="toplink.jdbc.password" value="estudo" />
<!-- Provider-specific settings -->
<property name="toplink.logging.level" value="FINE" />
<!-- java2db properties -->
<property name="toplink.ddl-generation" value="create-tables" />
</properties>
</persistence-unit>
</persistence>
Primeiramente crie um Dinamic Web Project utilizando Eclipse
Coloquei as classes dentro de seus pacotes
e a xml persistence.xml dentro de META-INF
esta retornando um
ou seja a linha
EntityManagerFactory emf = Persistence.createEntityManagerFactory(“1”);
nao esta ok
alguem sabe aonde poderia estar o erro?
Abraço