-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Session.save() work fine, Session.update() Unknown entity ?
PostPosted: Wed Aug 30, 2006 6:55 am 
Newbie

Joined: Mon Jan 03, 2005 10:57 pm
Posts: 14
Hi ,


I'm working with Hibernate 3 with MBean in Jboss 4.0.4. and one incredible thinks happend.. in same code, same mapping.. i use session.save to create a registry this work fine, but after save im using a session.update to update one register and i'm receive the error:

- nested throwable: (org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=BCO-SIST-002/169, BranchQual=, localId=169] status=STATUS_NO_TRANSACTION; - nested throwable: (org.hibernate.MappingException: Unknown entity: br.com.bvsistemas.servicoteste.data.VUsuario))


Some one can help me about this ???

Thx


hibernate-service.xml


Code:
<server>
    <mbean code="org.jboss.hibernate.jmx.Hibernate"
           name="jboss.adminguide:name=BVServicoTesteFactory">
        <attribute name="DatasourceName">java:MysqlTeste</attribute>
        <attribute name="Dialect">
            org.hibernate.dialect.MySQLDialect
        </attribute>
        <attribute name="SessionFactoryName">
            java:/hibernate/BVServicoTesteFactory
        </attribute>
        <attribute name="CacheProviderClass">
            org.hibernate.cache.HashtableCacheProvider
        </attribute>
        <attribute name="ShowSqlEnabled">true</attribute>
        <attribute name="DefaultSchema">dbteste</attribute>
      <attribute name="MaxFetchDepth">8</attribute>
      <attribute name="JdbcBatchSize">1000</attribute>
      <attribute name="JdbcFetchSize">5000</attribute>
      <attribute name="ReflectionOptimizationEnabled">true</attribute>
      <attribute name="QueryCacheEnabled">true</attribute>
      <attribute name="SessionFactoryInterceptor">br.com.bvsistemas.framework.interceptor.AuditHibernateInterceptor</attribute>
    </mbean>
</server>


Mapping documents:
VUsuario.hbm.xml
Code:
<?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>
<!--
    Created by the Middlegen Hibernate plugin 2.2

    http://boss.bekk.no/boss/middlegen/
    http://www.hibernate.org/
-->

<class
    name="br.com.bvsistemas.servicoteste.data.VUsuario"
    table="tbusuario"
    lazy="false"
    schema="dbteste"
>

    <id
        name="cdUsuario"
        type="java.lang.Integer"
        column="CdUsuario"
    >
   
        <generator class="increment" />
    </id>

    <property
        name="nmUsuario"
        type="java.lang.String"
        column="NmUsuario"
        length="100"
    />

    <!-- Associations -->
 

</class>
</hibernate-mapping>



VUsuario.java
Code:
public class VUsuario extends BaseVUsuario implements IVUsuario,IAuditar{


/**
     *
     */
    private static final long serialVersionUID = 8354978809242555880L;

   
/*[CONSTRUCTOR MARKER BEGIN]*/
   public VUsuario () {
      super();
   }

   /**
    * Constructor for primary key
    */
   public VUsuario (java.lang.Integer cdUsuario) {
      super(cdUsuario);
   }

/*[CONSTRUCTOR MARKER END]*/

    /* (non-Javadoc)
     * @see br.com.bvsistemas.framework.auditoria.IAuditar#getId()
     */
    public String getId() {
        return super.getCdUsuario().toString();
    }
}


BaseVUsuario.java
Code:
public abstract class BaseVUsuario  implements Comparable, Serializable {

   public static String REF = "VUsuario";
   public static String PROP_NM_USUARIO = "nmUsuario";
   public static String PROP_CD_USUARIO = "cdUsuario";


   // constructors
   public BaseVUsuario () {
      initialize();
   }

   /**
    * Constructor for primary key
    */
   public BaseVUsuario (java.lang.Integer cdUsuario) {
      this.setCdUsuario(cdUsuario);
      initialize();
   }

   protected void initialize () {}



   private int hashCode = Integer.MIN_VALUE;

   // primary key
   private java.lang.Integer cdUsuario;

   // fields
   private java.lang.String nmUsuario;



   /**
    * Return the unique identifier of this class
     * @hibernate.id
     *  generator-class="increment"
     *  column="CdUsuario"
     */
   public java.lang.Integer getCdUsuario () {
      return cdUsuario;
   }

   /**
    * Set the unique identifier of this class
    * @param cdUsuario the new ID
    */
   public void setCdUsuario (java.lang.Integer cdUsuario) {
      this.cdUsuario = cdUsuario;
      this.hashCode = Integer.MIN_VALUE;
   }




