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.  [ 9 posts ] 
Author Message
 Post subject: Problems with object perisitence
PostPosted: Tue May 30, 2006 2:33 pm 
Beginner
Beginner

Joined: Sun May 07, 2006 4:41 pm
Posts: 20
Location: São Paulo - BRAZIL
Hi guys,
Always when i try persist one object returns it Exception:

Exception in thread "main" org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:27)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2062)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2427)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
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.dao.teste.GenericDAO.main(GenericDAO.java:44)



Tank's,

_________________
Paulo Nepomuceno
Java Developer


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 30, 2006 2:57 pm 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
Hi how is your java code?

how is your object and the mapping?

Obrigado...:)

_________________
Don´t forget to rate!


Top
 Profile  
 
 Post subject: Code and mappings
PostPosted: Tue May 30, 2006 3:53 pm 
Beginner
Beginner

Joined: Sun May 07, 2006 4:41 pm
Posts: 20
Location: São Paulo - BRAZIL
<?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 30/05/2006 12:48:10 by Hibernate Tools 3.1.0.beta5 -->
<hibernate-mapping>
<class name="br.com.shc.teste.Pessoa" table="PESSOA" schema="dbo" catalog="HIBERNATE">
<composite-id name="id" class="br.com.shc.teste.PessoaId">
<key-property name="id" type="int">
<column name="ID" />
</key-property>
<key-property name="cpf" type="string">
<column name="CPF" length="11" />
</key-property>
</composite-id>
<property name="nome" type="string">
<column name="NOME" length="20" />
</property>
<property name="sobrenome" type="string">
<column name="SOBRENOME" length="20" />
</property>
<property name="idade" type="string">
<column name="IDADE" length="3" />
</property>
</class>
</hibernate-mapping>


/////////////////////////////////////////////////////////////////////////////////////

public class Pessoa implements java.io.Serializable {

// Fields

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

private PessoaId id;

private String nome;

private String sobrenome;

private String idade;

// Constructors

/** default constructor */
public Pessoa() {
}

/** minimal constructor */
public Pessoa(PessoaId id) {
this.id = id;
}

/** full constructor */
public Pessoa(PessoaId id, String nome, String sobrenome, String idade) {
this.id = id;
this.nome = nome;
this.sobrenome = sobrenome;
this.idade = idade;
}

// Property accessors
public PessoaId getId() {
return this.id;
}

public void setId(PessoaId id) {
this.id = id;
}

public String getNome() {
return this.nome;
}

public void setNome(String nome) {
this.nome = nome;
}

public String getSobrenome() {
return this.sobrenome;
}

public void setSobrenome(String sobrenome) {
this.sobrenome = sobrenome;
}

public String getIdade() {
return this.idade;
}

public void setIdade(String idade) {
this.idade = idade;
}

}

////////////////////////////////////////////////////////////////////////////////////

public class PessoaId implements java.io.Serializable {

// Fields

/**
*
*/
private static final long serialVersionUID = -6329528343781045264L;

private int id;

private String cpf;

// Constructors

/** default constructor */
public PessoaId() {
}

/** full constructor */
public PessoaId(int id, String cpf) {
this.id = id;
this.cpf = cpf;
}

// Property accessors
public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}

public String getCpf() {
return this.cpf;
}

public void setCpf(String cpf) {
this.cpf = cpf;
}

public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof PessoaId))
return false;
PessoaId castOther = (PessoaId) other;

return (this.getId() == castOther.getId())
&& ((this.getCpf() == castOther.getCpf()) || (this.getCpf() != null
&& castOther.getCpf() != null && this.getCpf().equals(
castOther.getCpf())));
}

public int hashCode() {
int result = 17;

result = 37 * result + this.getId();
result = 37 * result
+ (getCpf() == null ? 0 : this.getCpf().hashCode());
return result;
}

}

/////////////////////////////////////////////////////////////////////////////////////

public class GenericDAO {

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
SessionFactory sessionFactory;
Session session = null;
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();

} catch (Exception e) {
// TODO: handle exception
}

//GRAVACAO
Transaction transaction = session.beginTransaction();


