org.hibernate.LazyInitializationException could not initialize proxy - no Session | java php laravel linux mysql sql bootstrap html css query java php laravel linux mysql sql bootstrap html css query: org.hibernate.LazyInitializationException could not initialize proxy - no Session

Friday, June 10, 2016

org.hibernate.LazyInitializationException could not initialize proxy - no Session

Error LazyInitializationException :)

Bismillah

q> Ini coding header


[sourcecode language="java"]

package com.saifiahmada.spring.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;

@Entity
public class HdrTest implements Serializable {

private static final long serialVersionUID = 1L;

@Id @GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "HDR_ID" , length=36)
private String hdrId;

@Temporal(TemporalType.TIMESTAMP)
private Date tglTest;

@Column(length = 20)
private String username;

@OneToMany(cascade={CascadeType.ALL})
@JoinColumn(name="hdr_id")
private List<DtlTest> dtlTests;

public String getHdrId() {
return hdrId;
}

public void setHdrId(String hdrId) {
this.hdrId = hdrId;
}

public Date getTglTest() {
return tglTest;
}

public void setTglTest(Date tglTest) {
this.tglTest = tglTest;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public List<DtlTest> getDtlTests() {
return dtlTests;
}

public void setDtlTests(List<DtlTest> dtlTests) {
this.dtlTests = dtlTests;
}
}[/sourcecode]


q> Ini coding detail


[sourcecode language="java"]

package com.saifiahmada.spring.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import org.hibernate.annotations.GenericGenerator;

@Entity
public class DtlTest implements Serializable {

private static final long serialVersionUID = 1L;

@Id @GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "DTL_ID" , length=36)
private String dtlId;

@Column(name="ID_SOAL", length=36)
private String idSoal;

@Column(length=1)
private String jawabanSiswa;

@ManyToOne
@JoinColumn(name="hdr_id")
private HdrTest hdrTest;

public DtlTest() {

}

public String getDtlId() {
return dtlId;
}

public void setDtlId(String dtlId) {
this.dtlId = dtlId;
}

public String getIdSoal() {
return idSoal;
}

public void setIdSoal(String idSoal) {
this.idSoal = idSoal;
}

public String getJawabanSiswa() {
return jawabanSiswa;
}

public void setJawabanSiswa(String jawabanSiswa) {
this.jawabanSiswa = jawabanSiswa;
}

public HdrTest getHdrTest() {
return hdrTest;
}

public void setHdrTest(HdrTest hdrTest) {
this.hdrTest = hdrTest;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dtlId == null) ? 0 : dtlId.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DtlTest other = (DtlTest) obj;
if (dtlId == null) {
if (other.dtlId != null)
return false;
} else if (!dtlId.equals(other.dtlId))
return false;
return true;
}

@Override
public String toString() {
return "DtlTest [dtlId=" + dtlId + "]";
}

}[/sourcecode]


q> Ketika coba mengambil data dengan coding ini


[sourcecode language="java"]

package com.saifiahmada.spring;

import java.util.List;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ApplicationContext;

import com.saifiahmada.spring.domain.DtlTest;
import com.saifiahmada.spring.domain.HdrTest;
import com.saifiahmada.spring.repository.HdrTestRepository;

@Configurable
@EnableAutoConfiguration
public class OfflineTest {

public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(OfflineTest.class, args);

HdrTestRepository repo = (HdrTestRepository) ctx.getBean("hdrTestRepository");

List<HdrTest> list = repo.findAll();
for (HdrTest hdr : list) {
System.out.println("" + hdr.getHdrId());
System.out.println("" + hdr.getUsername());
System.out.println("" + hdr.getTglTest());
for (DtlTest dtl : hdr.getDtlTests()) {
System.out.println("" + dtl.getDtlId());
System.out.println("" + dtl.getIdSoal());
System.out.println("" + dtl.getJawabanSiswa());
}
}

}
}[/sourcecode]


q> Muncul error ini
Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.saifiahmada.spring.domain.HdrTest.dtlTests, could not initialize proxy - no Session

q> Coba kita tambahkan code ini pada header


[sourcecode language="java"]@OneToMany(cascade={CascadeType.ALL}, fetch = FetchType.EAGER)[/sourcecode]


q> Sehingga coding untuk OneToMany pada header seperti ini :


[sourcecode language="java"]@OneToMany(cascade={CascadeType.ALL}, fetch = FetchType.EAGER)
@JoinColumn(name="hdr_id")
private List<DtlTest> dtlTests;[/sourcecode]


q> Berhasil mengambil seluruh header beserta detailnya :)

Alhamdulillah

No comments:

Post a Comment

saifiahmada.com adalah blog belajar programming Indonesia, membahas lengkap materi bahasa pemrograman: code HTML, CSS, Bootstrap, Desain, PHP, MySQL, coding Java, Query, SQL, dan dunia linux