Olá galera do PJ, estou precisando da ajuda de vcs…
eu tenho o código abaixo, onde eu consigo listar, porém nao persisto, nem altero e nem excluo nenhum objeto. eu nao sei o que pode estar errado, mas pesquisando na net, eu vi que a propriedade do meu “ID” no hbm.xml talvez nao devesse ser <generator class=“increment”/>. alguem tem uma ideia do que pode ser, e se for esse o motivo, como faço para que meu ID seja incremental e automatico através do hibernate?
public class FachadaCliente {
EntityManagerFactory factory;
EntityManager manager;
public FachadaCliente() {
}
public void init() {
factory = Persistence.createEntityManagerFactory("myPU");
manager = factory.createEntityManager();
}
private void shutdown() {
manager.close();
factory.close();
}
public List listAll() {
EntityTransaction tx = null;
List<TipoClienteDO> results = null;
try {
init();
tx= manager.getTransaction();
tx.begin();
System.out.println("searching for people");
Query query = manager.createQuery("select t from TipoClienteDO t");
results = (List<TipoClienteDO>)query.getResultList();
tx.commit();
shutdown();
} catch (Exception ex) {
tx.rollback();
shutdown();
}
return results;
}
public TipoClienteDO getTipoClienteById(BigInteger id) {
EntityTransaction tx = null;
TipoClienteDO tc = null;
try {
init();
tx= manager.getTransaction();
tx.begin();
System.out.println("searching for people");
Query query = manager.createQuery("select t from TipoClienteDO t where id="+ id);
tc = (TipoClienteDO)query.getSingleResult();
tx.commit();
shutdown();
} catch (Exception ex) {
tx.rollback();
shutdown();
}
return tc;
}
public void save(TipoClienteDO tc) {
EntityTransaction tx = null;
try {
init();
tx= manager.getTransaction();
tx.begin();
manager.persist(tc);
tx.commit();
shutdown();
} catch (Exception ex) {
tx.rollback();
shutdown();
}
}
public void update(TipoClienteDO tc) {
EntityTransaction tx = null;
try {
init();
tx= manager.getTransaction();
tx.begin();
manager.merge(tc);
tx.commit();
shutdown();
} catch (Exception ex) {
tx.rollback();
shutdown();
}
}
public void delete(TipoClienteDO tc) {
EntityTransaction tx = null;
try {
init();
tx= manager.getTransaction();
tx.begin();
manager.remove(tc);
tx.commit();
shutdown();
} catch (Exception ex) {
tx.rollback();
shutdown();
}
}
}
meu mapeamento no hiberntate esta assim:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="br.com.hstar.model.cliente.TipoClienteDO" table="tipoCliente">
<id name="id" column="id" type="long">
<generator class="increment"/>
</id>
<property name="descricao" column="descricao" type="string"/>
</class>
</hibernate-mapping>
meu persistence.xml esta assim:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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_1_0.xsd">
<persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>br.com.hstar.model.cliente.TipoClienteDO</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/hstar"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.password" value=".pg12345."/>
<property name="hibernate.connection.username" value="postgres"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
o erro que está aparecendo quando eu olho a exceção é:
IllegalArgumentException: “Removing a detached instance br.com.hstar.model.cliente.TipoClienteDO#1”
alguem por favor poderia me dar ima solução??