saudações…
estou com uma dúvida aki. Tenho 2 classes mapeadas, estão abaixo:
public class Cliente implements java . io . Serializable {
@Id
@GeneratedValue ( strategy = GenerationType . AUTO , generator = "INC_CLIENTE" )
private Integer idCliente ;
@Column
private String nome ;
@Column
private String site ;
@Column
private String cpfCnpj ;
@OneToMany ( mappedBy = "idCliente" )
private List < Telefone > telefones ;
e
public class Telefone implements java . io . Serializable {
@Id
@GeneratedValue ( strategy = GenerationType . AUTO , generator = "INC_TELEFONE" )
private Integer idTelefone ;
@ManyToOne ()
@JoinColumn ( name = "IDCLIENTE" )
private Cliente idCliente ;
@Column
private String telefone ;
tenho uma classe ClienteDao, e essa classe contém o seguinte método:
protected < T extends Serializable > T readById ( Integer id , Class < T > type ) {
Session session = getSession ();
Serializable toReturn = null ;
try {
toReturn = ( Serializable ) session . get ( type , id );
session . getTransaction () . commit ();
} catch ( Exception ex ) {
session . getTransaction () . rollback ();
} finally {
session . close ();
return ( T ) toReturn ;
}
}
quando vou recuperar um registro do banco de dados e tentar escrevê-lo na tela functiona tudo bem até a parte da lista de telefones. Me mostra um erro:
16 / 07 / 2010 15 : 14 : 26 or g . hibernate . LazyInitializationException
SEVERE : failed to lazily initialize a collection of role : scgp . model . pojo . Cliente . telefones , no session or session was close d
or g . hibernate . LazyInitializationException : failed to lazily initialize a collection of role : scgp . model . pojo . Cliente . telefones , no session or session was close d
at or g . hibernate . collection . Abs tractPersistentCollection . throwLazyInitializationException ( Abs tractPersistentCollection . java : 358 )
at or g . hibernate . collection . Abs tractPersistentCollection . throwLazyInitializationExceptionIfNotConnected ( Abs tractPersistentCollection . java : 350 )
at or g . hibernate . collection . Abs tractPersistentCollection . initialize ( Abs tractPersistentCollection . java : 343 )
at or g . hibernate . collection . Abs tractPersistentCollection . read ( Abs tractPersistentCollection . java : 86 )
at or g . hibernate . collection . PersistentBag . iterator ( PersistentBag . java : 249 )
at scgp . model . dao . ClienteDaoTest . testLerPorId ( ClienteDaoTest . java : 94 )
at sun . reflect . NativeMethodAccessorImpl . invoke0 ( Native Method )
at sun . reflect . NativeMethodAccessorImpl . invoke ( NativeMethodAccessorImpl . java : 39 )
at sun . reflect . DelegatingMethodAccessorImpl . invoke ( DelegatingMethodAccessorImpl . java : 25 )
at java . lang . reflect . Method . invoke ( Method . java : 597 )
at or g . junit . run ners . model . FrameworkMethod$ 1. run ReflectiveCall ( FrameworkMethod . java : 44 )
at or g . junit . int ernal . run ners . model . ReflectiveCallable . run ( ReflectiveCallable . java : 15 )
at or g . junit . run ners . model . FrameworkMethod . invokeExplosively ( FrameworkMethod . java : 41 )
at or g . junit . int ernal . run ners . statements . InvokeMethod . evaluate ( InvokeMethod . java : 20 )
at or g . junit . int ernal . run ners . statements . Run Befores . evaluate ( Run Befores . java : 28 )
at or g . junit . int ernal . run ners . statements . Run Afters . evaluate ( Run Afters . java : 31 )
at or g . junit . run ners . BlockJUnit4ClassRunner . run Child ( BlockJUnit4ClassRunner . java : 73 )
at or g . junit . run ners . BlockJUnit4ClassRunner . run Child ( BlockJUnit4ClassRunner . java : 46 )
at or g . junit . run ners . ParentRunner . run Children ( ParentRunner . java : 180 )
at or g . junit . run ners . ParentRunner . access$ 000 ( ParentRunner . java : 41 )
at or g . junit . run ners . ParentRunner$ 1. evaluate ( ParentRunner . java : 173 )
at or g . junit . int ernal . run ners . statements . Run Befores . evaluate ( Run Befores . java : 28 )
at or g . junit . int ernal . run ners . statements . Run Afters . evaluate ( Run Afters . java : 31 )
at or g . junit . run ners . ParentRunner . run ( ParentRunner . java : 220 )
at junit . framework . JUnit4TestAdapter . run ( JUnit4TestAdapter . java : 39 )
at or g . apache . to ols . ant . taskdefs . optional . junit . JUnitTestRunner . run ( JUnitTestRunner . java : 515 )
at or g . apache . to ols . ant . taskdefs . optional . junit . JUnitTestRunner . launch ( JUnitTestRunner . java : 1031 )
at or g . apache . to ols . ant . taskdefs . optional . junit . JUnitTestRunner . main ( JUnitTestRunner . java : 888 )
se eu tiro o session.close, fica tudo blza. Minha dúvida é sobre o session.close, quero dizer, eu preciso fechá-la não é? Como eu faço, não posso simplismente tirar essa linha. Alguém tem alguma idéia?