PessoaId pessoaId = new PessoaId();
pessoaId.setId(0);
pessoaId.setCpf("29391078826");
Pessoa pessoa = new Pessoa();
pessoa.setId(pessoaId);
pessoa.setIdade("24");
pessoa.setNome("PAULO");
pessoa.setSobrenome("NEPOMUCENO");

session.save(pessoa);
transaction.commit();
//session.flush();
//session.close();
}
}

===============================================
Thank you!

_________________
Paulo Nepomuceno
Java Developer


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 30, 2006 4:13 pm 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
Seems to me the problem is in the ID

you do this :

Code:
    pessoaId.setId(0);


how is this field in the database? you have to inform it? if not why do you put 0 actualy the initial value of a primitive int is 0.
Try not to set this value and see what happens I have an application that uses a very similar structure and it works...
Also try to avoid the primitive types It´s better to use the wrapper implementation... Integer

also you could try:

Code:
    session.saveOrUpdate(pessoa);


Boa Sorte....

_________________
Don´t forget to rate!


Top
 Profile  
 
 Post subject: Database Script
PostPosted: Tue May 30, 2006 4:26 pm 
Beginner
Beginner

Joined: Sun May 07, 2006 4:41 pm
Posts: 20
Location: São Paulo - BRAZIL
CREATE TABLE [dbo].[PESSOA] (
[ID] [int] NOT NULL ,
[CPF] [char] (11) COLLATE Latin1_General_CI_AS NOT NULL ,
[NOME] [char] (20) COLLATE Latin1_General_CI_AS NULL ,
[SOBRENOME] [char] (20) COLLATE Latin1_General_CI_AS NULL ,
[IDADE] [char] (3) COLLATE Latin1_General_CI_AS NULL
) ON [PRIMARY]
GO

I think with the problem is with the table has 2 primary keys... how i do handled this?

Thank you!

_________________
Paulo Nepomuceno
Java Developer


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 30, 2006 4:43 pm 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
I don´t see a problem in having a composite key....
I used it but in my case the id column was an Identity column generated by the database that way it worked for me....

Try to implement equals e hasCode in your class Pessoa as well.

I´ll test your examle to see what happens here...

_________________
Don´t forget to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 30, 2006 5:06 pm 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
I gave it a go with the following example :

Table:
Code:
create table tb_A (id integer not null, name varchar(50) not null, primary key (id, name))


MApping:

Code:
<hibernate-mapping>
   <class name="test.A" table="tb_A">

      <composite-id name="id" class="test.IdA">
         <key-property name="id" type="int" column="id" />

         <key-property name="name" type="java.lang.String"
            column="name" length="50" />

      </composite-id>


   </class>

</hibernate-mapping>


Class:
Code:
/**
*
* @hibernate.class
*       table = "tb_A"
*
*
*/
public class A {

   public IdA id;
   
   /**
    *
    * @hibernate.id type="test.IdA"
    *       
    */
   public IdA getId() {
      return id;
   }
   public void setId(IdA id) {
      this.id = id;
   }

   
}


IdClass:

Code:
public class IdA implements Serializable{
   private int id;
   private String name;
   
   public IdA(){
      
   }

   /**
    *
    * @hibernate.property type="int"
    *       column="id" not-null = "true"
    */
   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   /**
    *
    * @hibernate.property type="java.lang.String"
    *       column="name" length="50" not-null = "true"
    */
   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
   
   public boolean equals ( Object o ) {
      return false;
   }
   
   public int hasCode () {
      return 17 * this.id + this.name.hashCode();
   }
   
}


Code to save:

Code:
      IdA id = new IdA();
      id.setId(0);
      id.setName("ernst");
      A a = new A ();
      a.setId(id);
      
      session.save(a);
      
      session.flush();


This worked for me. I am using hibernate 3.1.3
Now as you can see I kept the implementations of equals and hashCode
extremely simple ...... maybe your problem is there.....

Good Luck

_________________
Don´t forget to rate!


Top
 Profile  
 
 Post subject: Problems
PostPosted: Wed May 31, 2006 8:40 am 
Beginner
Beginner

Joined: Sun May 07, 2006 4:41 pm
Posts: 20
Location: São Paulo - BRAZIL
now return this exception:
Exception in thread "main" org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): br.com.shc.teste.Pessoa


