Pessoal, estou iniciando no Hibernate, portanto encontrando algumas dificuldades.
no momento estou tentando persistir 2 objetos relacionados:
Dispositivo (n x 1) TipoDispositivo
@Entity
@SequenceGenerator(name = "disSeq", sequenceName = "dispositivo_id_dispositivo_seq")
@Table(name = "dispositivo")
public class Dispositivo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO , generator = "disSeq")
@Column(name = "id_dispositivo")
private int id;
@Column(name = "serial_disp")
private int serial;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH, CascadeType.PERSIST})
@Fetch(FetchMode.JOIN)
@JoinColumn(name = "fk_id_tipo_disp", insertable = true, updatable = true)
private TipoDispositivo tipoDispositivo;
//getters n setters
}
@Entity
@SequenceGenerator(name = "tipoDispSeq", sequenceName = "tipo_dispositivo_id_tipo_dispositivo_seq")
@Table(name = "tipo_dispositivo")
public class TipoDispositivo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "tipoDispSeq")
@Column(name = "id_tipo_dispositivo")
private int id;
@Column(name = "nome_tipo_disp")
private String nome;
@Column(name = "dsc_tipo_disp")
private String descricao;
@OneToMany(mappedBy = "tipoDispositivo" ,fetch = FetchType.LAZY , cascade = CascadeType.ALL)
private List<Dispositivo> dispositivos;
//getters n setters
}
ao executar esta retornando o seguinte stacktrace
.....
16:45:11 INFO [SessionFactoryImpl] building session factory
16:45:12 INFO [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
Hibernate: select nextval ('dispositivo_id_dispositivo_seq')
Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: uerj.ime.hhs.model.TipoDispositivo
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:243)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:456)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:265)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:275)
at org.hibernate.type.TypeHelper.findDirty(TypeHelper.java:295)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3378)
at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:520)
at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:230)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:154)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at Teste.main(Teste.java:54)
Realmente n sei o q fazer, recorro aos mestres =)