Skip to content

Commit 9f40855

Browse files
committed
initial commit
0 parents  commit 9f40855

File tree

12 files changed

+457
-0
lines changed

12 files changed

+457
-0
lines changed

README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# groovy-cli-gradle-hibernate-cockroachdb-single-node-without-ssl-simple
2+
3+
## Description
4+
Creates a small database table
5+
called `dog` and populates with
6+
hql.
7+
A groovy gradle build, that connects to single node
8+
cockroach database without ssl.
9+
10+
## Tech stack
11+
- groovy
12+
- gradle
13+
- hibernate
14+
- hql
15+
- log4j
16+
- postgres drivers
17+
18+
## Docker stack
19+
- cockroachdb/cockroach:v19.2.2
20+
- gradle:jdk11
21+
22+
## To run
23+
`sudo ./install.sh -u`
24+
- [webui](http://localhost:8080)
25+
26+
## To stop
27+
`sudo ./install.sh -d`
28+
29+
## For help
30+
`sudo ./install.sh -h`
31+
32+
## Credit
33+
- [HQL code based on](https://www.journaldev.com/2954/hibernate-query-language-hql-example-tutorial)
34+
- [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)
35+
- [Hibernate code based on](https://github.com/lokeshgupta1981/hibernate/tree/master/hibernate-hello-world)

docker-compose.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
version: '3'
2+
services:
3+
java-srv:
4+
build:
5+
context: java-srv
6+
command: /bin/sh -c "/wait && gradle run"
7+
environment:
8+
- WAIT_HOSTS=db:26257
9+
- WAIT_HOSTS_TIMEOUT=300
10+
- WAIT_SLEEP_INTERVAL=30
11+
- WAIT_HOST_CONNECT_TIMEOUT=30
12+
depends_on:
13+
- db
14+
links:
15+
- "db:db"
16+
17+
db:
18+
image: cockroachdb/cockroach:v19.2.2
19+
ports:
20+
- "26257:26257"
21+
- "8080:8080"
22+
command: start-single-node --insecure

general.log

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[2023-09-28 13:09:56 INFO]: install::setup-logging ended
2+
================
3+
[2023-09-28 13:09:56 INFO]: install::start-up started
4+
[2023-09-28 13:09:56 INFO]: install::start-up starting services
5+
[2023-09-28 13:09:56 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

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM gradle:jdk11
2+
3+
WORKDIR /app
4+
5+
ADD --chown=gradle:gradle bin .
6+
7+
ENV WAIT_VERSION 2.7.2
8+
9+
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/$WAIT_VERSION/wait /wait
10+
11+
RUN chmod +x /wait
12+
13+
CMD "gradle run"

java-srv/bin/build.gradle

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apply plugin: 'groovy'
2+
apply plugin: 'application'
3+
4+
repositories {
5+
jcenter()
6+
mavenCentral()
7+
}
8+
9+
sourceCompatibility = 11
10+
targetCompatibility = 11
11+
12+
dependencies {
13+
implementation 'org.codehaus.groovy:groovy-all:3.0.7'
14+
implementation 'com.google.guava:guava:29.0-jre'
15+
implementation 'org.postgresql:postgresql:42.2.18'
16+
implementation 'org.hibernate:hibernate-core:5.4.23.Final'
17+
implementation 'jakarta.xml.bind:jakarta.xml.bind-api:2.3.3'
18+
implementation 'org.glassfish.jaxb:jaxb-runtime:2.3.3'
19+
implementation 'log4j:log4j:1.2.17'
20+
implementation 'org.slf4j:slf4j-api:1.7.5'
21+
implementation 'org.slf4j:slf4j-log4j12:1.7.5'
22+
}
23+
24+
mainClassName = 'example.Main'
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,23 @@
1+
package example;
2+
3+
import example.model.Dog;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) {
8+
org.hibernate.Session session = HibernateUtils.getSessionFactory().openSession();
9+
10+
Dog tbl = new Dog(session);
11+
12+
try {
13+
tbl.insert(1L, "Am Bulldog", "White");
14+
tbl.insert(2L, "Blue Tick", "Grey");
15+
tbl.insert(3L, "Labrador", "Black");
16+
tbl.insert(4L, "Gr Shepard", "Brown");
17+
} catch (Exception e) {}
18+
19+
tbl.selectAll();
20+
21+
HibernateUtils.shutdown();
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package example.entity;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Column;
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.GenerationType;
8+
import javax.persistence.Id;
9+
import javax.persistence.Table;
10+
import javax.persistence.UniqueConstraint;
11+
import org.hibernate.annotations.OptimisticLockType;
12+
13+
@Entity
14+
@Table(name = "dog")
15+
public class DogEntity {
16+
17+
public DogEntity(long a, String b, String c)
18+
{
19+
id = a;
20+
color = b;
21+
breed = c;
22+
}
23+
24+
@Id
25+
// @GeneratedValue(strategy = GenerationType.IDENTITY)
26+
@Column(name="id")
27+
private long id;
28+
29+
@Column(name="color")
30+
private String color;
31+
32+
@Column(name="breed")
33+
private String breed;
34+
35+
// Accessors and mutators for all three fields
36+
37+
public long getId(){ return id;}
38+
39+
public String getColor(){ return color;}
40+
public void setColor(String value){color = value;}
41+
42+
public String getBreed(){ return breed;}
43+
public void setBreed(String value){breed = value;}
44+
45+
@Override
46+
public String toString(){
47+
return String.format("[OUTPUT] %d, breed=%s, color=%s", id, breed, color);
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package example.model;
2+
3+
import org.hibernate.Query;
4+
import org.hibernate.Session;
5+
import java.util.List;
6+
import example.entity.DogEntity;
7+
8+
public class Dog{
9+
Session session = null;
10+
public Dog(Session s){session = s;}
11+
12+
public void insert(long id, String breed, String color) throws Exception {
13+
if (color == null)
14+
throw new Exception("must provide color");
15+
else if (breed == null)
16+
throw new Exception("must provide breed");
17+
18+
session.beginTransaction();
19+
20+
DogEntity dog = new DogEntity(id, color, breed);
21+
22+
session.save(dog);
23+
24+
session.getTransaction().commit();
25+
}
26+
public void selectAll(){
27+
28+
String hql = "FROM DogEntity";
29+
Query query = session.createQuery(hql);
30+
List<DogEntity> lst = query.list();
31+
for (DogEntity entity : lst)
32+
System.out.println(entity.toString());
33+
}
34+
}

0 commit comments

Comments
 (0)