@Reza
dia inisilisasi Set karena ada method:
public void addNota(Nota notaTambahan) {
notaTambahan. setCustomer( this);
nota.add(notaTambah an);
}
gimana dia mo nota.add() klo gak di inisialisasi :D
@Andri
bener kata reza di set lazy false aja, karena toh elo emang butuh Set<Nota>
untuk langsung diinisialisasi
Maap klo ada kata2 yang salah :D
Reza Alaudin <[EMAIL PROTECTED]> wrote:
Hi Andri,
sebelum coba aku jawab, aku mo nanya...
>>/ Class Customer
@Entity
@Table(name= "CUSTOMER" )
public class Customer implements Serializable {
private Long id;
...
private Set<Nota> nota = new HashSet<Nota> ();
>>
kenapa kok di kelas entitynya, field Nota diinstansiasi (new)
AFAIK kalo di kelas entity umumnya gak diinstansiasi..
mungkin gak nyambung seeh ma pertanyaannya...
LazyInit exception itu biasanya terjadi karena fitur lazy di Hibernate itu
default enable..
AFAIK gak disebabkan ma pemilihan Collections (Set,Bag,List,Map)
<CMIIW>
kalo mau disable fitur LazyInitnya pake
@LazyCollection(FALSE)
</CMIIW>
best regards,
Reza
----- Original Message ----
From: Andri Bratakusuma <[EMAIL PROTECTED]>
To: [email protected]
Sent: Tuesday, September 9, 2008 12:34:11 AM
Subject: [JUG-Indonesia] Hibernate LazyInitializationException JIKA
menggunakan Set - HashSet
Gw punya domain object Customer dan Nota, Nota
merupakan collection
attribute dari customer dengan tipe Set dan di implement menggunakan
HashSet. Kalo dalam jsp gw mencoba mengakses object nota dari object
customer, pasti selalu timbul exception ky gini :
LazyInitializationE xception: 19 - failed to lazily initialize a
collection of role: com.zeusware. avd.domain. Customer. nota, no session
or session was closed
...
Tapi kalo gw rubah tipe atribut nota dg Collection dan di implement dg
ArrayList (bag), object2x nota BISA di akses tanpa exception. Kenapa
ya?
Gw pake Spring (standard ada Dao dan Service nya), dan sessionnya gw
buka di view (osiv).
// Class Customer
@Entity
@Table(name= "CUSTOMER" )
public class Customer implements Serializable {
private Long id;
...
private Set<Nota> nota = new HashSet<Nota> ();
//kl pake bag bisa!
//private Collection<Nota> nota = new ArrayList<Nota> ();
...
public Customer() {}
...
/*
@OneToMany(cascade = {CascadeType. ALL}, mappedBy = "customer")
public Collection<Nota> getNota() {
return nota;
}
*/
@OneToMany(cascade = {CascadeType. ALL}, mappedBy = "customer")
public Set<Nota> getNota() {
return nota;
}
public void addNota(Nota notaTambahan) {
notaTambahan. setCustomer( this);
nota.add(notaTambah an);
}
/*
public void setNota(Collection< Nota> nota) {
this.nota = nota;
}
*/
public void setNota(Set< Nota> nota) {
this.nota = nota;
}
}
// Class Nota
@Entity
@Table(name= "NOTA")
public class Nota implements Serializable {
private Long id;
...
private Customer customer;
...
public Nota() {}
...
@ManyToOne(targetEn tity=com. zeusware. avd.domain. Customer. class)
@JoinColumn( name="CUSTOMER_ ID", nullable=false)
public Customer getCustomer( ) {
return customer;
}
public void setCustomer( Customer customer) {
this.customer = customer;
}
}
TIA
--
~Jabat Erat!
www.instanceof. net