   /**
    * Return the value associated with the column: NmUsuario
    */
   public java.lang.String getNmUsuario () {
      return nmUsuario;
   }

   /**
    * Set the value related to the column: NmUsuario
    * @param nmUsuario the NmUsuario value
    */
   public void setNmUsuario (java.lang.String nmUsuario) {
      this.nmUsuario = nmUsuario;
   }





   public boolean equals (Object obj) {
      if (null == obj) return false;
      if (!(obj instanceof br.com.bvsistemas.servicoteste.data.VUsuario)) return false;
      else {
         br.com.bvsistemas.servicoteste.data.VUsuario vUsuario = (br.com.bvsistemas.servicoteste.data.VUsuario) obj;
         if (null == this.getCdUsuario() || null == vUsuario.getCdUsuario()) return false;
         else return (this.getCdUsuario().equals(vUsuario.getCdUsuario()));
      }
   }

   public int hashCode () {
      if (Integer.MIN_VALUE == this.hashCode) {
         if (null == this.getCdUsuario()) return super.hashCode();
         else {
            String hashStr = this.getClass().getName() + ":" + this.getCdUsuario().hashCode();
            this.hashCode = hashStr.hashCode();
         }
      }
      return this.hashCode;
   }

   public int compareTo (Object obj) {
      if (obj.hashCode() > hashCode()) return 1;
      else if (obj.hashCode() < hashCode()) return -1;
      else return 0;
   }

   public String toString () {
      return super.toString();
   }


}


Code between sessionFactory.openSession() and session.close():
The Code test Save/Update
Code:
// The entity populate
VUsuario usuarioDTO = new VUsuario();
usuarioDTO.setCdUsuario(3389);
usuarioDTO.setNmUsuario("Nina");

// Get the session
final InitialContext ctx = new InitialContext();
final SessionFactory factory = (SessionFactory) ctx.lookup("java:/hibernate/BVServicoTesteFactory");
Session sessao =  factory.openSession();

sessao.save(usuarioDTO)

// Change the value to update other registry
usuarioDTO.setCdUsuario(3380);
usuarioDTO.setNmUsuario("Gustavo");
sessao.update(usuarioDTO);

sessao.flush();
sessao.close();



Full stack trace of any exception that occurs:
This is the error:
org.jboss.tm.JBossTransactionRolledbackLocalException: - nested throwable: (org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=BCO-SIST-002/169, BranchQual=, localId=169] status=STATUS_NO_TRANSACTION; - nested throwable: (org.hibernate.MappingException: Unknown entity: br.com.bvsistemas.servicoteste.data.VUsuario))
at org.jboss.ejb.plugins.TxInterceptorCMT.throwJBossException(TxInterceptorCMT.java:565)
at org.jboss.ejb.plugins.TxInterceptorCMT.endTransaction(TxInterceptorCMT.java:506)
at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:411)
at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:181)
at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:168)
at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:205)
at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:136)
at org.jboss.ejb.SessionContainer.internalInvoke(SessionContainer.java:648)
at org.jboss.ejb.Container.invoke(Container.java:954)
at org.jboss.ejb.plugins.local.BaseLocalProxyFactory.invoke(BaseLocalProxyFactory.java:430)
at org.jboss.ejb.plugins.local.StatelessSessionProxy.invoke(StatelessSessionProxy.java:103)
at $Proxy66.atualizarUsuario(Unknown Source)
... 48 more
Caused by: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=BCO-SIST-002/169, BranchQual=, localId=169] status=STATUS_NO_TRANSACTION; - nested throwable: (org.hibernate.MappingException: Unknown entity: br.com.bvsistemas.servicoteste.data.VUsuario)
at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:372)
at org.jboss.ejb.plugins.TxInterceptorCMT.endTransaction(TxInterceptorCMT.java:501)
... 58 more
Caused by: org.hibernate.MappingException: Unknown entity: br.com.bvsistemas.servicoteste.data.VUsuario
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:547)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:65)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:871)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:801)
at br.com.bvsistemas.framework.interceptor.AuditHibernateInterceptor.onFlushDirty(AuditHibernateInterceptor.java:108)
at org.hibernate.event.def.DefaultFlushEntityEventListener.invokeInterceptor(DefaultFlushEntityEventListener.java:325)
at org.hibernate.event.def.DefaultFlushEntityEventListener.handleInterception(DefaultFlushEntityEventListener.java:301)
at org.hibernate.event.def.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:241)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:121)
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:993)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:340)
at org.hibernate.transaction.CacheSynchronization.beforeCompletion(CacheSynchronization.java:59)
at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1491)
at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1110)
at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:324)
... 59 more

