query cek beberapa kolom untuk nilai tertentu

ada table pengawas, terdiri dari kolom pengawas1 dan pengawas2, bagaimana query untuk pencari pengawas yang ada di kolom pengawas1 dan pengawas2 ?

 

Move :
http://www.saifiahmada.com/2016/07/query-cek-beberapa-kolom-untuk-nilai.html


http://www.saifiahmada.com/2016/08/download-video-playlist-youtube.html

http://www.saifiahmada.com/2016/08/belajar-perintah-mysql-mariadb-command.html

http://www.saifiahmada.com/2016/08/panduan-menulis-blog.html

http://www.saifiahmada.com/2016/08/cara-download-youtube-menggunakan.html

http://www.saifiahmada.com/2016/08/menulis-coding-java-pada-github-gist.html

 

squirrel sql error codecompletion

Ops, ketika buka squirrelsql kok muncul error

Bismillah

Move : http://www.saifiahmada.com/2016/06/squirrel-sql-error-codecompletion.html
http://www.saifiahmada.com/2016/07/query-cek-beberapa-kolom-untuk-nilai.html


http://www.saifiahmada.com/2016/08/download-video-playlist-youtube.html

http://www.saifiahmada.com/2016/08/belajar-perintah-mysql-mariadb-command.html

http://www.saifiahmada.com/2016/08/panduan-menulis-blog.html

http://www.saifiahmada.com/2016/08/cara-download-youtube-menggunakan.html

http://www.saifiahmada.com/2016/08/menulis-coding-java-pada-github-gist.html

 

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

Error LazyInitializationException 🙂

Bismillah

q> Ini coding header


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;
}
}

q> Ini coding detail


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 + "]";
}

}

q> Ketika coba mengambil data dengan coding ini


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());
}
}

}
}

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

@OneToMany(cascade={CascadeType.ALL}, fetch = FetchType.EAGER)

q> Sehingga coding untuk OneToMany pada header seperti ini :

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

q> Berhasil mengambil seluruh header beserta detailnya 🙂

Alhamdulillah

@Secured pada controller spring boot

Bagaiamana sebuah controller pada aplikasi spring boot hanya bisa diakases oleh user yang mempunyai ROLE admin ?

Bismillah

misalnya kita mempunyai sebuah controller seperti ini :

@Controller
@RequestMapping("/kontak")
@Secured("ROLE_ADMIN")
public class KontakController {
@RequestMapping("/save")
public void save(){
}
}

Maka untuk mengakses controller tersebut hanya user-user yang mempunyai role admin saja yang bisa mengaksesnya, selain user tersebut maka akan muncul error Access is denied

Catatan :
aplikasi menggunakan spring boot, spring data jpa, spring security, thymeleaf dan database mariadb

Alhamdulillah

@lob menghasilkan longtext mysql

Ternyata @lob menghasilkan longtext pada mysql

Bismillah

Test coding ini 🙂

private static final long serialVersionUID = 1L;
@Id @GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(length=64)
private String id;

@Lob
private String teks;

private String pilihanA;

private String pilihanB;

private String pilihanC;

private String pilihanD;

private String pilihanE;
@Column(length=1)
private String jawaban;
@Column(length=25)
private String tipe;

q> Ketika di-build ( / + running code)Menghasilkan table seperti ini :

testcode

Alhamdulillah

update data mysql menggunakan limit

bismillah

update kontak set koordinator = 'saidi11' where koordinator = 'saidi' order by id asc limit 50

query di atas akan melakukan update data sebanyak 50 baris, field yang di-update adalah koordinator

select count(id), koordinator from kontak where koordinator like '%saidi%' group by koordinator

query di atas akan menampilkan data grouping berdasarkan field koordinator

query1

alhamdulillah