Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
commit
dc8676630d
|
@ -2,7 +2,7 @@
|
|||
The "REST with Spring" Classes
|
||||
==============================
|
||||
|
||||
After 5 months of work, here's the Master Class of REST With Spring: <br/>
|
||||
Here's the Master Class of REST With Spring (price changes permanently next Friday): <br/>
|
||||
**[>> THE REST WITH SPRING MASTER CLASS](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)**
|
||||
|
||||
And here's the Master Class of Learn Spring Security: <br/>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package benchmarking;
|
||||
package com.baeldung.streamordering;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit;
|
|||
import java.util.stream.IntStream;
|
||||
|
||||
|
||||
public class TestBenchmark
|
||||
public class BenchmarkUnitTest
|
||||
{
|
||||
|
||||
@Test
|
||||
|
@ -94,4 +94,4 @@ public class TestBenchmark
|
|||
for (int i = 0; i < 1000; i++)
|
||||
bh.consume (list.get (i));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
package com.baeldung.streamordering;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -10,10 +12,9 @@ import java.util.stream.IntStream;
|
|||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StreamsOrderingUnitTest {
|
||||
|
||||
public class StreamsOrderingTest {
|
||||
|
||||
Logger logger = Logger.getLogger( StreamsOrderingTest.class.getName());
|
||||
Logger logger = Logger.getLogger( StreamsOrderingUnitTest.class.getName());
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
|
@ -1,10 +1,12 @@
|
|||
package org.baeldung.java.io;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.SequenceInputStream;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
@ -74,6 +76,28 @@ public class JavaXToInputStreamUnitTest {
|
|||
IOUtils.closeQuietly(targetStream);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingPlainJava_whenConvertingFileToDataInputStream_thenCorrect() throws IOException {
|
||||
final File initialFile = new File("src/test/resources/sample.txt");
|
||||
final InputStream targetStream = new DataInputStream(new FileInputStream(initialFile));
|
||||
|
||||
IOUtils.closeQuietly(targetStream);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingPlainJava_whenConvertingFileToSequenceInputStream_thenCorrect() throws IOException {
|
||||
final File initialFile = new File("src/test/resources/sample.txt");
|
||||
final File anotherFile = new File("src/test/resources/anothersample.txt");
|
||||
final InputStream targetStream = new FileInputStream(initialFile);
|
||||
final InputStream anotherTargetStream = new FileInputStream(anotherFile);
|
||||
|
||||
InputStream sequenceTargetStream = new SequenceInputStream(targetStream, anotherTargetStream);
|
||||
|
||||
IOUtils.closeQuietly(targetStream);
|
||||
IOUtils.closeQuietly(anotherTargetStream);
|
||||
IOUtils.closeQuietly(sequenceTargetStream);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingGuava_whenConvertingFileToInputStream_thenCorrect() throws IOException {
|
||||
final File initialFile = new File("src/test/resources/sample.txt");
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
...Continues
|
|
@ -13,21 +13,31 @@ public class PersonRepositoryUnitTest {
|
|||
PersonRepository personRepository = new PersonRepository();
|
||||
|
||||
@Test
|
||||
public void whenIdIsNull_thenExceptionIsThrown() throws Exception {
|
||||
assertThrows(Exception.class,
|
||||
public void whenIdIsNull_thenExceptionIsThrown() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() ->
|
||||
Optional
|
||||
.ofNullable(personRepository.findNameById(null))
|
||||
.orElseThrow(Exception::new));
|
||||
.orElseThrow(IllegalArgumentException::new));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIdIsNonNull_thenNoExceptionIsThrown() throws Exception {
|
||||
public void whenIdIsNonNull_thenNoExceptionIsThrown() {
|
||||
assertAll(
|
||||
() ->
|
||||
Optional
|
||||
.ofNullable(personRepository.findNameById("id"))
|
||||
.orElseThrow(Exception::new));
|
||||
.orElseThrow(RuntimeException::new));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIdNonNull_thenReturnsNameUpperCase() {
|
||||
String name = Optional
|
||||
.ofNullable(personRepository.findNameById("id"))
|
||||
.map(String::toUpperCase)
|
||||
.orElseThrow(RuntimeException::new);
|
||||
|
||||
assertEquals("NAME", name);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -1,85 +1,105 @@
|
|||
<?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>
|
||||
<artifactId>core-kotlin</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
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>
|
||||
<artifactId>core-kotlin</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-kotlin</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../parent-kotlin</relativePath>
|
||||
</parent>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-kotlin</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../parent-kotlin</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-api</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-subject-extension</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-junit-platform-engine</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>khttp</groupId>
|
||||
<artifactId>khttp</artifactId>
|
||||
<version>${khttp.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.nhaarman</groupId>
|
||||
<artifactId>mockito-kotlin</artifactId>
|
||||
<version>${mockito-kotlin.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.salomonbrys.kodein</groupId>
|
||||
<artifactId>kodein</artifactId>
|
||||
<version>${kodein.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.beust</groupId>
|
||||
<artifactId>klaxon</artifactId>
|
||||
<version>${klaxon.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>exposed</id>
|
||||
<name>exposed</name>
|
||||
<url>https://dl.bintray.com/kotlin/exposed</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<properties>
|
||||
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
|
||||
<kodein.version>4.1.0</kodein.version>
|
||||
<klaxon.version>3.0.4</klaxon.version>
|
||||
<khttp.version>0.1.0</khttp.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<junit.platform.version>1.1.1</junit.platform.version>
|
||||
<junit.vintage.version>5.2.0</junit.vintage.version>
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-api</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-subject-extension</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.spek</groupId>
|
||||
<artifactId>spek-junit-platform-engine</artifactId>
|
||||
<version>1.1.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>khttp</groupId>
|
||||
<artifactId>khttp</artifactId>
|
||||
<version>${khttp.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.nhaarman</groupId>
|
||||
<artifactId>mockito-kotlin</artifactId>
|
||||
<version>${mockito-kotlin.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.salomonbrys.kodein</groupId>
|
||||
<artifactId>kodein</artifactId>
|
||||
<version>${kodein.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.beust</groupId>
|
||||
<artifactId>klaxon</artifactId>
|
||||
<version>${klaxon.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.exposed</groupId>
|
||||
<artifactId>exposed</artifactId>
|
||||
<version>${exposed.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2database.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
|
||||
<kodein.version>4.1.0</kodein.version>
|
||||
<klaxon.version>3.0.4</klaxon.version>
|
||||
<khttp.version>0.1.0</khttp.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<junit.platform.version>1.1.1</junit.platform.version>
|
||||
<junit.vintage.version>5.2.0</junit.vintage.version>
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
<h2database.version>1.4.197</h2database.version>
|
||||
<exposed.version>0.10.4</exposed.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,333 @@
|
|||
package com.baeldung.kotlin.exposed
|
||||
|
||||
import org.jetbrains.exposed.dao.*
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.transactions.TransactionManager
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.junit.Test
|
||||
import java.sql.DriverManager
|
||||
import kotlin.test.*
|
||||
|
||||
class ExposedTest {
|
||||
|
||||
@Test
|
||||
fun whenH2Database_thenConnectionSuccessful() {
|
||||
val database = Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
|
||||
transaction {
|
||||
assertEquals(1.4.toBigDecimal(), database.version)
|
||||
assertEquals("h2", database.vendor)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenH2DatabaseWithCredentials_thenConnectionSuccessful() {
|
||||
Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver", user = "myself", password = "secret")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenH2DatabaseWithManualConnection_thenConnectionSuccessful() {
|
||||
var connected = false
|
||||
Database.connect({ connected = true; DriverManager.getConnection("jdbc:h2:mem:test;MODE=MySQL") })
|
||||
assertEquals(false, connected)
|
||||
transaction {
|
||||
addLogger(StdOutSqlLogger)
|
||||
assertEquals(false, connected)
|
||||
SchemaUtils.create(Cities)
|
||||
assertEquals(true, connected)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenManualCommit_thenOk() {
|
||||
Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
|
||||
transaction {
|
||||
assertTrue(this is Transaction)
|
||||
commit()
|
||||
commit()
|
||||
commit()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenInsert_thenGeneratedKeys() {
|
||||
Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
|
||||
transaction {
|
||||
SchemaUtils.create(StarWarsFilms)
|
||||
val id = StarWarsFilms.insertAndGetId {
|
||||
it[name] = "The Last Jedi"
|
||||
it[sequelId] = 8
|
||||
it[director] = "Rian Johnson"
|
||||
}
|
||||
assertEquals(1, id.value)
|
||||
val insert = StarWarsFilms.insert {
|
||||
it[name] = "The Force Awakens"
|
||||
it[sequelId] = 7
|
||||
it[director] = "J.J. Abrams"
|
||||
}
|
||||
assertEquals(2, insert[StarWarsFilms.id]?.value)
|
||||
val selectAll = StarWarsFilms.selectAll()
|
||||
selectAll.forEach {
|
||||
assertTrue { it[StarWarsFilms.sequelId] >= 7 }
|
||||
}
|
||||
StarWarsFilms.slice(StarWarsFilms.name, StarWarsFilms.director).selectAll()
|
||||
.forEach {
|
||||
assertTrue { it[StarWarsFilms.name].startsWith("The") }
|
||||
}
|
||||
val select = StarWarsFilms.select { (StarWarsFilms.director like "J.J.%") and (StarWarsFilms.sequelId eq 7) }
|
||||
assertEquals(1, select.count())
|
||||
StarWarsFilms.update ({ StarWarsFilms.sequelId eq 8 }) {
|
||||
it[name] = "Episode VIII – The Last Jedi"
|
||||
with(SqlExpressionBuilder) {
|
||||
it.update(StarWarsFilms.sequelId, StarWarsFilms.sequelId + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenForeignKey_thenAutoJoin() {
|
||||
Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
|
||||
transaction {
|
||||
addLogger(StdOutSqlLogger)
|
||||
SchemaUtils.create(StarWarsFilms, Players)
|
||||
StarWarsFilms.insert {
|
||||
it[name] = "The Last Jedi"
|
||||
it[sequelId] = 8
|
||||
it[director] = "Rian Johnson"
|
||||
}
|
||||
StarWarsFilms.insert {
|
||||
it[name] = "The Force Awakens"
|
||||
it[sequelId] = 7
|
||||
it[director] = "J.J. Abrams"
|
||||
}
|
||||
Players.insert {
|
||||
it[name] = "Mark Hamill"
|
||||
it[sequelId] = 7
|
||||
}
|
||||
Players.insert {
|
||||
it[name] = "Mark Hamill"
|
||||
it[sequelId] = 8
|
||||
}
|
||||
val simpleInnerJoin = (StarWarsFilms innerJoin Players).selectAll()
|
||||
assertEquals(2, simpleInnerJoin.count())
|
||||
simpleInnerJoin.forEach {
|
||||
assertNotNull(it[StarWarsFilms.name])
|
||||
assertEquals(it[StarWarsFilms.sequelId], it[Players.sequelId])
|
||||
assertEquals("Mark Hamill", it[Players.name])
|
||||
}
|
||||
val innerJoinWithCondition = (StarWarsFilms innerJoin Players)
|
||||
.select { StarWarsFilms.sequelId eq Players.sequelId }
|
||||
assertEquals(2, innerJoinWithCondition.count())
|
||||
innerJoinWithCondition.forEach {
|
||||
assertNotNull(it[StarWarsFilms.name])
|
||||
assertEquals(it[StarWarsFilms.sequelId], it[Players.sequelId])
|
||||
assertEquals("Mark Hamill", it[Players.name])
|
||||
}
|
||||
val complexInnerJoin = Join(StarWarsFilms, Players, joinType = JoinType.INNER, onColumn = StarWarsFilms.sequelId, otherColumn = Players.sequelId, additionalConstraint = {
|
||||
StarWarsFilms.sequelId eq 8
|
||||
}).selectAll()
|
||||
assertEquals(1, complexInnerJoin.count())
|
||||
complexInnerJoin.forEach {
|
||||
assertNotNull(it[StarWarsFilms.name])
|
||||
assertEquals(it[StarWarsFilms.sequelId], it[Players.sequelId])
|
||||
assertEquals("Mark Hamill", it[Players.name])
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenJoinWithAlias_thenFun() {
|
||||
Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
|
||||
transaction {
|
||||
addLogger(StdOutSqlLogger)
|
||||
SchemaUtils.create(StarWarsFilms, Players)
|
||||
StarWarsFilms.insert {
|
||||
it[name] = "The Last Jedi"
|
||||
it[sequelId] = 8
|
||||
it[director] = "Rian Johnson"
|
||||
}
|
||||
StarWarsFilms.insert {
|
||||
it[name] = "The Force Awakens"
|
||||
it[sequelId] = 7
|
||||
it[director] = "J.J. Abrams"
|
||||
}
|
||||
val sequel = StarWarsFilms.alias("sequel")
|
||||
Join(StarWarsFilms, sequel,
|
||||
additionalConstraint = { sequel[StarWarsFilms.sequelId] eq StarWarsFilms.sequelId + 1 })
|
||||
.selectAll().forEach {
|
||||
assertEquals(it[sequel[StarWarsFilms.sequelId]], it[StarWarsFilms.sequelId] + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenEntity_thenDAO() {
|
||||
val database = Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
|
||||
val connection = database.connector.invoke() //Keep a connection open so the DB is not destroyed after the first transaction
|
||||
val inserted = transaction {
|
||||
addLogger(StdOutSqlLogger)
|
||||
SchemaUtils.create(StarWarsFilms, Players)
|
||||
val theLastJedi = StarWarsFilm.new {
|
||||
name = "The Last Jedi"
|
||||
sequelId = 8
|
||||
director = "Rian Johnson"
|
||||
}
|
||||
assertFalse(TransactionManager.current().entityCache.inserts.isEmpty())
|
||||
assertEquals(1, theLastJedi.id.value) //Reading this causes a flush
|
||||
assertTrue(TransactionManager.current().entityCache.inserts.isEmpty())
|
||||
theLastJedi
|
||||
}
|
||||
transaction {
|
||||
val theLastJedi = StarWarsFilm.findById(1)
|
||||
assertNotNull(theLastJedi)
|
||||
assertEquals(inserted.id, theLastJedi?.id)
|
||||
}
|
||||
connection.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenManyToOne_thenNavigation() {
|
||||
val database = Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
|
||||
val connection = database.connector.invoke()
|
||||
transaction {
|
||||
addLogger(StdOutSqlLogger)
|
||||
SchemaUtils.create(StarWarsFilms, Players, Users, UserRatings)
|
||||
val theLastJedi = StarWarsFilm.new {
|
||||
name = "The Last Jedi"
|
||||
sequelId = 8
|
||||
director = "Rian Johnson"
|
||||
}
|
||||
val someUser = User.new {
|
||||
name = "Some User"
|
||||
}
|
||||
val rating = UserRating.new {
|
||||
value = 9
|
||||
user = someUser
|
||||
film = theLastJedi
|
||||
}
|
||||
assertEquals(theLastJedi, rating.film)
|
||||
assertEquals(someUser, rating.user)
|
||||
assertEquals(rating, theLastJedi.ratings.first())
|
||||
}
|
||||
transaction {
|
||||
val theLastJedi = StarWarsFilm.find { StarWarsFilms.sequelId eq 8 }.first()
|
||||
val ratings = UserRating.find { UserRatings.film eq theLastJedi.id }
|
||||
assertEquals(1, ratings.count())
|
||||
val rating = ratings.first()
|
||||
assertEquals("Some User", rating.user.name)
|
||||
assertEquals(rating, theLastJedi.ratings.first())
|
||||
UserRating.new {
|
||||
value = 8
|
||||
user = rating.user
|
||||
film = theLastJedi
|
||||
}
|
||||
assertEquals(2, theLastJedi.ratings.count())
|
||||
}
|
||||
connection.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenManyToMany_thenAssociation() {
|
||||
val database = Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
|
||||
val connection = database.connector.invoke()
|
||||
val film = transaction {
|
||||
SchemaUtils.create(StarWarsFilms)
|
||||
StarWarsFilm.new {
|
||||
name = "The Last Jedi"
|
||||
sequelId = 8
|
||||
director = "Rian Johnson"
|
||||
}
|
||||
}
|
||||
|
||||
val actor = transaction {
|
||||
SchemaUtils.create(Actors)
|
||||
Actor.new {
|
||||
firstname = "Daisy"
|
||||
lastname = "Ridley"
|
||||
}
|
||||
}
|
||||
|
||||
transaction {
|
||||
SchemaUtils.create(StarWarsFilmActors)
|
||||
film.actors = SizedCollection(listOf(actor))
|
||||
}
|
||||
connection.close()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
object Cities: IntIdTable() {
|
||||
val name = varchar("name", 50)
|
||||
}
|
||||
|
||||
object StarWarsFilms_Simple : Table() {
|
||||
val id = integer("id").autoIncrement().primaryKey()
|
||||
val sequelId = integer("sequel_id").uniqueIndex()
|
||||
val name = varchar("name", 50)
|
||||
val director = varchar("director", 50)
|
||||
}
|
||||
|
||||
object StarWarsFilms : IntIdTable() {
|
||||
val sequelId = integer("sequel_id").uniqueIndex()
|
||||
val name = varchar("name", 50)
|
||||
val director = varchar("director", 50)
|
||||
}
|
||||
|
||||
object Players : Table() {
|
||||
//val sequelId = integer("sequel_id").uniqueIndex().references(StarWarsFilms.sequelId)
|
||||
val sequelId = reference("sequel_id", StarWarsFilms.sequelId).uniqueIndex()
|
||||
//val filmId = reference("film_id", StarWarsFilms).nullable()
|
||||
val name = varchar("name", 50)
|
||||
}
|
||||
|
||||
class StarWarsFilm(id: EntityID<Int>) : Entity<Int>(id) {
|
||||
companion object : EntityClass<Int, StarWarsFilm>(StarWarsFilms)
|
||||
|
||||
var sequelId by StarWarsFilms.sequelId
|
||||
var name by StarWarsFilms.name
|
||||
var director by StarWarsFilms.director
|
||||
var actors by Actor via StarWarsFilmActors
|
||||
val ratings by UserRating referrersOn UserRatings.film
|
||||
}
|
||||
|
||||
object Users: IntIdTable() {
|
||||
val name = varchar("name", 50)
|
||||
}
|
||||
|
||||
object UserRatings: IntIdTable() {
|
||||
val value = long("value")
|
||||
val film = reference("film", StarWarsFilms)
|
||||
val user = reference("user", Users)
|
||||
}
|
||||
|
||||
class User(id: EntityID<Int>): IntEntity(id) {
|
||||
companion object : IntEntityClass<User>(Users)
|
||||
|
||||
var name by Users.name
|
||||
}
|
||||
|
||||
class UserRating(id: EntityID<Int>): IntEntity(id) {
|
||||
companion object : IntEntityClass<UserRating>(UserRatings)
|
||||
|
||||
var value by UserRatings.value
|
||||
var film by StarWarsFilm referencedOn UserRatings.film
|
||||
var user by User referencedOn UserRatings.user
|
||||
}
|
||||
|
||||
object Actors: IntIdTable() {
|
||||
val firstname = varchar("firstname", 50)
|
||||
val lastname = varchar("lastname", 50)
|
||||
}
|
||||
|
||||
class Actor(id: EntityID<Int>): IntEntity(id) {
|
||||
companion object : IntEntityClass<Actor>(Actors)
|
||||
|
||||
var firstname by Actors.firstname
|
||||
var lastname by Actors.lastname
|
||||
}
|
||||
|
||||
object StarWarsFilmActors : Table() {
|
||||
val starWarsFilm = reference("starWarsFilm", StarWarsFilms).primaryKey(0)
|
||||
val actor = reference("actor", Actors).primaryKey(1)
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.hibernate.joincolumn;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Address {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "ZIP")
|
||||
private String zipCode;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getZipCode() {
|
||||
return zipCode;
|
||||
}
|
||||
|
||||
public void setZipCode(String zipCode) {
|
||||
this.zipCode = zipCode;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.baeldung.hibernate.joincolumn;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class Email {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String address;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "employee_id")
|
||||
private Employee employee;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public Employee getEmployee() {
|
||||
return employee;
|
||||
}
|
||||
|
||||
public void setEmployee(Employee employee) {
|
||||
this.employee = employee;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.hibernate.joincolumn;
|
||||
|
||||
import java.util.List;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
@Entity
|
||||
public class Employee {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@OneToMany(fetch = FetchType.LAZY, mappedBy = "employee")
|
||||
private List<Email> emails;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<Email> getEmails() {
|
||||
return emails;
|
||||
}
|
||||
|
||||
public void setEmails(List<Email> emails) {
|
||||
this.emails = emails;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.hibernate.joincolumn;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinColumns;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class Office {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumns({
|
||||
@JoinColumn(name="ADDR_ID", referencedColumnName="ID"),
|
||||
@JoinColumn(name="ADDR_ZIP", referencedColumnName="ZIP")
|
||||
})
|
||||
private Address address;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.hibernate.joincolumn;
|
||||
|
||||
import com.baeldung.hibernate.HibernateUtil;
|
||||
import java.io.IOException;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class JoinColumnIntegrationTest {
|
||||
|
||||
private Session session;
|
||||
private Transaction transaction;
|
||||
|
||||
@Before
|
||||
public void setUp() throws IOException {
|
||||
session = HibernateUtil.getSessionFactory("hibernate-spatial.properties")
|
||||
.openSession();
|
||||
transaction = session.beginTransaction();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
transaction.rollback();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOfficeEntity_setAddress_shouldPersist() {
|
||||
Office office = new Office();
|
||||
|
||||
Address address = new Address();
|
||||
address.setZipCode("11-111");
|
||||
office.setAddress(address);
|
||||
|
||||
session.save(office);
|
||||
session.flush();
|
||||
session.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeEntity_setEmails_shouldPersist() {
|
||||
Employee employee = new Employee();
|
||||
|
||||
Email email = new Email();
|
||||
email.setAddress("example@email.com");
|
||||
email.setEmployee(employee);
|
||||
|
||||
session.save(employee);
|
||||
session.flush();
|
||||
session.clear();
|
||||
}
|
||||
|
||||
}
|
|
@ -15,6 +15,18 @@
|
|||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/org.openjdk.jmh/jmh-core -->
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
|
@ -107,6 +119,7 @@
|
|||
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<jmh.version>1.21</jmh.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<lombok.version>1.16.12</lombok.version>
|
||||
<vavr.version>0.9.0</vavr.version>
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
package com.baeldung.streamordering;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
import org.openjdk.jmh.infra.Blackhole;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
import org.openjdk.jmh.runner.options.TimeValue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
|
||||
public class BenchmarkUnitTest
|
||||
{
|
||||
|
||||
@Test
|
||||
public void
|
||||
launchBenchmark() throws Exception {
|
||||
|
||||
Options opt = new OptionsBuilder()
|
||||
// Specify which benchmarks to run.
|
||||
// You can be more specific if you'd like to run only one benchmark per test.
|
||||
.include(this.getClass().getName() + ".*")
|
||||
// Set the following options as needed
|
||||
.mode (Mode.AverageTime)
|
||||
.timeUnit(TimeUnit.MICROSECONDS)
|
||||
.warmupTime(TimeValue.seconds(1))
|
||||
.warmupIterations(2)
|
||||
.measurementTime(TimeValue.seconds(1))
|
||||
.measurementIterations(2)
|
||||
.threads(2)
|
||||
.forks(1)
|
||||
.shouldFailOnError(true)
|
||||
.shouldDoGC(true)
|
||||
//.jvmArgs("-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintInlining")
|
||||
//.addProfiler(WinPerfAsmProfiler.class)
|
||||
.build();
|
||||
|
||||
new Runner(opt).run();
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void givenOrderedStreamInput_whenStreamFiltered_showOpsPerMS(){
|
||||
IntStream.range(1, 100_000_000).parallel().filter(i -> i % 10 == 0).toArray();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void givenUnorderedStreamInput_whenStreamFiltered_showOpsPerMS(){
|
||||
IntStream.range(1,100_000_000).unordered().parallel().filter(i -> i % 10 == 0).toArray();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void givenUnorderedStreamInput_whenStreamDistinct_showOpsPerMS(){
|
||||
IntStream.range(1, 1_000_000).unordered().parallel().distinct().toArray();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void givenOrderedStreamInput_whenStreamDistinct_showOpsPerMS() {
|
||||
//section 5.1.
|
||||
IntStream.range(1, 1_000_000).parallel().distinct().toArray();
|
||||
}
|
||||
|
||||
|
||||
// The JMH samples are the best documentation for how to use it
|
||||
// http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/
|
||||
@State(Scope.Thread)
|
||||
public static class BenchmarkState
|
||||
{
|
||||
List<Integer> list;
|
||||
|
||||
@Setup(Level.Trial) public void
|
||||
initialize() {
|
||||
|
||||
Random rand = new Random();
|
||||
|
||||
list = new ArrayList<>();
|
||||
for (int i = 0; i < 1000; i++)
|
||||
list.add (rand.nextInt());
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark public void
|
||||
benchmark1 (BenchmarkState state, Blackhole bh) {
|
||||
|
||||
List<Integer> list = state.list;
|
||||
|
||||
for (int i = 0; i < 1000; i++)
|
||||
bh.consume (list.get (i));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,149 @@
|
|||
package com.baeldung.streamordering;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StreamsOrderingUnitTest {
|
||||
|
||||
Logger logger = Logger.getLogger( StreamsOrderingUnitTest.class.getName());
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
logger.setLevel(Level.ALL);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoCollections_whenStreamed_thenCheckOutputDifferent(){
|
||||
|
||||
List<String> list = Arrays.asList("B", "A", "C", "D", "F");
|
||||
Set<String> set = new TreeSet<>(Arrays.asList("B", "A", "C", "D", "F"));
|
||||
|
||||
Object[] listOutput = list.stream().toArray();
|
||||
Object[] setOutput = set.stream().toArray();
|
||||
|
||||
assertEquals("[B, A, C, D, F]", Arrays.toString(listOutput));
|
||||
assertEquals("[A, B, C, D, F]", Arrays.toString(setOutput));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoCollections_whenStreamedInParallel_thenCheckOutputDifferent(){
|
||||
|
||||
List<String> list = Arrays.asList("B", "A", "C", "D", "F");
|
||||
Set<String> set = new TreeSet<>(Arrays.asList("B", "A", "C", "D", "F"));
|
||||
|
||||
Object[] listOutput = list.stream().parallel().toArray();
|
||||
Object[] setOutput = set.stream().parallel().toArray();
|
||||
|
||||
assertEquals("[B, A, C, D, F]", Arrays.toString(listOutput));
|
||||
assertEquals("[A, B, C, D, F]", Arrays.toString(setOutput));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void givenOrderedInput_whenUnorderedAndOrderedCompared_thenCheckUnorderedOutputChanges(){
|
||||
Set<Integer> set = new TreeSet<>(
|
||||
Arrays.asList(-9, -5, -4, -2, 1, 2, 4, 5, 7, 9, 12, 13, 16, 29, 23, 34, 57, 68, 90, 102, 230));
|
||||
|
||||
Object[] orderedArray = set.stream()
|
||||
.parallel()
|
||||
.limit(5)
|
||||
.toArray();
|
||||
Object[] unorderedArray = set.stream()
|
||||
.unordered()
|
||||
.parallel()
|
||||
.limit(5)
|
||||
.toArray();
|
||||
|
||||
logger.info(Arrays.toString(orderedArray));
|
||||
logger.info(Arrays.toString(unorderedArray));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenUnsortedStreamInput_whenStreamSorted_thenCheckOrderChanged(){
|
||||
|
||||
List<Integer> list = Arrays.asList(-3,10,-4,1,3);
|
||||
|
||||
Object[] listOutput = list.stream().toArray();
|
||||
Object[] listOutputSorted = list.stream().sorted().toArray();
|
||||
|
||||
assertEquals("[-3, 10, -4, 1, 3]", Arrays.toString(listOutput));
|
||||
assertEquals("[-4, -3, 1, 3, 10]", Arrays.toString(listOutputSorted));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnsortedStreamInput_whenStreamDistinct_thenShowTimeTaken(){
|
||||
long start, end;
|
||||
start = System.currentTimeMillis();
|
||||
IntStream.range(1,1_000_000).unordered().parallel().distinct().toArray();
|
||||
end = System.currentTimeMillis();
|
||||
System.out.println(String.format("Time taken when unordered: %d ms", (end - start)));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenSameCollection_whenStreamTerminated_thenCheckEachVsEachOrdered(){
|
||||
|
||||
List<String> list = Arrays.asList("B", "A", "C", "D", "F");
|
||||
|
||||
list.stream().parallel().forEach(e -> logger.log(Level.INFO, e));
|
||||
list.stream().parallel().forEachOrdered(e -> logger.log(Level.INFO, e));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSameCollection_whenStreamCollected_thenCheckOutput(){
|
||||
|
||||
List<String> list = Arrays.asList("B", "A", "C", "D", "F");
|
||||
|
||||
List<String> collectionList = list.stream().parallel().collect(Collectors.toList());
|
||||
Set<String> collectionSet = list.stream().parallel().collect(Collectors.toCollection(TreeSet::new));
|
||||
|
||||
assertEquals("[B, A, C, D, F]", collectionList.toString());
|
||||
assertEquals("[A, B, C, D, F]", collectionSet.toString());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenListIterationOrder_whenStreamCollectedToMap_thenCeckOrderChanged() {
|
||||
List<String> list = Arrays.asList("A", "BB", "CCC");
|
||||
|
||||
Map<String, Integer> hashMap = list.stream().collect(Collectors.toMap(Function.identity(), String::length));
|
||||
|
||||
Object[] keySet = hashMap.keySet().toArray();
|
||||
|
||||
assertEquals("[BB, A, CCC]", Arrays.toString(keySet));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListIteration_whenStreamCollectedtoHashMap_thenCheckOrderMaintained() {
|
||||
List<String> list = Arrays.asList("A", "BB", "CCC", "CCC");
|
||||
|
||||
Map<String, Integer> linkedHashMap = list.stream().collect(Collectors.toMap(
|
||||
Function.identity(),
|
||||
String::length,
|
||||
(u, v) -> u,
|
||||
LinkedHashMap::new
|
||||
));
|
||||
|
||||
Object[] keySet = linkedHashMap.keySet().toArray();
|
||||
|
||||
assertEquals("[A, BB, CCC]", Arrays.toString(keySet));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package org.baeldung.persistence.criteria.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.baeldung.persistence.criteria.model.Book;
|
||||
import org.baeldung.persistence.criteria.repository.BookRepositoryCustom;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class BookRepositoryImpl implements BookRepositoryCustom {
|
||||
|
||||
private EntityManager em;
|
||||
|
||||
public BookRepositoryImpl(EntityManager em) {
|
||||
this.em = em;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Book> findBooksByAuthorNameAndTitle(String authorName, String title) {
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<Book> cq = cb.createQuery(Book.class);
|
||||
|
||||
Root<Book> book = cq.from(Book.class);
|
||||
List<Predicate> predicates = new ArrayList<>();
|
||||
|
||||
if (authorName != null) {
|
||||
predicates.add(cb.equal(book.get("author"), authorName));
|
||||
}
|
||||
if (title != null) {
|
||||
predicates.add(cb.like(book.get("title"), "%" + title + "%"));
|
||||
}
|
||||
cq.where(predicates.toArray(new Predicate[0]));
|
||||
|
||||
TypedQuery<Book> query = em.createQuery(cq);
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package org.baeldung.persistence.criteria.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Book {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String author;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package org.baeldung.persistence.criteria.repository;
|
||||
|
||||
import org.baeldung.persistence.criteria.model.Book;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
public interface BookRepository extends JpaRepository<Book, Long>, BookRepositoryCustom, JpaSpecificationExecutor<Book> {
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package org.baeldung.persistence.criteria.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.criteria.model.Book;
|
||||
|
||||
public interface BookRepositoryCustom {
|
||||
|
||||
List<Book> findBooksByAuthorNameAndTitle(String authorName, String title);
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package org.baeldung.persistence.criteria.repository;
|
||||
|
||||
import static org.baeldung.persistence.criteria.repository.BookSpecifications.hasAuthor;
|
||||
import static org.baeldung.persistence.criteria.repository.BookSpecifications.titleContains;
|
||||
import static org.springframework.data.jpa.domain.Specifications.where;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.criteria.model.Book;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class BookService {
|
||||
|
||||
private BookRepository bookRepository;
|
||||
|
||||
public BookService(BookRepository bookRepository) {
|
||||
this.bookRepository = bookRepository;
|
||||
}
|
||||
|
||||
public List<Book> query(String author, String title) {
|
||||
return bookRepository.findAll(where(hasAuthor(author)).and(titleContains(title)));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package org.baeldung.persistence.criteria.repository;
|
||||
|
||||
import org.baeldung.persistence.criteria.model.Book;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
public class BookSpecifications {
|
||||
|
||||
public static Specification<Book> hasAuthor(String author) {
|
||||
return (book, cq, cb) -> cb.equal(book.get("author"), author);
|
||||
}
|
||||
|
||||
public static Specification<Book> titleContains(String title) {
|
||||
return (book, cq, cb) -> cb.like(book.get("title"), "%" + title + "%");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.event.listener;
|
||||
|
||||
import org.springframework.context.event.ContextStartedEvent;
|
||||
import org.springframework.context.event.ContextStoppedEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ContextEventListener {
|
||||
|
||||
@Order(2)
|
||||
@EventListener
|
||||
public void handleContextRefreshEvent(ContextStartedEvent ctxStartEvt) {
|
||||
System.out.println("Context Start Event received.");
|
||||
}
|
||||
|
||||
@Order(1)
|
||||
@EventListener(classes = { ContextStartedEvent.class, ContextStoppedEvent.class })
|
||||
public void handleMultipleEvents() {
|
||||
System.out.println("Multi-event listener invoked");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.event.listener;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "com.baeldung.event.listener")
|
||||
public class EventConfig {
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.event.listener;
|
||||
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
public class SpringRunner {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(EventConfig.class);
|
||||
ctx.start();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.thymeleaf.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import com.baeldung.thymeleaf.model.Course;
|
||||
|
||||
/**
|
||||
* Handles requests for the student model.
|
||||
*
|
||||
*/
|
||||
@Controller
|
||||
public class CourseRegistrationController {
|
||||
|
||||
@RequestMapping(value = "/registerCourse", method = RequestMethod.POST)
|
||||
public String register(@ModelAttribute Course course, Model model) {
|
||||
model.addAttribute("successMessage", "You have successfully registered for course: " + course.getName() + ".");
|
||||
return "courseRegistration.html";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/registerCourse", method = RequestMethod.GET)
|
||||
public String register(Model model) {
|
||||
model.addAttribute("course", new Course());
|
||||
return "courseRegistration.html";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.thymeleaf.model;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Course {
|
||||
|
||||
private String name;
|
||||
private String description;
|
||||
private Date startDate;
|
||||
private Date endDate;
|
||||
private String teacher;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Date getStartDate() {
|
||||
return startDate;
|
||||
}
|
||||
|
||||
public void setStartDate(Date startDate) {
|
||||
this.startDate = startDate;
|
||||
}
|
||||
|
||||
public Date getEndDate() {
|
||||
return endDate;
|
||||
}
|
||||
|
||||
public void setEndDate(Date endDate) {
|
||||
this.endDate = endDate;
|
||||
}
|
||||
|
||||
public String getTeacher() {
|
||||
return teacher;
|
||||
}
|
||||
|
||||
public void setTeacher(String teacher) {
|
||||
this.teacher = teacher;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Register for course</title>
|
||||
<script>
|
||||
function validateForm() {
|
||||
var e = document.getElementById("course");
|
||||
var value = e.options[e.selectedIndex].value;
|
||||
if (value == '') {
|
||||
var error = document.getElementById("errormesg");
|
||||
error.textContent = e.getAttribute('data-validation-message');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h3>Register Here</h3>
|
||||
<form action="#" th:action="@{/registerCourse}" th:object="${course}"
|
||||
method="post" onsubmit="return validateForm();">
|
||||
<span id="errormesg" style="color: red"></span> <span
|
||||
style="font-weight: bold" th:text="${successMessage}"></span>
|
||||
<table>
|
||||
<tr>
|
||||
<td><label th:text="#{msg.courseName}+': '"></label></td>
|
||||
<td><select id="course" th:field="*{name}"
|
||||
th:data-validation-message="#{msg.courseName.mandatory}">
|
||||
<option th:value="''" th:text="'Select'"></option>
|
||||
<option th:value="'Mathematics'" th:text="'Mathematics'"></option>
|
||||
<option th:value="'History'" th:text="'History'"></option>
|
||||
</select></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><input type="submit" value="Submit" /></td>
|
||||
</tr>
|
||||
</table>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
|
@ -24,6 +24,9 @@
|
|||
<tr>
|
||||
<td><a th:href="@{/listBooks}" th:text="#{msg.ListBooks}" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a th:href="@{/registerCourse}" th:text="#{msg.CourseRegistration}" /></td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
<?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.baeldung</groupId>
|
||||
<artifactId>StreamsOrdering</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.openjdk.jmh/jmh-core -->
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>1.21</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>1.21</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
Loading…
Reference in New Issue