Hi,
I'm using Hibernate 3.2.2ga with Annotations and, when I try to persist an object, I get the following error:
Code:
Exception in thread "AWT-EventQueue-0" javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): presenca.Aluno
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:629)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:218)
Ok, I've read a lot of posts about this kind of problem, saying that they have Ids that require to be manual assigned but they are not providing it. Well, that's not my case, because I'm setting it before calling persist(). My ID (PK) column in database is the RA column. As one can see, it is being set prior to persist() by setRA() method.
My table description (mySql DB)
Code:
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| RA | int(11) | NO | PRI | | |
| Nome | varchar(50) | NO | | | |
| Email | varchar(80) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
My entity class:
Code:
package presenca;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
* Entity class Aluno
*
* @author Bruno
*/
@Entity
@Table(name = "aluno")
@NamedQueries( {
@NamedQuery(name = "Aluno.findByRa", query = "SELECT a FROM Aluno a WHERE a.ra = :ra"),
@NamedQuery(name = "Aluno.findByNome", query = "SELECT a FROM Aluno a WHERE a.nome = :nome"),
@NamedQuery(name = "Aluno.findByEmail", query = "SELECT a FROM Aluno a WHERE a.email = :email")
})
public class Aluno implements Serializable {
@Id
@Column(name = "RA", nullable = false, insertable = true)
private Integer ra;
@Column(name = "Nome", nullable = false)
private String nome;
@Column(name = "Email")
private String email;
/** Creates a new instance of Aluno */
public Aluno() {
}
/**
* Creates a new instance of Aluno with the specified values.
* @param ra the ra of the Aluno
*/
public Aluno(Integer ra) {
this.ra = ra;
}
/**
* Creates a new instance of Aluno with the specified values.
* @param ra the ra of the Aluno
* @param nome the nome of the Aluno
*/
public Aluno(Integer ra, String nome) {
this.ra = ra;
this.nome = nome;
}
/**
* Gets the ra of this Aluno.
* @return the ra
*/
public Integer getRa() {
return this.ra;
}
/**
* Sets the ra of this Aluno to the specified value.
* @param ra the new ra
*/
public void setRa(Integer ra) {
this.ra = ra;
}
/**
* Gets the nome of this Aluno.
* @return the nome
*/
public String getNome() {
return this.nome;
}
/**
* Sets the nome of this Aluno to the specified value.
* @param nome the new nome
*/
public void setNome(String nome) {
this.nome = nome;
}
/**
* Gets the email of this Aluno.
* @return the email
*/
public String getEmail() {
return this.email;
}
/**
* Sets the email of this Aluno to the specified value.
* @param email the new email
*/
public void setEmail(String email) {
this.email = email;
}
/**
* Returns a hash code value for the object. This implementation computes
* a hash code value based on the id fields in this object.
* @return a hash code value for this object.
*/
@Override
public int hashCode() {
int hash = 0;
hash += (this.ra != null ? this.ra.hashCode() : 0);
return hash;
}
/**
* Determines whether another object is equal to this Aluno. The result is
* <code>true</code> if and only if the argument is not null and is a Aluno object that
* has the same id field values as this object.
* @param object the reference object with which to compare
* @return <code>true</code> if this object is the same as the argument;
* <code>false</code> otherwise.
*/
@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 Aluno)) {
return false;
}
Aluno other = (Aluno)object;
if (this.ra != other.ra && (this.ra == null || !this.ra.equals(other.ra))) return false;
return true;
}
/**
* Returns a string representation of the object. This implementation constructs
* that representation based on the id fields.
* @return a string representation of the object.
*/
@Override
public String toString() {
return "presenca.Aluno[ra=" + ra + "]";
}
}
And my calling method:
Code:
private void criaNovoAluno() {
initEntityManager();
em.getTransaction().begin();
Aluno novoAluno = new Aluno();
novoAluno.setRa(Integer.getInteger(jTextFieldRA.getText()));
novoAluno.setNome(jTextFieldNome.getText());
novoAluno.setEmail(jTextFieldEmail.getText());
em.persist(novoAluno); // THIS LINE THROWS THE EXCEPTION
em.getTransaction().commit();
}
I'm a newbie on JPA, so I might be doing something wrong,, but I just can't tell what. I believe the entity class is defined correctly, since it was generated by NetBeans 5.5. Any help is appreciate.
Regards,
Bruno.