Ps.: All this code is generated for hibernate plugin for eclipse.

Please Help me...


Thank you so much!


package br.com.shc.teste;

// Generated 31/05/2006 09:05:30 by Hibernate Tools 3.1.0.beta5

/**
* Pessoa generated by hbm2java
*/
public class Pessoa implements java.io.Serializable {

// Fields

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

private PessoaId id;

private String nome;

private String sobrenome;

private String idade;

// Constructors

/** default constructor */
public Pessoa() {
}

/** minimal constructor */
public Pessoa(PessoaId id) {
this.id = id;
}

/** full constructor */
public Pessoa(PessoaId id, String nome, String sobrenome, String idade) {
this.id = id;
this.nome = nome;
this.sobrenome = sobrenome;
this.idade = idade;
}

// Property accessors
public PessoaId getId() {
return this.id;
}

public void setId(PessoaId id) {
this.id = id;
}

public String getNome() {
return this.nome;
}

public void setNome(String nome) {
this.nome = nome;
}

public String getSobrenome() {
return this.sobrenome;
}

public void setSobrenome(String sobrenome) {
this.sobrenome = sobrenome;
}

public String getIdade() {
return this.idade;
}

public void setIdade(String idade) {
this.idade = idade;
}

}

===========================================


package br.com.shc.teste;

// Generated 31/05/2006 09:05:30 by Hibernate Tools 3.1.0.beta5

/**
* PessoaId generated by hbm2java
*/
public class PessoaId implements java.io.Serializable {

// Fields

/**
*
*/
private static final long serialVersionUID = -2998517921816812043L;

private Integer id;

private String cpf;

// Constructors

/** default constructor */
public PessoaId() {
}

/** full constructor */
public PessoaId(Integer id, String cpf) {
this.id = id;
this.cpf = cpf;
}

// Property accessors
public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public String getCpf() {
return this.cpf;
}

public void setCpf(String cpf) {
this.cpf = cpf;
}

public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof PessoaId))
return false;
PessoaId castOther = (PessoaId) other;

return (this.getId().intValue() == castOther.getId().intValue())
&& ((this.getCpf() == castOther.getCpf()) || (this.getCpf() != null
&& castOther.getCpf() != null && this.getCpf().equals(
castOther.getCpf())));
}

public int hashCode() {
int result = 17;

result = 37 * result + this.getId();
result = 37 * result
+ (getCpf() == null ? 0 : this.getCpf().hashCode());
return result;
}

}
=============================================

<?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 31/05/2006 09:05:32 by Hibernate Tools 3.1.0.beta5 -->
<hibernate-mapping>
<class name="br.com.shc.teste.Pessoa" table="PESSOA" schema="dbo" catalog="HIBERNATE">
<composite-id name="id" class="br.com.shc.teste.PessoaId">
<key-property name="id" type="java.lang.Integer">
<column name="ID" />
</key-property>
<key-property name="cpf" type="string">
<column name="CPF" length="11" />
</key-property>
</composite-id>
<property name="nome" type="string">
<column name="NOME" length="20" />
</property>
<property name="sobrenome" type="string">
<column name="SOBRENOME" length="20" />
</property>
<property name="idade" type="string">
<column name="IDADE" length="3" />
</property>
</class>
</hibernate-mapping>

===============================================

public class GenericDAO {

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
SessionFactory sessionFactory;
Session session = null;
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();

} catch (Exception e) {
// TODO: handle exception
}

//GRAVACAO
Transaction transaction = session.beginTransaction();


//TABELA PESSOA
PessoaId pessoaId = new PessoaId();
//pessoaId.setId(new Integer(0));
pessoaId.setCpf("29391078826");
Pessoa pessoa = new Pessoa();
//pessoa.setId();
//pessoa.setCpf("29391078826");
pessoa.setIdade("24");
pessoa.setNome("PAULO");
pessoa.setSobrenome("NEPOMUCENO");

session.save(pessoa);

transaction.commit();
//session.flush();
//session.close();
}
}

_________________
Paulo Nepomuceno
Java Developer


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 8:54 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
It looks like you missed this:
Code:
pessoa.setId(pessoaId);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.