Skip to content

Commit

Permalink
examples: added another maven & gradle examples (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
dorav authored Dec 11, 2023
1 parent 921b1de commit 32c4c04
Show file tree
Hide file tree
Showing 25 changed files with 520 additions and 0 deletions.
42 changes: 42 additions & 0 deletions examples/e2e_java_example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
21 changes: 21 additions & 0 deletions examples/e2e_java_example/atlas.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
data "external_schema" "hibernate" {
program = [
"./gradlew",
"-q",
"schema",
"--properties", "schema-export.properties"
]
}

env "hibernate" {
src = data.external_schema.hibernate.url
dev = "docker://postgres/15/dev?search_path=public"
migration {
dir = "file://migrations"
}
format {
migrate {
diff = "{{ sql . \" \" }}"
}
}
}
16 changes: 16 additions & 0 deletions examples/e2e_java_example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
plugins {
id("java")
id("io.atlasgo.hibernate-provider-gradle-plugin") version "0.1"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
}

dependencies {
implementation("org.hibernate.orm:hibernate-core:6.4.0.Final")
implementation("org.postgresql:postgresql:42.7.0")
}
1 change: 1 addition & 0 deletions examples/e2e_java_example/gradlew
2 changes: 2 additions & 0 deletions examples/e2e_java_example/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = "e2e_java_example"

20 changes: 20 additions & 0 deletions examples/e2e_java_example/src/main/java/org/example/Actor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.example;

import jakarta.persistence.*;

import java.util.List;
import java.util.Set;

@Entity
@Table(name = "Actors")
public class Actor {
Actor(String name) {
this.name = name;
}

@Id
public String name;

@OneToMany(mappedBy = "actor", cascade = CascadeType.PERSIST)
public Set<MovieParticipation> actedIn;
}
25 changes: 25 additions & 0 deletions examples/e2e_java_example/src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.example;

import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;

import java.util.Set;

public class Main {
public static void main(String[] args) {
SessionFactory sessionFactory = new MetadataSources()
.addAnnotatedClass(Movie.class)
.addAnnotatedClass(Actor.class)
.addAnnotatedClass(MovieParticipation.class)
.buildMetadata()
.buildSessionFactory();

sessionFactory.inTransaction(session -> {
Movie matrix = new Movie();
matrix.title = "The Matrix";
Actor keanuReeves = new Actor("Keanu Reeves");
keanuReeves.actedIn = Set.of(new MovieParticipation(matrix, keanuReeves));
session.persist(keanuReeves);
});
}
}
15 changes: 15 additions & 0 deletions examples/e2e_java_example/src/main/java/org/example/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.example;

import jakarta.persistence.*;

import java.util.List;

@Entity
@Table(name = "Movies")
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;

public String title;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.example;

import jakarta.persistence.*;

@Entity
public class MovieParticipation {
MovieParticipation(Movie movie, Actor actor) {
this.key = new MovieParticipationKey(movie.id, actor.name);
this.actor = actor;
this.movie = movie;
}

@EmbeddedId
public MovieParticipationKey key;

@ManyToOne(cascade=CascadeType.PERSIST)
@MapsId("movieId")
@JoinColumn(name = "movieId")
Movie movie;

@ManyToOne(cascade=CascadeType.PERSIST)
@MapsId("actorName")
@JoinColumn(name = "actorName")
Actor actor;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.example;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;

import java.io.Serializable;

@Embeddable
public class MovieParticipationKey implements Serializable {
public MovieParticipationKey(Long movieId, String actorName) {
this.movieId = movieId;
this.actorName = actorName;
}
public Long movieId;

public String actorName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
hibernate.connection.url=jdbc:postgresql://localhost:5432/postgres
hibernate.connection.username=postgres
hibernate.connection.password=password

hibernate.show_sql=true
hibernate.format_sql=true
hibernate.highlight_sql=true

hibernate.hbm2ddl.auto=create
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
jakarta.persistence.database-product-name=PostgreSQL
jakarta.persistence.database-major-version=15
89 changes: 89 additions & 0 deletions examples/maven_project_spring_example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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>
<parent>
<artifactId>maven_project_example</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>

<groupId>com.example</groupId>
<artifactId>maven-spring-example</artifactId>
<version>1.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.2.13.Final</version>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>1.9.20</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.224</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.0</version>
</dependency>

<dependency>
<groupId>io.atlasgo</groupId>
<artifactId>hibernate-provider-maven-plugin</artifactId>
<version>0.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>3.2.0</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.9</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>3.2.0</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

</dependencies>

<build>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
</build>

</project>
17 changes: 17 additions & 0 deletions examples/maven_project_spring_example/private.pgp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-----BEGIN PGP PRIVATE KEY BLOCK-----

lIYEZXA6axYJKwYBBAHaRw8BAQdAXGMVgjm1eYjVZyeSu10iaIOWeDwNwrI8pSoh
C8eRNPT+BwMCO7sNTz2yAyD1YwnbHp4evuz2IkIdJuvWj+5/3UmEvaBGOuiv8aoT
O6vD74EN5cNRjlnz52aRX9vh+9+73t6cImiK3/AjIaiovBhwTG/yeLQWQXJpZ2Et
aXQgPGl0QGFyaWdhLmlvPoiZBBMWCgBBFiEEHDW4Gu+ioUHPCJr+y7ibSLsZCX0F
AmVwOmsCGwMFCQWjmoAFCwkIBwICIgIGFQoJCAsCBBYCAwECHgcCF4AACgkQy7ib
SLsZCX0y5QEAnrPx9t2FB847hEGEXSOyK2rCYkwFGgr6SEI1BI4p0RsBAKSg2UKk
uxVL7Mo7PNvyNyKfJKadZ8N8d+BWjbR4iSYAnIsEZXA6axIKKwYBBAGXVQEFAQEH
QI3GSAr9+2xLbMlEMDmZLCOnHr+Wcf55SYvxhWWzkDlAAwEIB/4HAwItX7A0CRoz
5vUxHj2s82NRJoaoMPddIZE3CghfOz8ubE5Ds7jaJWhnxyM5fb3HsFmaeq7srEJk
DGiia/Cra34MM6F/dLCbEdg0RCsfa6X9iH4EGBYKACYWIQQcNbga76KhQc8Imv7L
uJtIuxkJfQUCZXA6awIbDAUJBaOagAAKCRDLuJtIuxkJfXRVAQCfsR08ZOgdlWwO
NYZtZQGkjYX2wHaFHxXEZSgjAuoTcgEAgFCRwFXXsJZJ+GaMsfa+TxosK6lh5a9k
bilF82JcEww=
=7L48
-----END PGP PRIVATE KEY BLOCK-----
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example;

import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.service.ServiceRegistry;

import java.util.Map;
import java.util.Properties;
import java.util.function.Function;

public class H2ServiceRegistryBuilder implements Function<Properties, ServiceRegistry> {
public H2ServiceRegistryBuilder() {

}

@Override
public ServiceRegistry apply(Properties properties) {
return new StandardServiceRegistryBuilder()
.applySettings(Map.of(
"hibernate.connection.url","jdbc:h2:mem:testdb",
AvailableSettings.SCHEMA_MANAGEMENT_TOOL, properties.get(AvailableSettings.SCHEMA_MANAGEMENT_TOOL),
"hibernate.temp.use_jdbc_metadata_defaults", true,
"jakarta.persistence.database-product-name", "H2",
"jakarta.persistence.database-major-version", "",
"hibernate.connection.provider_class", ""))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.PropertySource;

@SpringBootApplication
@PropertySource(value = {"classpath:schema-export.properties"})
public class Main {
public static void main(String[] args) {
new AnnotationConfigApplicationContext(Main.class);
}
}
Loading

0 comments on commit 32c4c04

Please sign in to comment.