Caros estou utilizando o JPA com Hibernate no netbeans 6.0:
//Classe principal
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testejpa;
import javax.persistence.*;
/**
*
* @author Arquiris
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
EntityManagerFactory factory;
EntityManager manager;
factory = Persistence.createEntityManagerFactory("testeJPAPU");
manager = factory.createEntityManager();
EntityTransaction tx = manager.getTransaction();
try {
tx.begin();
Fabricante fab = new Fabricante();
fab.setNmFabricante("Teste");
manager.persist(fab);
tx.commit();
System.out.println("Fabricante inserido com sucesso!");
}catch(Exception ex) {
tx.rollback();
System.out.println("Erro ao tentar inserir fabricante\nErro: " + ex.getMessage());
}
}
}
//Classe Fabricante
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testejpa;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
*
* @author Arquiris
*/
@Entity
@Table(name = "FABRICANTE")
@SequenceGenerator(name = "fabricante_seq", sequenceName = "seq_fabricante")
@NamedQueries({@NamedQuery(name = "Fabricante.findByCdFabricante", query = "SELECT f FROM Fabricante f WHERE f.cdFabricante = :cdFabricante"), @NamedQuery(name = "Fabricante.findByNmFabricante", query = "SELECT f FROM Fabricante f WHERE f.nmFabricante = :nmFabricante"), @NamedQuery(name = "Fabricante.findByDtCadastro", query = "SELECT f FROM Fabricante f WHERE f.dtCadastro = :dtCadastro")})
public class Fabricante implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="fabricante_seq")
@Column(name = "CD_FABRICANTE", nullable = false)
private Integer cdFabricante;
@Column(name = "NM_FABRICANTE")
private String nmFabricante;
@Column(name = "DT_CADASTRO")
@Temporal(TemporalType.DATE)
private Date dtCadastro;
public Fabricante() {
}
public Fabricante(Integer cdFabricante) {
this.cdFabricante = cdFabricante;
}
public Integer getCdFabricante() {
return cdFabricante;
}
public void setCdFabricante(Integer cdFabricante) {
this.cdFabricante = cdFabricante;
}
public String getNmFabricante() {
return nmFabricante;
}
public void setNmFabricante(String nmFabricante) {
this.nmFabricante = nmFabricante;
}
public Date getDtCadastro() {
return dtCadastro;
}
public void setDtCadastro(Date dtCadastro) {
this.dtCadastro = dtCadastro;
}
@Override
public int hashCode() {
int hash = 0;
hash += (cdFabricante != null ? cdFabricante.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Fabricante)) {
return false;
}
Fabricante other = (Fabricante) object;
if ((this.cdFabricante == null && other.cdFabricante != null) || (this.cdFabricante != null && !this.cdFabricante.equals(other.cdFabricante))) {
return false;
}
return true;
}
@Override
public String toString() {
return "testejpa.Fabricante[cdFabricante=" + cdFabricante + "]";
}
}
Ao executar, recebo a seguinte mensagem de erro:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Exception in thread “main” java.lang.IllegalStateException: Transaction not active
at org.hibernate.ejb.TransactionImpl.rollback(TransactionImpl.java:82)
O que devo fazer ?