Olá amigos, estou com dificuldade ao persistir um objeto no meu banco. Quando vou salvar algo (save) dá erro de TransientObjectException. Eu já usei Hibernate com vraptor e com struts, mas nunca tinha usado ele pra aplicação desktop (alias nunca tinha programado em desktop), por isto acho que tô comendo bola com algo simples que não consegui enxergar. Eu tenho uma tabela chamada driver e outra chamada aplicação, onde na aplicação eu tenho um relacionamento com driver. Ou seja, toda a aplicação terá um driver. Falando em classes de entidade, as fiz assim:
Biblioteca
@Entity
@Table(name="driver")
public class BibliotecaVO {
@Id @GeneratedValue
private Integer id;
@Column(length=40)
private String name;
@Column(length=50)
private String dialect;
@Column(length=80)
private String driver;
private String filepath;
@Column(length=20)
private String protocol;
@Column(length=1)
private String reservada;
...
}
@Entity
@Table(name="aplicacao")
public class AplicacaoVO {
@Id @GeneratedValue
private Integer id;
@Column(length=40)
private String nome;
private String url;
@Column(length=20)
private String usuario;
@Column(length=20)
private String senha;
private String descricao;
@ManyToOne(fetch= FetchType.EAGER)
private BibliotecaVO driver;
...
}
Até aqui, aparentemente está tudo certo… Pelo menos criou certo o meu banco de dados… Tenho uma classe abstrata que será pai das classes de acessoa à dados, segue:
public class AbstractDAO<T> {
protected Class<T> classVO = null;
protected Session session;
public AbstractDAO(Session session) {
this.session = session;
this.classVO = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public void add(T t) throws Exception {
Transaction trans = null;
try {
trans = this.session.beginTransaction();
this.session.save(t);
this.session.flush();
trans.commit();
} catch(Exception e) {
if (trans != null)
trans.rollback();
throw e;
}
}
public void update(T t) throws Exception {
Transaction trans = null;
try {
trans = this.session.beginTransaction();
this.session.update(t);
this.session.flush();
trans.commit();
} catch(Exception e) {
if (trans != null)
trans.rollback();
throw e;
}
}
public void del(T t) throws Exception {
Transaction trans = null;
try {
trans = this.session.beginTransaction();
this.session.delete(t);
this.session.flush();
trans.commit();
} catch(Exception e) {
if (trans != null)
trans.rollback();
throw e;
}
}
public List<T> findAll() {
return this.session.createCriteria(this.classVO.getName()).list();
}
}
E por fim, a classe ApicacaoDAO:
public class AplicacaoDAO extends AbstractDAO<AplicacaoVO> {
public AplicacaoDAO() {
super(HibernateUtil.getSession());
}
}
Blz… Agora eu tenho uma tela que tem um JComboBox populado por uma lista de bibliotecas e alguns campos para preencher. Eu pego, alimento um objeto (vo) do tipo AplicacaoVO com os dados da tela e tento persistir este cara, só que aí dá o erro abaixo:
AplicacaoDAO dao = new AplicacaoDAO();
dao.add(vo);
Erro
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: valueObjects.BibliotecaVO
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:242)
at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:597)
...
O que estou fazendo de errado ?
Obrigado