Skip to content

Instantly share code, notes, and snippets.

View ThangLeQuoc's full-sized avatar
:octocat:
Keep smashing the keyboard

Thang Le Quoc ThangLeQuoc

:octocat:
Keep smashing the keyboard
View GitHub Profile
package com.thanglequoc.decorator.starbuzz;
public enum BeverageSize {
SMALL("Small"),
MEDIUM("Medium"),
LARGE("Large");
private String literal;
BeverageSize(String literal){
@ThangLeQuoc
ThangLeQuoc / cloudSettings
Created March 20, 2018 15:40
Visual Studio Code Settings Sync Gist
{"lastUpload":"2018-03-20T15:38:12.734Z","extensionVersion":"v2.9.0"}
@ThangLeQuoc
ThangLeQuoc / CriteriaAPI_CountResult.java
Created April 16, 2018 12:42
Count number of result from result list
{
CriteriaBuilder qb = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> cq = qb.createQuery(Long.class);
cq.select(qb.count(cq.from(MyEntity.class)));
cq.where(/*your stuff*/);
return entityManager.createQuery(cq).getSingleResult();
}
/*SELECT e
FROM Employee e
WHERE e.id IN (SELECT emp.id
FROM Project p JOIN p.employees emp
WHERE p.name = :project)
*/
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
Root<Employee> emp = c.from(Employee.class);
@ThangLeQuoc
ThangLeQuoc / CriteriaAPI_JoinNonRelationalTables.java
Created April 16, 2018 12:57
Criteria API - Join non relational tables
//SELECT a FROM A a, B b WHERE a.someField = b.otherField ORDER BY b.anotherField
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<A> crit = cb.createQuery(A.class);
Root<A> aRoot = crit.from(A.class);
Root<B> bRoot = crit.from(B.class);
aRoot.alias("a");
bRoot.alias("b");
crit.select(aRoot);
Something simple like this can be done using subqueries in the select clause:
select ((select sum(hours) from resource) +
(select sum(hours) from projects-time)
) as totalHours
For such a simple query as this, such a subselect is reasonable.
In some databases, you might have to add from dual for the query to compile.
If you want to output each individually:
@ThangLeQuoc
ThangLeQuoc / PredicateExample.java
Created May 11, 2018 06:21
Java 8 - Lambda Expresison
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
//www . ja va 2 s .c om
public class Main {
public static void main(String[] args) {
List<Student> employees = Arrays.asList(
new Student(1, 3, "John"),
@ThangLeQuoc
ThangLeQuoc / pom.xml
Created June 27, 2018 05:17
JUnit 5 - POM with Junit 5 dependencies that works
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.thanglequoc.junit5</groupId>
<artifactId>junit5</artifactId>
<version>0.0.1-SNAPSHOT</version>
@ThangLeQuoc
ThangLeQuoc / ReflectionMagic.java
Created October 18, 2018 07:10
Java Reflection
public void doReflectionMagic() {
Method method = ldapUserCache.getClass().getDeclaredMethod("initActiveUsers", Long.TYPE);
method.setAccessible(true);
method.invoke(ldapUserCache, 3600L);
}
@ThangLeQuoc
ThangLeQuoc / InCriteria.java
Created November 9, 2018 17:08
Spring JPA Criteria
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Employee> q = cb.createQuery(Employee.class);
Root<Employee> root = q.from(Employee.class);
q.select(root);
List<String> parentList = Arrays.asList(new String[]{"John", "Raj"});
Expression<String> parentExpression = root.get(Employee_.Parent);