[/code]

Hibernate version:
3
JBoss version:
4.0.4
Name and version of the database you are using:
Sybase 12


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 9:26 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
O.K., let's have a look at your code:

> // The entity populate
> VUsuario usuarioDTO = new VUsuario();
> usuarioDTO.setCdUsuario(3389);
You needn't set CdUsuario, you have defined it as your id-attribute and told hibernate to generate it for you (<generator class="increment" />)
> usuarioDTO.setNmUsuario("Nina");
>
> // Get the session
> final InitialContext ctx = new InitialContext();
> final SessionFactory factory = (SessionFactory) ctx.lookup("java:/hibernate/BVServicoTesteFactory");
> Session sessao = factory.openSession();
>
> sessao.save(usuarioDTO)
O.K., so now you saved an object with id 3389.
>
> // Change the value to update other registry
> usuarioDTO.setCdUsuario(3380);
Now you change the id attribute of the object - you shouldn't do this! hibernate does identify objects by their id-attributes (that's why they're called id....), so from hibernate's view you got a new object!
> usuarioDTO.setNmUsuario("Gustavo");
> sessao.update(usuarioDTO);
Since the object has a new id you can't update it - there is no VUsuario with id 3380 in the database (you only stored one with 3389), so you should use save instead of update.
>
> sessao.flush();
> sessao.close();

Greets

piet


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 9:42 am 
Newbie

Joined: Mon Jan 03, 2005 10:57 pm
Posts: 14
Hi piet,

Thanks for reply, my comment about that is id 3380 exists in database

I'm change the teste code to simple update the register, and the same error is show.

Code:
// The entity populate
VUsuario usuarioDTO = new VUsuario();
usuarioDTO.setCdUsuario(3389);
usuarioDTO.setNmUsuario("Gustavo");

// Get the session
final InitialContext ctx = new InitialContext();
final SessionFactory factory = (SessionFactory) ctx.lookup("java:/hibernate/BVServicoTesteFactory");
Session sessao =  factory.openSession();

// Try Update
sessao.update(usuarioDTO)

sessao.flush();
sessao.close();


Code:
Caused by: org.hibernate.MappingException: Unknown entity: br.com.bvsistemas.servicoteste.data.VUsuario
   at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:547)
   at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:65)
   at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:871)
   at org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
   at org.hibernate.impl.SessionImpl.get(SessionImpl.java:801)
   at br.com.bvsistemas.framework.interceptor.AuditHibernateInterceptor.onFlushDirty(AuditHibernateInterceptor.java:108)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.invokeInterceptor(DefaultFlushEntityEventListener.java:325)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.handleInterception(DefaultFlushEntityEventListener.java:301)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:241)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:121)
   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:993)
   at br.com.bvsistemas.servicoteste.ejb.UsuarioBean.atualizarUsuario(UsuarioBean.java:377)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 10:34 am 
Beginner
Beginner

Joined: Wed Jun 21, 2006 10:08 am
Posts: 26
You never specify in your hibernate configurations to map VUsuario.

I don't even think your save went through originally.

_________________
- Jonathan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 12:53 pm 
Newbie

Joined: Mon Jan 03, 2005 10:57 pm
Posts: 14
huangjd,

I´m using hibernate in Jboss as MBean. you dont need to declare .hbm.xml file in config.. this is automatic loaded.

[]s


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 8:13 am 
Newbie

Joined: Mon Jan 03, 2005 10:57 pm
Posts: 14
Any people can help me ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 8:48 am 
Beginner
Beginner

Joined: Wed Jun 21, 2006 10:08 am
Posts: 26
I've never used Hibnerate as MBean before, but...

http://www.hibernate.org/66.html

Quote in reference to the MBean setup... wrote:
Of course, you will want to change the attributes above.

* MapResources: a list of the mapping files used, separated by commas.
........ etc . . ... .


gaquino wrote:
huangjd,
I´m using hibernate in Jboss as MBean. you dont need to declare .hbm.xml file in config.. this is automatic loaded.


Besides your error is even telling you the object isn't mapped.

_________________
- Jonathan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 12:41 pm 
Newbie

Joined: Mon Jan 03, 2005 10:57 pm
Posts: 14
Jonathan,

My object is mapped.. look the first post, in my test i Save and Update. the save run allrigth but the ´problem is that update ou saveOrUpdate does not run, when im try to do update the hibernate return the Unknow entity, but the Save work without problem.

[]s


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.