Hi guys,
When make persist a object occours following exeption:
thank you so much!
==============================================
org.hibernate.TransientObjectException:
br.com.shc.database.unigeo.UniGeo
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:108)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:221)
at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:476)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:2803)
at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:467)
at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:190)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:113)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:195)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at br.com.shc.delegate.unidadegeografica.UnidadeGeograficaBD.cadastrar(UnidadeGeograficaBD.java:65)
at br.com.shc.teste.TesteGravaUniGeo.main(TesteGravaUniGeo.java:25)
===============================================
//POJO
public class UniGeo implements java.io.Serializable {
// Fields
/**
*
*/
private static final long serialVersionUID = -1248449563826131893L;
private Integer pkUniGeo;
private Date timestamp;
private UniGeo uniGeo;
private Integer fkTpUniGeo;
private String descricao;
private Set<UniGeo> uniGeos = new HashSet<UniGeo>(0);
// Constructors
/** default constructor */
public UniGeo() {
}
/** minimal constructor */
public UniGeo(Integer fkTpUniGeo, String descricao) {
this.fkTpUniGeo = fkTpUniGeo;
this.descricao = descricao;
}
/** full constructor */
public UniGeo(UniGeo uniGeo, Integer fkTpUniGeo, String descricao,
Set<UniGeo> uniGeos) {
this.uniGeo = uniGeo;
this.fkTpUniGeo = fkTpUniGeo;
this.descricao = descricao;
this.uniGeos = uniGeos;
}
// Property accessors
public Integer getPkUniGeo() {
return this.pkUniGeo;
}
public void setPkUniGeo(Integer pkUniGeo) {
this.pkUniGeo = pkUniGeo;
}
public Date getTimestamp() {
return this.timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public UniGeo getUniGeo() {
return this.uniGeo;
}
public void setUniGeo(UniGeo uniGeo) {
this.uniGeo = uniGeo;
}
public Integer getFkTpUniGeo() {
return this.fkTpUniGeo;
}
public void setFkTpUniGeo(Integer fkTpUniGeo) {
this.fkTpUniGeo = fkTpUniGeo;
}
public String getDescricao() {
return this.descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public Set<UniGeo> getUniGeos() {
return this.uniGeos;
}
public void setUniGeos(Set<UniGeo> uniGeos) {
this.uniGeos = uniGeos;
}
public void addUniGeos(UniGeo uniGeo)
{
this.uniGeos.add(uniGeo);
}
}
===============================================
//MAPPING
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 16/06/2006 16:49:27 by Hibernate Tools 3.1.0.beta5 -->
<hibernate-mapping>
<class name="br.com.shc.database.unigeo.UniGeo" table="UniGeo" schema="dbo" catalog="HIBERNATE">
<id name="pkUniGeo" type="java.lang.Integer" unsaved-value="0">
<column name="PkUniGeo"/>
<generator class="native"></generator>
</id>
<version name="timestamp" type="java.util.Date">
<column name="Timestamp" length="23" not-null="true" />
</version>
<many-to-one name="uniGeo" class="br.com.shc.database.unigeo.UniGeo" fetch="select">
<column name="FkSubord" />
</many-to-one>
<property name="fkTpUniGeo" type="java.lang.Integer">
<column name="FkTpUniGeo" not-null="true" />
</property>
<property name="descricao" type="java.lang.String">
<column name="Descricao" length="200" not-null="true" unique="true" />
</property>
<set name="uniGeos" inverse="true">
<key>
<column name="FkSubord" />
</key>
<one-to-many class="br.com.shc.database.unigeo.UniGeo" />
</set>
</class>
</hibernate-mapping>
===============================================
//Main
public class TesteGravaUniGeo
{
public static void main(String[] args)
{
UnidadeGeograficaBD unidadeGeograficaBD = new UnidadeGeograficaBD();
//VO
UniGeoVO uniGeoVO = new UniGeoVO();
uniGeoVO.setDescricao("Jandira");
uniGeoVO.setFkTpUniGeo(new Integer(3));
uniGeoVO.setPkUniGeo(new Integer(0));
UniGeoVO uniGeoVOSubord = new UniGeoVO();
uniGeoVOSubord.setPkUniGeo(new Integer(12));
uniGeoVO.setUniGeo(uniGeoVOSubord);
try {
unidadeGeograficaBD.cadastrar(uniGeoVO);
} catch (SHCException e) {
System.out.println("DEU ERRO: " + e.getMessage());
e.printStackTrace();
}
}
}
===============================================
//Delegate class
public class UnidadeGeograficaBD implements CRUD
{
@SuppressWarnings("unused")
private UniGeoVO uniGeoVOReq = new UniGeoVO();
@SuppressWarnings("unchecked")
public Object cadastrar(Object uniGeoVO) throws SHCException
{
//REQUISICAO
this.uniGeoVOReq = (UniGeoVO) uniGeoVO;
UniGeo uniGeoReq = new UniGeo();
//RESPOSTA
UniGeoVO uniGeoVOResp = new UniGeoVO();
UniGeo uniGeoResp = null;
try
{
//COPIA O VO EM UM OBJETO PERSISTENTE
//BeanUtils.copyProperties(uniGeoReq, this.uniGeoVOReq);
uniGeoReq.setDescricao(this.uniGeoVOReq.getDescricao());
uniGeoReq.setFkTpUniGeo(this.uniGeoVOReq.getFkTpUniGeo());
uniGeoReq.setPkUniGeo(this.uniGeoVOReq.getPkUniGeo());
uniGeoReq.setTimestamp(new Date());
//SE HOUVER, SETA VALORES DO SUBORDINANTE
if(this.uniGeoVOReq.getUniGeo() != null)
{
UniGeo uniGeoSubordinanteReq = new UniGeo();
uniGeoSubordinanteReq.setPkUniGeo(this.uniGeoVOReq.getUniGeo().getPkUniGeo());
uniGeoReq.setUniGeo(uniGeoSubordinanteReq);
}
//INICIA SESSAO COM O HIBERNATE
Session session = SessionManager.getSession();
//INICIA TRANSAÇÃO
Transaction transaction = session.beginTransaction();
//INICIA REUISICAO COM A CAMADA DE INTEGRACAO
GenericSHCDAO genericDAO = new GenericSHCDAO(UniGeo.class, session);
uniGeoResp = (UniGeo) genericDAO.saveOrUpdate(uniGeoReq);
//COMMIT
transaction.commit();
}
catch (HibernateException e)
{
e.printStackTrace();
throw new SHCException(e.getMessage());
}
//RETORNA RESULTADO
return uniGeoVOResp;
}
}
_________________ Paulo Nepomuceno
Java Developer
|