Skip to content

Commit 12a9222

Browse files
committed
initial commit
0 parents  commit 12a9222

29 files changed

+1032
-0
lines changed

README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# java-cli-buildr-hibernate-sqlserver-case-count
2+
3+
## Description
4+
Creates a small database table
5+
called `dog` and populates with
6+
hql. Creates 2 lookup tables `breedLookup`
7+
and `colorLookup` and 4N `dog`.
8+
9+
Joins `breedLookup` and `colorLookup`
10+
to form a new table `dogextended`. These
11+
case-counts are deministrated 3 ways, `ResultTransformer`,
12+
looking up ids on `breedLookup` and `colorLookup`,
13+
and non-ResultTransformer method.
14+
15+
Add an immutable entity `breedcount` as a case-count uses a subquery join.
16+
17+
Updated an immutable entity `breedcount` as a view using a subselect with
18+
case and join.
19+
20+
## Tech stack
21+
- java
22+
- buildr
23+
- hibernate
24+
- hql
25+
- log4j
26+
- sql server driver
27+
28+
## Docker stack
29+
- vanto/apache-buildr:latest-jruby-jdk8
30+
- mcr.microsoft.com/mssql/server:2017-latest-ubuntu
31+
32+
## To run
33+
`sudo ./install.sh -u`
34+
35+
## To stop
36+
`sudo ./install.sh -d`
37+
38+
## For help
39+
`sudo ./install.sh -h`
40+
41+
## Credit
42+
- [ResultTransformer code based on](https://thorben-janssen.com/hibernate-resulttransformer/)
43+
- [HQL code based on](https://www.journaldev.com/2954/hibernate-query-language-hql-example-tutorial)
44+
- [Hibernate config based on](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/An-example-hibernatecfgxml-for-MySQL-8-and-Hibernate-5)
45+
- [Hibernate code based on](https://github.com/lokeshgupta1981/hibernate/tree/master/hibernate-hello-world)

db/Dockerfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM mcr.microsoft.com/mssql/server:2017-latest-ubuntu
2+
3+
ENV MSSQL_SA_PASSWORD z!oBx1ab
4+
5+
ENV ACCEPT_EULA Y
6+
7+
EXPOSE 1433

docker-compose.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '3'
2+
services:
3+
java-srv:
4+
build:
5+
context: java-srv
6+
depends_on:
7+
- db
8+
links:
9+
- "db:db"
10+
11+
db:
12+
build:
13+
context: db

general.log

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[2022-07-28 22:04:48 INFO]: install::setup-logging ended
2+
================
3+
[2022-07-28 22:04:48 INFO]: install::start-up started
4+
[2022-07-28 22:04:48 INFO]: install::start-up starting services
5+
[2022-07-28 22:04:48 INFO]: install::start-up ended
6+
================

install.sh

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/usr/bin/env bash
2+
basefile="install"
3+
logfile="general.log"
4+
timestamp=`date '+%Y-%m-%d %H:%M:%S'`
5+
6+
if [ "$#" -ne 1 ]; then
7+
msg="[ERROR]: $basefile failed to receive enough args"
8+
echo "$msg"
9+
echo "$msg" >> $logfile
10+
exit 1
11+
fi
12+
13+
function setup-logging(){
14+
scope="setup-logging"
15+
info_base="[$timestamp INFO]: $basefile::$scope"
16+
17+
echo "$info_base started" >> $logfile
18+
19+
echo "$info_base removing old logs" >> $logfile
20+
21+
rm -f $logfile
22+
23+
echo "$info_base ended" >> $logfile
24+
25+
echo "================" >> $logfile
26+
}
27+
28+
function root-check(){
29+
scope="root-check"
30+
info_base="[$timestamp INFO]: $basefile::$scope"
31+
32+
echo "$info_base started" >> $logfile
33+
34+
#Make sure the script is running as root.
35+
if [ "$UID" -ne "0" ]; then
36+
echo "[$timestamp ERROR]: $basefile::$scope you must be root to run $0" >> $logfile
37+
echo "==================" >> $logfile
38+
echo "You must be root to run $0. Try the following"
39+
echo "sudo $0"
40+
exit 1
41+
fi
42+
43+
echo "$info_base ended" >> $logfile
44+
echo "================" >> $logfile
45+
}
46+
47+
function docker-check() {
48+
scope="docker-check"
49+
info_base="[$timestamp INFO]: $basefile::$scope"
50+
cmd=`docker -v`
51+
52+
echo "$info_base started" >> $logfile
53+
54+
if [ -z "$cmd" ]; then
55+
echo "$info_base docker not installed"
56+
echo "$info_base docker not installed" >> $logfile
57+
fi
58+
59+
echo "$info_base ended" >> $logfile
60+
echo "================" >> $logfile
61+
62+
}
63+
64+
function docker-compose-check() {
65+
scope="docker-compose-check"
66+
info_base="[$timestamp INFO]: $basefile::$scope"
67+
cmd=`docker-compose -v`
68+
69+
echo "$info_base started" >> $logfile
70+
71+
if [ -z "$cmd" ]; then
72+
echo "$info_base docker-compose not installed"
73+
echo "$info_base docker-compose not installed" >> $logfile
74+
fi
75+
76+
echo "$info_base ended" >> $logfile
77+
echo "================" >> $logfile
78+
79+
}
80+
function usage() {
81+
echo ""
82+
echo "Usage: "
83+
echo ""
84+
echo "-u: start."
85+
echo "-d: tear down."
86+
echo "-h: Display this help and exit."
87+
echo ""
88+
}
89+
function start-up(){
90+
91+
local scope="start-up"
92+
local docker_img_name=`head -n 1 README.md | sed 's/# //'`
93+
local info_base="[$timestamp INFO]: $basefile::$scope"
94+
95+
echo "$info_base started" >> $logfile
96+
97+
echo "$info_base starting services" >> $logfile
98+
99+
sudo docker-compose up --build
100+
101+
echo "$info_base ended" >> $logfile
102+
103+
echo "================" >> $logfile
104+
}
105+
function tear-down(){
106+
107+
scope="tear-down"
108+
info_base="[$timestamp INFO]: $basefile::$scope"
109+
110+
echo "$info_base started" >> $logfile
111+
112+
echo "$info_base starting services" >> $logfile
113+
114+
sudo docker-compose down
115+
116+
echo "$info_base ended" >> $logfile
117+
118+
echo "================" >> $logfile
119+
}
120+
121+
root-check
122+
docker-check
123+
docker-compose-check
124+
125+
while getopts ":udh" opts; do
126+
case $opts in
127+
u)
128+
setup-logging
129+
start-up ;;
130+
d)
131+
tear-down ;;
132+
h)
133+
usage
134+
exit 0 ;;
135+
/?)
136+
usage
137+
exit 1 ;;
138+
esac
139+
done

java-srv/Dockerfile

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
FROM vanto/apache-buildr:latest-jruby-jdk8 as builder
2+
3+
WORKDIR /workspace
4+
5+
COPY bin .
6+
7+
RUN buildr compile
8+
9+
FROM alpine:edge
10+
11+
RUN adduser -D developer
12+
13+
ENV DISPLAY :0
14+
15+
RUN apk update \
16+
&& apk add openjdk11
17+
18+
RUN apk --no-cache add msttcorefonts-installer fontconfig && \
19+
update-ms-fonts && \
20+
fc-cache -f
21+
22+
USER developer
23+
24+
WORKDIR /home/developer
25+
26+
COPY --from=builder /workspace/target/classes .
27+
28+
CMD ["java", "example.Main"]

java-srv/bin/buildfile

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Generated by Buildr 1.4.22, change to your liking
2+
3+
# Version number for this release
4+
VERSION_NUMBER = "1.0.0"
5+
# Group identifier for your projects
6+
GROUP = "desktop-app"
7+
COPYRIGHT = ""
8+
9+
# Specify Maven 2.0 remote repositories here, like this:
10+
11+
repositories.remote << "http://insecure.repo1.maven.org/maven2"
12+
13+
desc "The POC apache buildr project"
14+
define "desktop-app" do
15+
16+
project.version = VERSION_NUMBER
17+
project.group = GROUP
18+
manifest["Implementation-Vendor"] = COPYRIGHT
19+
compile.with "log4j:log4j:jar:1.2.17",
20+
"org.slf4j:slf4j-api:jar:1.7.5",
21+
"org.slf4j:slf4j-log4j12:jar:1.7.5",
22+
"com.microsoft.sqlserver:mssql-jdbc:jar:8.4.1.jre11",
23+
"org.hibernate:hibernate-core:jar:5.3.7.Final",
24+
"jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3",
25+
"org.glassfish.jaxb:jaxb-runtime:jar:2.3.3"
26+
package(:jar)
27+
28+
run.using :main => 'example.Main'
29+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package example;
2+
3+
import org.hibernate.SessionFactory;
4+
import org.hibernate.boot.Metadata;
5+
import org.hibernate.boot.MetadataSources;
6+
import org.hibernate.boot.registry.StandardServiceRegistry;
7+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
8+
9+
public class HibernateUtils {
10+
private static SessionFactory sessionFactory = buildSessionFactory();
11+
12+
private static SessionFactory buildSessionFactory()
13+
{
14+
try
15+
{
16+
if (sessionFactory == null)
17+
{
18+
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
19+
.configure("hibernate.cfg.xml").build();
20+
21+
Metadata metaData = new MetadataSources(standardRegistry)
22+
.getMetadataBuilder()
23+
.build();
24+
25+
sessionFactory = metaData.getSessionFactoryBuilder().build();
26+
}
27+
return sessionFactory;
28+
} catch (Throwable ex) {
29+
throw new ExceptionInInitializerError(ex);
30+
}
31+
}
32+
33+
public static SessionFactory getSessionFactory() {
34+
return sessionFactory;
35+
}
36+
37+
public static void shutdown() {
38+
getSessionFactory().close();
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package example;
2+
3+
import example.dto.*;
4+
import example.dto.constants.*;
5+
import example.dto.interfaces.*;
6+
7+
import org.hibernate.Session;
8+
9+
public class Main {
10+
11+
private static <T extends IJoined> void together(T tbl)
12+
{
13+
try {
14+
int i = 1;
15+
for (BREED breed : BREED.values()){
16+
int j = 1;
17+
for (COLOR color : COLOR.values())
18+
tbl.insert(i, j++);
19+
i++;
20+
}
21+
} catch (Exception e) {}
22+
23+
tbl.selectAll();
24+
}
25+
private static <T extends ILookup, U extends Enum<U>> void lookup(T tbl, Class<U> data)
26+
{
27+
try {
28+
for (Enum<U> unit: data.getEnumConstants())
29+
tbl.insert(unit.name());
30+
} catch (Exception e) {}
31+
32+
tbl.selectAll();
33+
}
34+
35+
public static void main(String[] args) {
36+
org.hibernate.Session session = HibernateUtils.getSessionFactory().openSession();
37+
lookup(new Breed(session), BREED.class);
38+
lookup(new Color(session), COLOR.class);
39+
together(new Dog(session));
40+
together(new DogExpanded(session));
41+
together(new BreedCount(session));
42+
HibernateUtils.shutdown();
43+
}
44+
}

0 commit comments

Comments
 (0)