commit
b59d268361
|
@ -106,7 +106,7 @@
|
|||
<arguments>
|
||||
<argument>java</argument>
|
||||
<argument>-jar</argument>
|
||||
<argument>sample-blade-app.jar</argument>
|
||||
<argument>blade.jar</argument>
|
||||
</arguments>
|
||||
</configuration>
|
||||
</execution>
|
||||
|
|
|
@ -73,6 +73,28 @@
|
|||
<version>${cache2k.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.moshi</groupId>
|
||||
<artifactId>moshi</artifactId>
|
||||
<version>${moshi.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.moshi</groupId>
|
||||
<artifactId>moshi-adapters</artifactId>
|
||||
<version>${moshi.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.jcabi</groupId>
|
||||
<artifactId>jcabi-aspects</artifactId>
|
||||
<version>${jcabi-aspects.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
<version>${aspectjrt.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
|
@ -81,6 +103,36 @@
|
|||
<url>https://jitpack.io</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<build>
|
||||
<finalName>libraries-3</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.jcabi</groupId>
|
||||
<artifactId>jcabi-maven-plugin</artifactId>
|
||||
<version>${jcabi-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>ajc</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjtools</artifactId>
|
||||
<version>${aspectjtools.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>${aspectjweaver.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<jcommander.version>1.78</jcommander.version>
|
||||
|
@ -93,5 +145,11 @@
|
|||
<cactoos.version>0.43</cactoos.version>
|
||||
<airline.version>2.7.2</airline.version>
|
||||
<cache2k.version>1.2.3.Final</cache2k.version>
|
||||
<moshi.version>1.9.2</moshi.version>
|
||||
<jcabi-aspects.version>0.22.6</jcabi-aspects.version>
|
||||
<aspectjrt.version>1.9.2</aspectjrt.version>
|
||||
<jcabi-maven-plugin.version>0.14.1</jcabi-maven-plugin.version>
|
||||
<aspectjtools.version>1.9.2</aspectjtools.version>
|
||||
<aspectjweaver.version>1.9.2</aspectjweaver.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -4,38 +4,33 @@ import java.util.Objects;
|
|||
|
||||
import org.cache2k.Cache;
|
||||
import org.cache2k.Cache2kBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ProductHelper {
|
||||
|
||||
final Logger LOGGER = LoggerFactory.getLogger(ProductHelper.class);
|
||||
|
||||
private Cache<String, Integer> cachedDiscounts;
|
||||
|
||||
private int cacheMissCount = 0;
|
||||
|
||||
public ProductHelper() {
|
||||
cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class)
|
||||
.name("discount")
|
||||
.eternal(true)
|
||||
.entryCapacity(100)
|
||||
.build();
|
||||
|
||||
initDiscountCache("Sports", 20);
|
||||
}
|
||||
|
||||
public void initDiscountCache(String productType, Integer value) {
|
||||
cachedDiscounts.put(productType, value);
|
||||
}
|
||||
|
||||
public Integer getDiscount(String productType) {
|
||||
Integer discount = cachedDiscounts.get(productType);
|
||||
if (Objects.isNull(discount)) {
|
||||
LOGGER.info("Discount for {} not found.", productType);
|
||||
discount = 0;
|
||||
} else {
|
||||
LOGGER.info("Discount for {} found.", productType);
|
||||
cacheMissCount++;
|
||||
discount = "Sports".equalsIgnoreCase(productType) ? 20 : 10;
|
||||
cachedDiscounts.put(productType, discount);
|
||||
}
|
||||
return discount;
|
||||
}
|
||||
|
||||
public int getCacheMissCount() {
|
||||
return cacheMissCount;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package com.baeldung.cache2k;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.cache2k.Cache;
|
||||
|
@ -14,28 +13,26 @@ public class ProductHelperUsingLoader {
|
|||
|
||||
private Cache<String, Integer> cachedDiscounts;
|
||||
|
||||
private int cacheMissCount = 0;
|
||||
|
||||
public ProductHelperUsingLoader() {
|
||||
cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class)
|
||||
.name("discount-loader")
|
||||
.eternal(false)
|
||||
.expireAfterWrite(10, TimeUnit.MILLISECONDS)
|
||||
.entryCapacity(100)
|
||||
.loader((key) -> {
|
||||
LOGGER.info("Calculating discount for {}.", key);
|
||||
cacheMissCount++;
|
||||
return "Sports".equalsIgnoreCase(key) ? 20 : 10;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
public Integer getDiscount(String productType) {
|
||||
Integer discount = cachedDiscounts.get(productType);
|
||||
if (Objects.isNull(discount)) {
|
||||
LOGGER.info("Discount for {} not found.", productType);
|
||||
discount = 0;
|
||||
} else {
|
||||
LOGGER.info("Discount for {} found.", productType);
|
||||
}
|
||||
return discount;
|
||||
return cachedDiscounts.get(productType);
|
||||
}
|
||||
|
||||
public int getCacheMissCount() {
|
||||
return cacheMissCount;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package com.baeldung.cache2k;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.cache2k.Cache;
|
||||
|
@ -16,14 +15,15 @@ public class ProductHelperWithEventListener {
|
|||
|
||||
private Cache<String, Integer> cachedDiscounts;
|
||||
|
||||
private int cacheMissCount = 0;
|
||||
|
||||
public ProductHelperWithEventListener() {
|
||||
cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class)
|
||||
.name("discount-listener")
|
||||
.eternal(false)
|
||||
.expireAfterWrite(10, TimeUnit.MILLISECONDS)
|
||||
.entryCapacity(100)
|
||||
.loader((key) -> {
|
||||
LOGGER.info("Calculating discount for {}.", key);
|
||||
cacheMissCount++;
|
||||
return "Sports".equalsIgnoreCase(key) ? 20 : 10;
|
||||
})
|
||||
.addListener(new CacheEntryCreatedListener<String, Integer>() {
|
||||
|
@ -36,14 +36,11 @@ public class ProductHelperWithEventListener {
|
|||
}
|
||||
|
||||
public Integer getDiscount(String productType) {
|
||||
Integer discount = cachedDiscounts.get(productType);
|
||||
if (Objects.isNull(discount)) {
|
||||
LOGGER.info("Discount for {} not found.", productType);
|
||||
discount = 0;
|
||||
} else {
|
||||
LOGGER.info("Discount for {} found.", productType);
|
||||
}
|
||||
return discount;
|
||||
return cachedDiscounts.get(productType);
|
||||
}
|
||||
|
||||
public int getCacheMissCount() {
|
||||
return cacheMissCount;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,39 +5,34 @@ import java.util.concurrent.TimeUnit;
|
|||
|
||||
import org.cache2k.Cache;
|
||||
import org.cache2k.Cache2kBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ProductHelperWithExpiry {
|
||||
|
||||
final Logger LOGGER = LoggerFactory.getLogger(ProductHelperWithExpiry.class);
|
||||
|
||||
private Cache<String, Integer> cachedDiscounts;
|
||||
|
||||
private int cacheMissCount = 0;
|
||||
|
||||
public ProductHelperWithExpiry() {
|
||||
cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class)
|
||||
.name("discount-expiry")
|
||||
.eternal(false)
|
||||
.expireAfterWrite(5, TimeUnit.MILLISECONDS)
|
||||
.entryCapacity(100)
|
||||
.build();
|
||||
|
||||
initDiscountCache("Sports", 20);
|
||||
}
|
||||
|
||||
public void initDiscountCache(String productType, Integer value) {
|
||||
cachedDiscounts.put(productType, value);
|
||||
}
|
||||
|
||||
public Integer getDiscount(String productType) {
|
||||
Integer discount = cachedDiscounts.get(productType);
|
||||
if (Objects.isNull(discount)) {
|
||||
LOGGER.info("Discount for {} not found.", productType);
|
||||
discount = 0;
|
||||
} else {
|
||||
LOGGER.info("Discount for {} found.", productType);
|
||||
cacheMissCount++;
|
||||
discount = "Sports".equalsIgnoreCase(productType) ? 20 : 10;
|
||||
cachedDiscounts.put(productType, discount);
|
||||
}
|
||||
return discount;
|
||||
}
|
||||
|
||||
public int getCacheMissCount() {
|
||||
return cacheMissCount;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,112 @@
|
|||
package com.baeldung.jcabi;
|
||||
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.jcabi.aspects.Async;
|
||||
import com.jcabi.aspects.Cacheable;
|
||||
import com.jcabi.aspects.LogExceptions;
|
||||
import com.jcabi.aspects.Loggable;
|
||||
import com.jcabi.aspects.Quietly;
|
||||
import com.jcabi.aspects.RetryOnFailure;
|
||||
import com.jcabi.aspects.UnitedThrow;
|
||||
|
||||
public class JcabiAspectJ {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
displayFactorial(10);
|
||||
getFactorial(10).get();
|
||||
|
||||
String result = cacheExchangeRates();
|
||||
if (result != cacheExchangeRates()) {
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
divideByZero();
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
divideByZeroQuietly();
|
||||
try {
|
||||
processFile();
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Loggable
|
||||
@Async
|
||||
public static void displayFactorial(int number) {
|
||||
long result = factorial(number);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
@Loggable
|
||||
@Async
|
||||
public static Future<Long> getFactorial(int number) {
|
||||
Future<Long> factorialFuture = CompletableFuture.supplyAsync(() -> factorial(number));
|
||||
return factorialFuture;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds factorial of a number
|
||||
* @param number
|
||||
* @return
|
||||
*/
|
||||
public static long factorial(int number) {
|
||||
long result = 1;
|
||||
for(int i=number;i>0;i--) {
|
||||
result *= i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Loggable
|
||||
@Cacheable(lifetime = 2, unit = TimeUnit.SECONDS)
|
||||
public static String cacheExchangeRates() {
|
||||
String result = null;
|
||||
try {
|
||||
URL exchangeRateUrl = new URL("https://api.exchangeratesapi.io/latest");
|
||||
URLConnection con = exchangeRateUrl.openConnection();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||
result = in.readLine();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@LogExceptions
|
||||
public static void divideByZero() {
|
||||
int x = 1/0;
|
||||
}
|
||||
|
||||
@RetryOnFailure(attempts = 2, types = {java.lang.NumberFormatException.class})
|
||||
@Quietly
|
||||
public static void divideByZeroQuietly() {
|
||||
int x = 1/0;
|
||||
}
|
||||
|
||||
@UnitedThrow(IllegalStateException.class)
|
||||
public static void processFile() throws IOException, InterruptedException {
|
||||
BufferedReader reader = new BufferedReader(new FileReader("baeldung.txt"));
|
||||
reader.readLine();
|
||||
|
||||
Thread thread = new Thread();
|
||||
thread.wait(2000);
|
||||
}
|
||||
|
||||
}
|
|
@ -6,12 +6,13 @@ import org.junit.Test;
|
|||
|
||||
public class ProductHelperUnitTest {
|
||||
|
||||
ProductHelper productHelper = new ProductHelper();
|
||||
|
||||
@Test
|
||||
public void whenInvokedGetDiscount_thenGetItFromCache() {
|
||||
public void whenInvokedGetDiscountTwice_thenGetItFromCache() {
|
||||
ProductHelper productHelper = new ProductHelper();
|
||||
assertTrue(productHelper.getCacheMissCount() == 0);
|
||||
assertTrue(productHelper.getDiscount("Sports") == 20);
|
||||
assertTrue(productHelper.getDiscount("Electronics") == 0);
|
||||
assertTrue(productHelper.getDiscount("Sports") == 20);
|
||||
assertTrue(productHelper.getCacheMissCount() == 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,12 +6,16 @@ import org.junit.Test;
|
|||
|
||||
public class ProductHelperUsingLoaderUnitTest {
|
||||
|
||||
ProductHelperUsingLoader productHelper = new ProductHelperUsingLoader();
|
||||
|
||||
@Test
|
||||
public void whenInvokedGetDiscount_thenPopulateCache() {
|
||||
public void whenInvokedGetDiscount_thenPopulateCacheUsingLoader() {
|
||||
ProductHelperUsingLoader productHelper = new ProductHelperUsingLoader();
|
||||
assertTrue(productHelper.getCacheMissCount() == 0);
|
||||
|
||||
assertTrue(productHelper.getDiscount("Sports") == 20);
|
||||
assertTrue(productHelper.getCacheMissCount() == 1);
|
||||
|
||||
assertTrue(productHelper.getDiscount("Electronics") == 10);
|
||||
assertTrue(productHelper.getCacheMissCount() == 2);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,10 +6,9 @@ import org.junit.Test;
|
|||
|
||||
public class ProductHelperWithEventListenerUnitTest {
|
||||
|
||||
ProductHelperWithEventListener productHelper = new ProductHelperWithEventListener();
|
||||
|
||||
@Test
|
||||
public void whenEntryAddedInCache_thenEventListenerCalled() {
|
||||
ProductHelperWithEventListener productHelper = new ProductHelperWithEventListener();
|
||||
assertTrue(productHelper.getDiscount("Sports") == 20);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,13 +6,17 @@ import org.junit.Test;
|
|||
|
||||
public class ProductHelperWithExpiryUnitTest {
|
||||
|
||||
ProductHelperWithExpiry productHelper = new ProductHelperWithExpiry();
|
||||
|
||||
@Test
|
||||
public void whenInvokedGetDiscountForExpiredProduct_thenNoDiscount() throws InterruptedException {
|
||||
public void whenInvokedGetDiscountAfterExpiration_thenDiscountCalculatedAgain() throws InterruptedException {
|
||||
ProductHelperWithExpiry productHelper = new ProductHelperWithExpiry();
|
||||
assertTrue(productHelper.getCacheMissCount() == 0);
|
||||
assertTrue(productHelper.getDiscount("Sports") == 20);
|
||||
assertTrue(productHelper.getCacheMissCount() == 1);
|
||||
|
||||
Thread.sleep(20);
|
||||
assertTrue(productHelper.getDiscount("Sports") == 0);
|
||||
|
||||
assertTrue(productHelper.getDiscount("Sports") == 20);
|
||||
assertTrue(productHelper.getCacheMissCount() == 2);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,105 @@
|
|||
package com.baeldung.moshi;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.time.Instant;
|
||||
|
||||
import com.squareup.moshi.FromJson;
|
||||
import com.squareup.moshi.JsonAdapter;
|
||||
import com.squareup.moshi.JsonQualifier;
|
||||
import com.squareup.moshi.Moshi;
|
||||
import com.squareup.moshi.ToJson;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AlternativeAdapterUnitTest {
|
||||
@Test
|
||||
public void whenSerializing_thenAlternativeAdapterUsed() {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(new EpochMillisAdapter())
|
||||
.build();
|
||||
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
|
||||
|
||||
String json = jsonAdapter.toJson(new Post("Introduction to Moshi Json", "Baeldung", Instant.now()));
|
||||
System.out.println(json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeserializing_thenAlternativeAdapterUsed() throws IOException {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(new EpochMillisAdapter())
|
||||
.build();
|
||||
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
|
||||
|
||||
String json = "{\"author\":\"Baeldung\",\"posted\":1582095269204,\"title\":\"Introduction to Moshi Json\"}";
|
||||
Post post = jsonAdapter.fromJson(json);
|
||||
System.out.println(post);
|
||||
|
||||
}
|
||||
|
||||
public static class Post {
|
||||
String title;
|
||||
String author;
|
||||
@EpochMillis Instant posted;
|
||||
|
||||
public Post() {
|
||||
}
|
||||
|
||||
public Post(String title, String author, Instant posted) {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.posted = posted;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public Instant getPosted() {
|
||||
return posted;
|
||||
}
|
||||
|
||||
public void setPosted(Instant posted) {
|
||||
this.posted = posted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringBuilder(this).append("title", title).append("author", author).append("posted", posted)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
|
||||
@JsonQualifier
|
||||
public @interface EpochMillis {
|
||||
}
|
||||
|
||||
public static class EpochMillisAdapter {
|
||||
@ToJson
|
||||
public Long toJson(@EpochMillis Instant input) {
|
||||
return input.toEpochMilli();
|
||||
}
|
||||
@FromJson
|
||||
@EpochMillis
|
||||
public Instant fromJson(Long input) {
|
||||
return Instant.ofEpochMilli(input);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.moshi;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.squareup.moshi.JsonAdapter;
|
||||
import com.squareup.moshi.Moshi;
|
||||
import com.squareup.moshi.Types;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ArrayUnitTest {
|
||||
@Test
|
||||
public void whenSerializingList_thenJsonArrayProduced() {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.build();
|
||||
Type type = Types.newParameterizedType(List.class, String.class);
|
||||
JsonAdapter<List<String>> jsonAdapter = moshi.adapter(type);
|
||||
|
||||
String json = jsonAdapter.toJson(Arrays.asList("One", "Two", "Three"));
|
||||
System.out.println(json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeserializingJsonArray_thenListProduced() throws IOException {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.build();
|
||||
Type type = Types.newParameterizedType(List.class, String.class);
|
||||
JsonAdapter<List<String>> jsonAdapter = moshi.adapter(type);
|
||||
|
||||
String json = "[\"One\",\"Two\",\"Three\"]";
|
||||
List<String> result = jsonAdapter.fromJson(json);
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
package com.baeldung.moshi;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import com.squareup.moshi.FromJson;
|
||||
import com.squareup.moshi.JsonAdapter;
|
||||
import com.squareup.moshi.Moshi;
|
||||
import com.squareup.moshi.ToJson;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ComplexAdapterUnitTest {
|
||||
@Test
|
||||
public void whenSerializing_thenCorrectJsonProduced() {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(new JsonDateTimeAdapter())
|
||||
.build();
|
||||
JsonAdapter<ZonedDateTime> jsonAdapter = moshi.adapter(ZonedDateTime.class);
|
||||
|
||||
String json = jsonAdapter.toJson(ZonedDateTime.now());
|
||||
System.out.println(json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeserializing_thenCorrectJsonConsumed() throws IOException {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(new JsonDateTimeAdapter())
|
||||
.build();
|
||||
JsonAdapter<ZonedDateTime> jsonAdapter = moshi.adapter(ZonedDateTime.class);
|
||||
|
||||
String json = "{\"date\":\"2020-02-17\",\"time\":\"07:53:27.064\",\"timezone\":\"Europe/London\"}";
|
||||
ZonedDateTime now = jsonAdapter.fromJson(json);
|
||||
System.out.println(now);
|
||||
|
||||
}
|
||||
|
||||
public static class JsonDateTimeAdapter {
|
||||
@ToJson
|
||||
public JsonDateTime toJson(ZonedDateTime input) {
|
||||
String date = input.toLocalDate().toString();
|
||||
String time = input.toLocalTime().toString();
|
||||
String timezone = input.getZone().toString();
|
||||
return new JsonDateTime(date, time, timezone);
|
||||
}
|
||||
@FromJson
|
||||
public ZonedDateTime fromJson(JsonDateTime input) {
|
||||
LocalDate date = LocalDate.parse(input.getDate());
|
||||
LocalTime time = LocalTime.parse(input.getTime());
|
||||
ZoneId timezone = ZoneId.of(input.getTimezone());
|
||||
return ZonedDateTime.of(date, time, timezone);
|
||||
}
|
||||
}
|
||||
public static class JsonDateTime {
|
||||
private String date;
|
||||
private String time;
|
||||
private String timezone;
|
||||
|
||||
public JsonDateTime() {
|
||||
}
|
||||
|
||||
public JsonDateTime(String date, String time, String timezone) {
|
||||
this.date = date;
|
||||
this.time = time;
|
||||
this.timezone = timezone;
|
||||
}
|
||||
|
||||
public String getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(String date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public String getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(String time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public String getTimezone() {
|
||||
return timezone;
|
||||
}
|
||||
|
||||
public void setTimezone(String timezone) {
|
||||
this.timezone = timezone;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.baeldung.moshi;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
|
||||
import com.squareup.moshi.JsonAdapter;
|
||||
import com.squareup.moshi.Moshi;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DefaultUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenDeserializing_thenFieldsGetDefaultValues() throws IOException {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.build();
|
||||
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
|
||||
|
||||
String json = "{\"title\":\"My Post\"}";
|
||||
Post post = jsonAdapter.fromJson(json);
|
||||
System.out.println(post);
|
||||
}
|
||||
public static class Post {
|
||||
private String title;
|
||||
private String author;
|
||||
private String posted;
|
||||
|
||||
public Post() {
|
||||
posted = Instant.now().toString();
|
||||
}
|
||||
|
||||
public Post(String title, String author, String posted) {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.posted = posted;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String getPosted() {
|
||||
return posted;
|
||||
}
|
||||
|
||||
public void setPosted(String posted) {
|
||||
this.posted = posted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringBuilder(this).append("title", title).append("author", author).append("posted", posted)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
package com.baeldung.moshi;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.squareup.moshi.JsonAdapter;
|
||||
import com.squareup.moshi.Moshi;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PrimitiveUnitTest {
|
||||
@Test
|
||||
public void whenSerializing_thenCorrectJsonProduced() {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.build();
|
||||
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
|
||||
|
||||
Post post = new Post("My Post", "Baeldung", "This is my post");
|
||||
String json = jsonAdapter.toJson(post);
|
||||
System.out.println(json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeserializing_thenCorrectJsonConsumed() throws IOException {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.build();
|
||||
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
|
||||
|
||||
String json = "{\"author\":\"Baeldung\",\"text\":\"This is my post\",\"title\":\"My Post\"}";
|
||||
Post post = jsonAdapter.fromJson(json);
|
||||
System.out.println(post);
|
||||
}
|
||||
|
||||
public static class Post {
|
||||
private String title;
|
||||
private String author;
|
||||
private String text;
|
||||
|
||||
public Post() {
|
||||
}
|
||||
|
||||
public Post(String title, String author, String text) {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringBuilder(this).append("title", title).append("author", author).append("text", text)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.baeldung.moshi;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.squareup.moshi.Json;
|
||||
import com.squareup.moshi.JsonAdapter;
|
||||
import com.squareup.moshi.Moshi;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class RenameUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializing_thenFieldsGetRenamed() {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.build();
|
||||
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
|
||||
|
||||
Post post = new Post("My Post", "Baeldung");
|
||||
String json = jsonAdapter.toJson(post);
|
||||
System.out.println(json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializing_thenRenamedFieldsGetConsumed() throws IOException {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.build();
|
||||
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
|
||||
|
||||
String json = "{\"authored_by\":\"Baeldung\",\"title\":\"My Post\"}";
|
||||
Post post = jsonAdapter.fromJson(json);
|
||||
System.out.println(post);
|
||||
}
|
||||
public static class Post {
|
||||
private String title;
|
||||
@Json(name = "authored_by")
|
||||
private String author;
|
||||
|
||||
public Post() {
|
||||
}
|
||||
|
||||
public Post(String title, String author) {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringBuilder(this).append("title", title).append("author", author).toString();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,129 @@
|
|||
package com.baeldung.moshi;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.squareup.moshi.FromJson;
|
||||
import com.squareup.moshi.JsonAdapter;
|
||||
import com.squareup.moshi.Moshi;
|
||||
import com.squareup.moshi.ToJson;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SimpleAdapterUnitTest {
|
||||
@Test
|
||||
public void whenSerializing_thenAdapterUsed() {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(new AuthorAdapter())
|
||||
.build();
|
||||
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
|
||||
|
||||
Post post = new Post("My Post", new Author("Baeldung", "baeldung@example.com"), "This is my post");
|
||||
String json = jsonAdapter.toJson(post);
|
||||
System.out.println(json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeserializing_thenAdapterUsed() throws IOException {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(new AuthorAdapter())
|
||||
.build();
|
||||
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
|
||||
|
||||
String json = "{\"author\":\"Baeldung <baeldung@example.com>\",\"text\":\"This is my post\",\"title\":\"My Post\"}";
|
||||
Post post = jsonAdapter.fromJson(json);
|
||||
System.out.println(post);
|
||||
}
|
||||
public static class AuthorAdapter {
|
||||
private Pattern pattern = Pattern.compile("^(.*) <(.*)>$");
|
||||
@ToJson
|
||||
public String toJson(Author author) {
|
||||
return author.name + " <" + author.email + ">";
|
||||
}
|
||||
|
||||
@FromJson
|
||||
public Author fromJson(String author) {
|
||||
Matcher matcher = pattern.matcher(author);
|
||||
return matcher.find() ? new Author(matcher.group(1), matcher.group(2)) : null;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Author {
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
public Author() {
|
||||
}
|
||||
|
||||
public Author(String name, String email) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringBuilder(this).append("name", name).append("email", email).toString();
|
||||
}
|
||||
}
|
||||
public static class Post {
|
||||
private String title;
|
||||
private Author author;
|
||||
private String text;
|
||||
|
||||
public Post() {
|
||||
}
|
||||
|
||||
public Post(String title, Author author, String text) {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Author getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(Author author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringBuilder(this).append("title", title).append("author", author).append("text", text)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package com.baeldung.moshi;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.squareup.moshi.JsonAdapter;
|
||||
import com.squareup.moshi.Moshi;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TransientUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializing_thenTransientFieldIgnored() {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.build();
|
||||
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
|
||||
|
||||
Post post = new Post("My Post", "Baeldung");
|
||||
String json = jsonAdapter.toJson(post);
|
||||
System.out.println(json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeserializing_thenTransientFieldIgnored() throws IOException {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.build();
|
||||
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
|
||||
|
||||
String json = "{\"authored_by\":\"Baeldung\",\"title\":\"My Post\"}";
|
||||
Post post = jsonAdapter.fromJson(json);
|
||||
System.out.println(post);
|
||||
}
|
||||
public static class Post {
|
||||
private String title;
|
||||
private transient String author;
|
||||
|
||||
public Post() {
|
||||
}
|
||||
|
||||
public Post(String title, String author) {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringBuilder(this).append("title", title).append("author", author).toString();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,8 +4,8 @@ This module contains articles about Spring with Hibernate 3
|
|||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Hibernate 3 with Spring](http://www.baeldung.com/hibernate3-spring)
|
||||
- [HibernateException: No Hibernate Session Bound to Thread in Hibernate 3](http://www.baeldung.com/no-hibernate-session-bound-to-thread-exception)
|
||||
- [Hibernate 3 with Spring](https://www.baeldung.com/hibernate3-spring)
|
||||
- [HibernateException: No Hibernate Session Bound to Thread in Hibernate 3](https://www.baeldung.com/no-hibernate-session-bound-to-thread-exception)
|
||||
|
||||
### Quick Start
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
|
@ -1,18 +1,18 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
|
||||
|
||||
import org.baeldung.persistence.model.Event;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class EventDao extends AbstractHibernateDao<Event> implements IEventDao {
|
||||
|
||||
public EventDao() {
|
||||
super();
|
||||
|
||||
setClazz(Event.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
}
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
|
||||
import com.baeldung.persistence.model.Event;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class EventDao extends AbstractHibernateDao<Event> implements IEventDao {
|
||||
|
||||
public EventDao() {
|
||||
super();
|
||||
|
||||
setClazz(Event.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.persistence.dao;
|
||||
|
||||
import com.baeldung.persistence.model.Event;
|
||||
|
||||
|
||||
public interface IEventDao extends IOperations<Event> {
|
||||
//
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.persistence.dao;
|
||||
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
|
||||
public interface IFooDao extends IOperations<Foo> {
|
||||
//
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
|
@ -1,45 +1,45 @@
|
|||
package org.baeldung.persistence.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "EVENTS")
|
||||
public class Event implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String description;
|
||||
|
||||
public Event() {
|
||||
}
|
||||
|
||||
|
||||
public Event(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "EVENTS")
|
||||
public class Event implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String description;
|
||||
|
||||
public Event() {
|
||||
}
|
||||
|
||||
|
||||
public Event(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.model;
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
|
@ -1,27 +1,27 @@
|
|||
package org.baeldung.persistence.service;
|
||||
|
||||
|
||||
import org.baeldung.persistence.dao.IEventDao;
|
||||
import org.baeldung.persistence.model.Event;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class EventService {
|
||||
|
||||
@Autowired
|
||||
private IEventDao dao;
|
||||
|
||||
public EventService() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public void create(final Event entity) {
|
||||
dao.create(entity);
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
|
||||
import com.baeldung.persistence.model.Event;
|
||||
import com.baeldung.persistence.dao.IEventDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class EventService {
|
||||
|
||||
@Autowired
|
||||
private IEventDao dao;
|
||||
|
||||
public EventService() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public void create(final Event entity) {
|
||||
dao.create(entity);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import org.baeldung.persistence.dao.IFooDao;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.persistence.dao.IFooDao;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.spring;
|
||||
package com.baeldung.spring;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
|
@ -22,7 +22,7 @@ import com.google.common.base.Preconditions;
|
|||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@PropertySource({ "classpath:persistence-h2.properties" })
|
||||
@ComponentScan({ "org.baeldung.persistence.dao", "org.baeldung.persistence.service" })
|
||||
@ComponentScan({ "com.baeldung.persistence.dao", "com.baeldung.persistence.service" })
|
||||
public class PersistenceConfig {
|
||||
|
||||
@Autowired
|
||||
|
@ -36,7 +36,7 @@ public class PersistenceConfig {
|
|||
public AnnotationSessionFactoryBean sessionFactory() {
|
||||
final AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();
|
||||
sessionFactory.setDataSource(dataSource());
|
||||
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
||||
sessionFactory.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
|
||||
sessionFactory.setHibernateProperties(hibernateProperties());
|
||||
|
||||
return sessionFactory;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.spring;
|
||||
package com.baeldung.spring;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
|
@ -24,7 +24,7 @@ import com.google.common.base.Preconditions;
|
|||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@PropertySource({ "classpath:persistence-h2.properties" })
|
||||
@ComponentScan({ "org.baeldung.persistence.dao", "org.baeldung.persistence.service" })
|
||||
@ComponentScan({ "com.baeldung.persistence.dao", "com.baeldung.persistence.service" })
|
||||
public class PersistenceConfigHibernate3 {
|
||||
|
||||
@Autowired
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.spring;
|
||||
package com.baeldung.spring;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
|
@ -6,7 +6,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|||
|
||||
// @Configuration
|
||||
@EnableTransactionManagement
|
||||
@ComponentScan({ "org.baeldung.persistence.dao", "org.baeldung.persistence.service" })
|
||||
@ComponentScan({ "com.baeldung.persistence.dao", "com.baeldung.persistence.service" })
|
||||
@ImportResource({ "classpath:persistenceConfig.xml" })
|
||||
public class PersistenceXmlConfig {
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.model.Event;
|
||||
|
||||
|
||||
|
||||
public interface IEventDao extends IOperations<Event> {
|
||||
//
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
|
||||
public interface IFooDao extends IOperations<Foo> {
|
||||
//
|
||||
}
|
|
@ -4,6 +4,6 @@
|
|||
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
<hibernate-configuration>
|
||||
<session-factory>
|
||||
<mapping class="org.baeldung.persistence.model.Event" />
|
||||
<mapping class="com.baeldung.persistence.model.Event" />
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
|
||||
|
||||
<context:annotation-config />
|
||||
<context:component-scan base-package="org.baeldung.persistence" />
|
||||
<context:component-scan base-package="com.baeldung.persistence" />
|
||||
<context:property-placeholder location="classpath:persistence-h2.properties"/>
|
||||
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="packagesToScan" value="org.baeldung.persistence.model"/>
|
||||
<property name="packagesToScan" value="com.baeldung.persistence.model"/>
|
||||
<property name="hibernateProperties">
|
||||
<props>
|
||||
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
</context-param>
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>org.baeldung.spring</param-value>
|
||||
<param-value>com.baeldung.spring</param-value>
|
||||
</context-param>
|
||||
<listener>
|
||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.baeldung.spring.PersistenceConfig;
|
||||
import com.baeldung.spring.PersistenceConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
|
@ -1,10 +1,9 @@
|
|||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.baeldung.persistence.service.FooService;
|
||||
import org.baeldung.spring.PersistenceConfig;
|
||||
import com.baeldung.spring.PersistenceConfig;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
@ -1,7 +1,7 @@
|
|||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import org.baeldung.persistence.model.Event;
|
||||
import org.baeldung.spring.PersistenceConfigHibernate3;
|
||||
import com.baeldung.persistence.model.Event;
|
||||
import com.baeldung.spring.PersistenceConfigHibernate3;
|
||||
import org.hamcrest.core.IsInstanceOf;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.junit.Ignore;
|
|
@ -1,7 +1,7 @@
|
|||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import org.baeldung.persistence.model.Event;
|
||||
import org.baeldung.spring.PersistenceConfig;
|
||||
import com.baeldung.persistence.model.Event;
|
||||
import com.baeldung.spring.PersistenceConfig;
|
||||
import org.hamcrest.core.IsInstanceOf;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.junit.Ignore;
|
|
@ -1,42 +1,42 @@
|
|||
package org.baeldung.persistence.service;
|
||||
|
||||
import org.baeldung.persistence.model.Event;
|
||||
import org.baeldung.spring.PersistenceXmlConfig;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.orm.hibernate3.HibernateSystemException;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceXmlConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
EventService service;
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedEx = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public final void whenEntityIsCreated_thenNoExceptions() {
|
||||
service.create(new Event("from Annotation Session Bean Factory"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public final void whenNoTransBoundToSession_thenException() {
|
||||
expectedEx.expect(HibernateSystemException.class);
|
||||
expectedEx.expectMessage("No Hibernate Session bound to thread, "
|
||||
+ "and configuration does not allow creation of "
|
||||
+ "non-transactional one here");
|
||||
service.create(new Event("from Annotation Session Bean Factory"));
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import com.baeldung.persistence.model.Event;
|
||||
import com.baeldung.spring.PersistenceXmlConfig;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.orm.hibernate3.HibernateSystemException;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceXmlConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
EventService service;
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedEx = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public final void whenEntityIsCreated_thenNoExceptions() {
|
||||
service.create(new Event("from Annotation Session Bean Factory"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public final void whenNoTransBoundToSession_thenException() {
|
||||
expectedEx.expect(HibernateSystemException.class);
|
||||
expectedEx.expectMessage("No Hibernate Session bound to thread, "
|
||||
+ "and configuration does not allow creation of "
|
||||
+ "non-transactional one here");
|
||||
service.create(new Event("from Annotation Session Bean Factory"));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,39 +1,38 @@
|
|||
package org.baeldung.persistence.service;
|
||||
|
||||
import org.baeldung.persistence.model.Event;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.orm.hibernate3.HibernateSystemException;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "classpath:exceptionDemoPersistenceConfig.xml" })
|
||||
public class NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
EventService service;
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedEx = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public final void whenEntityIsCreated_thenNoExceptions() {
|
||||
service.create(new Event("from local session bean factory"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public final void whenNoTransBoundToSession_thenException() {
|
||||
expectedEx.expect(HibernateException.class);
|
||||
expectedEx.expectMessage("No Hibernate Session bound to thread, "
|
||||
+ "and configuration does not allow creation "
|
||||
+ "of non-transactional one here");
|
||||
service.create(new Event("from local session bean factory"));
|
||||
}
|
||||
}
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import com.baeldung.persistence.model.Event;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "classpath:exceptionDemoPersistenceConfig.xml" })
|
||||
public class NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
EventService service;
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedEx = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public final void whenEntityIsCreated_thenNoExceptions() {
|
||||
service.create(new Event("from local session bean factory"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public final void whenNoTransBoundToSession_thenException() {
|
||||
expectedEx.expect(HibernateException.class);
|
||||
expectedEx.expectMessage("No Hibernate Session bound to thread, "
|
||||
+ "and configuration does not allow creation "
|
||||
+ "of non-transactional one here");
|
||||
service.create(new Event("from local session bean factory"));
|
||||
}
|
||||
}
|
|
@ -4,11 +4,11 @@ This module contains articles about Hibernate 5 with Spring.
|
|||
|
||||
### Relevant articles
|
||||
|
||||
- [Hibernate Many to Many Annotation Tutorial](http://www.baeldung.com/hibernate-many-to-many)
|
||||
- [Programmatic Transactions in the Spring TestContext Framework](http://www.baeldung.com/spring-test-programmatic-transactions)
|
||||
- [JPA Criteria Queries](http://www.baeldung.com/hibernate-criteria-queries)
|
||||
- [Introduction to Hibernate Search](http://www.baeldung.com/hibernate-search)
|
||||
- [Hibernate Many to Many Annotation Tutorial](https://www.baeldung.com/hibernate-many-to-many)
|
||||
- [Programmatic Transactions in the Spring TestContext Framework](https://www.baeldung.com/spring-test-programmatic-transactions)
|
||||
- [JPA Criteria Queries](https://www.baeldung.com/hibernate-criteria-queries)
|
||||
- [Introduction to Hibernate Search](https://www.baeldung.com/hibernate-search)
|
||||
- [@DynamicUpdate with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-dynamicupdate)
|
||||
- [Hibernate Second-Level Cache](http://www.baeldung.com/hibernate-second-level-cache)
|
||||
- [Deleting Objects with Hibernate](http://www.baeldung.com/delete-with-hibernate)
|
||||
- [Spring, Hibernate and a JNDI Datasource](http://www.baeldung.com/spring-persistence-jpa-jndi-datasource)
|
||||
- [Hibernate Second-Level Cache](https://www.baeldung.com/hibernate-second-level-cache)
|
||||
- [Deleting Objects with Hibernate](https://www.baeldung.com/delete-with-hibernate)
|
||||
- [Spring, Hibernate and a JNDI Datasource](https://www.baeldung.com/spring-persistence-jpa-jndi-datasource)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
|
@ -3,13 +3,13 @@
|
|||
This module contains articles about Spring with Hibernate 4
|
||||
|
||||
### Relevant Articles:
|
||||
- [Guide to Hibernate 4 with Spring](http://www.baeldung.com/hibernate-4-spring)
|
||||
- [Hibernate Pagination](http://www.baeldung.com/hibernate-pagination)
|
||||
- [Sorting with Hibernate](http://www.baeldung.com/hibernate-sort)
|
||||
- [Stored Procedures with Hibernate](http://www.baeldung.com/stored-procedures-with-hibernate-tutorial)
|
||||
- [Hibernate: save, persist, update, merge, saveOrUpdate](http://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate)
|
||||
- [Eager/Lazy Loading In Hibernate](http://www.baeldung.com/hibernate-lazy-eager-loading)
|
||||
- [The DAO with Spring and Hibernate](http://www.baeldung.com/persistence-layer-with-spring-and-hibernate)
|
||||
- [Guide to Hibernate 4 with Spring](https://www.baeldung.com/hibernate-4-spring)
|
||||
- [Hibernate Pagination](https://www.baeldung.com/hibernate-pagination)
|
||||
- [Sorting with Hibernate](https://www.baeldung.com/hibernate-sort)
|
||||
- [Stored Procedures with Hibernate](https://www.baeldung.com/stored-procedures-with-hibernate-tutorial)
|
||||
- [Hibernate: save, persist, update, merge, saveOrUpdate](https://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate)
|
||||
- [Eager/Lazy Loading In Hibernate](https://www.baeldung.com/hibernate-lazy-eager-loading)
|
||||
- [The DAO with Spring and Hibernate](https://www.baeldung.com/persistence-layer-with-spring-and-hibernate)
|
||||
- [Auditing with JPA, Hibernate, and Spring Data JPA](https://www.baeldung.com/database-auditing-jpa)
|
||||
|
||||
### Quick Start
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung;
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
|
@ -4,13 +4,13 @@
|
|||
|
||||
|
||||
### Relevant Articles:
|
||||
- [The DAO with JPA and Spring](http://www.baeldung.com/spring-dao-jpa)
|
||||
- [JPA Pagination](http://www.baeldung.com/jpa-pagination)
|
||||
- [Sorting with JPA](http://www.baeldung.com/jpa-sort)
|
||||
- [Self-Contained Testing Using an In-Memory Database](http://www.baeldung.com/spring-jpa-test-in-memory-database)
|
||||
- [A Guide to Spring AbstractRoutingDatasource](http://www.baeldung.com/spring-abstract-routing-data-source)
|
||||
- [Obtaining Auto-generated Keys in Spring JDBC](http://www.baeldung.com/spring-jdbc-autogenerated-keys)
|
||||
- [Transactions with Spring 4 and JPA](http://www.baeldung.com/transaction-configuration-with-jpa-and-spring)
|
||||
- [The DAO with JPA and Spring](https://www.baeldung.com/spring-dao-jpa)
|
||||
- [JPA Pagination](https://www.baeldung.com/jpa-pagination)
|
||||
- [Sorting with JPA](https://www.baeldung.com/jpa-sort)
|
||||
- [Self-Contained Testing Using an In-Memory Database](https://www.baeldung.com/spring-jpa-test-in-memory-database)
|
||||
- [A Guide to Spring AbstractRoutingDatasource](https://www.baeldung.com/spring-abstract-routing-data-source)
|
||||
- [Obtaining Auto-generated Keys in Spring JDBC](https://www.baeldung.com/spring-jdbc-autogenerated-keys)
|
||||
- [Transactions with Spring 4 and JPA](https://www.baeldung.com/transaction-configuration-with-jpa-and-spring)
|
||||
- [Use Criteria Queries in a Spring Data Application](https://www.baeldung.com/spring-data-criteria-queries)
|
||||
- [Many-To-Many Relationship in JPA](https://www.baeldung.com/jpa-many-to-many)
|
||||
- [Spring Persistence (Hibernate and JPA) with a JNDI datasource](https://www.baeldung.com/spring-persistence-hibernate-and-jpa-with-a-jndi-datasource/)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.annotations;
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.annotations;
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
|
@ -8,7 +8,7 @@ import javax.persistence.NamedStoredProcedureQuery;
|
|||
import javax.persistence.ParameterMode;
|
||||
import javax.persistence.StoredProcedureParameter;
|
||||
|
||||
import org.baeldung.persistence.multiple.model.user.User;
|
||||
import com.baeldung.persistence.multiple.model.user.User;
|
||||
import org.springframework.data.annotation.CreatedBy;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.Id;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.annotations;
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import javax.persistence.LockModeType;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.config;
|
||||
package com.baeldung.config;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
|
@ -25,8 +25,8 @@ import com.google.common.base.Preconditions;
|
|||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@PropertySource({ "classpath:persistence-h2.properties" })
|
||||
@ComponentScan({ "org.baeldung.persistence" })
|
||||
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao")
|
||||
@ComponentScan({ "com.baeldung.persistence" })
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.persistence.dao")
|
||||
public class PersistenceJPAConfig {
|
||||
|
||||
@Autowired
|
||||
|
@ -42,7 +42,7 @@ public class PersistenceJPAConfig {
|
|||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource());
|
||||
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
||||
em.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
|
||||
|
||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.config;
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
|
@ -6,7 +6,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|||
|
||||
// @Configuration
|
||||
@EnableTransactionManagement
|
||||
@ComponentScan({ "org.baeldung.persistence" })
|
||||
@ComponentScan({ "com.baeldung.persistence" })
|
||||
@ImportResource({ "classpath:jpaConfig.xml" })
|
||||
public class PersistenceJPAConfigXml {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.config;
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
@ -10,7 +10,7 @@ import org.springframework.web.servlet.view.JstlView;
|
|||
|
||||
@EnableWebMvc
|
||||
@Configuration
|
||||
@ComponentScan({ "org.baeldung.web" })
|
||||
@ComponentScan({ "com.baeldung.web" })
|
||||
public class SpringWebConfig extends WebMvcConfigurerAdapter {
|
||||
|
||||
@Bean
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.config;
|
||||
package com.baeldung.config;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
|
@ -18,7 +18,7 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
|||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@Configuration
|
||||
@EnableJpaRepositories(basePackages = "org.baeldung.inmemory.persistence.dao")
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.inmemory.persistence.dao")
|
||||
@PropertySource("persistence-student.properties")
|
||||
@EnableTransactionManagement
|
||||
public class StudentJpaConfig {
|
||||
|
@ -41,7 +41,7 @@ public class StudentJpaConfig {
|
|||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource());
|
||||
em.setPackagesToScan(new String[] { "org.baeldung.inmemory.persistence.model" });
|
||||
em.setPackagesToScan(new String[] { "com.baeldung.inmemory.persistence.model" });
|
||||
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
|
||||
em.setJpaProperties(additionalProperties());
|
||||
return em;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.config;
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.dsrouting;
|
||||
package com.baeldung.dsrouting;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.dsrouting;
|
||||
package com.baeldung.dsrouting;
|
||||
|
||||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.dsrouting;
|
||||
package com.baeldung.dsrouting;
|
||||
|
||||
public enum ClientDatabase {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.dsrouting;
|
||||
package com.baeldung.dsrouting;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.dsrouting;
|
||||
package com.baeldung.dsrouting;
|
||||
|
||||
/**
|
||||
* Service layer code for datasource routing example. Here, the service methods are responsible
|
|
@ -1,6 +1,6 @@
|
|||
package org.baeldung.inmemory.persistence.dao;
|
||||
package com.baeldung.inmemory.persistence.dao;
|
||||
|
||||
import org.baeldung.inmemory.persistence.model.ManyStudent;
|
||||
import com.baeldung.inmemory.persistence.model.ManyStudent;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
|
@ -1,6 +1,6 @@
|
|||
package org.baeldung.inmemory.persistence.dao;
|
||||
package com.baeldung.inmemory.persistence.dao;
|
||||
|
||||
import org.baeldung.inmemory.persistence.model.ManyTag;
|
||||
import com.baeldung.inmemory.persistence.model.ManyTag;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface ManyTagRepository extends JpaRepository<ManyTag, Long> {
|
|
@ -1,6 +1,6 @@
|
|||
package org.baeldung.inmemory.persistence.dao;
|
||||
package com.baeldung.inmemory.persistence.dao;
|
||||
|
||||
import org.baeldung.inmemory.persistence.model.Student;
|
||||
import com.baeldung.inmemory.persistence.model.Student;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.inmemory.persistence.model;
|
||||
package com.baeldung.inmemory.persistence.model;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.inmemory.persistence.model;
|
||||
package com.baeldung.inmemory.persistence.model;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.inmemory.persistence.model;
|
||||
package com.baeldung.inmemory.persistence.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.HashSet;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.inmemory.persistence.model;
|
||||
package com.baeldung.inmemory.persistence.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.HashSet;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.inmemory.persistence.model;
|
||||
package com.baeldung.inmemory.persistence.model;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.inmemory.persistence.model;
|
||||
package com.baeldung.inmemory.persistence.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
|
@ -1,6 +1,6 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.model.Book;
|
||||
import com.baeldung.persistence.model.Book;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Book;
|
||||
import com.baeldung.persistence.model.Book;
|
||||
|
||||
public interface BookRepositoryCustom {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -10,7 +10,7 @@ import javax.persistence.criteria.CriteriaQuery;
|
|||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.baeldung.persistence.model.Book;
|
||||
import com.baeldung.persistence.model.Book;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
|
@ -1,12 +1,12 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import static org.baeldung.persistence.dao.BookSpecifications.hasAuthor;
|
||||
import static org.baeldung.persistence.dao.BookSpecifications.titleContains;
|
||||
import static com.baeldung.persistence.dao.BookSpecifications.hasAuthor;
|
||||
import static com.baeldung.persistence.dao.BookSpecifications.titleContains;
|
||||
import static org.springframework.data.jpa.domain.Specifications.where;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Book;
|
||||
import com.baeldung.persistence.model.Book;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
|
@ -1,6 +1,6 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.model.Book;
|
||||
import com.baeldung.persistence.model.Book;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
public class BookSpecifications {
|
|
@ -1,6 +1,6 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
|
@ -1,8 +1,8 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
|
||||
public interface IFooDao {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.model;
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.model;
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.model;
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import org.hibernate.annotations.CacheConcurrencyStrategy;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.multiple.model.user;
|
||||
package com.baeldung.persistence.multiple.model.user;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.multiple.model.user;
|
||||
package com.baeldung.persistence.multiple.model.user;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
|
@ -1,9 +1,9 @@
|
|||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.dao.IFooDao;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.persistence.dao.IFooDao;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.sqlfiles;
|
||||
package com.baeldung.sqlfiles;
|
||||
|
||||
import static javax.persistence.GenerationType.IDENTITY;
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package org.baeldung.web;
|
||||
package com.baeldung.web;
|
||||
|
||||
import org.baeldung.persistence.service.FooService;
|
||||
import com.baeldung.persistence.service.FooService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="packagesToScan" value="org.baeldung.persistence.model"/>
|
||||
<property name="packagesToScan" value="com.baeldung.persistence.model"/>
|
||||
<property name="jpaVendorAdapter">
|
||||
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
|
||||
<!-- <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> <property name="generateDdl" value="${jpa.generateDdl}" /> <property name="databasePlatform"
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
|
||||
<persistence-unit name="punit">
|
||||
<class>org.baeldung.persistence.model.Foo</class>
|
||||
<class>org.baeldung.persistence.model.Bar</class>
|
||||
<class>com.baeldung.persistence.model.Foo</class>
|
||||
<class>com.baeldung.persistence.model.Bar</class>
|
||||
<properties>
|
||||
<property name="javax.persistence.jdbc.user" value="root"/>
|
||||
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.baeldung;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfig;
|
||||
import com.baeldung.config.PersistenceJPAConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.dsrouting;
|
||||
package com.baeldung.dsrouting;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.dsrouting;
|
||||
package com.baeldung.dsrouting;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
|
@ -1,14 +1,14 @@
|
|||
package org.baeldung.persistence.repository;
|
||||
package com.baeldung.persistence.repository;
|
||||
|
||||
import org.baeldung.config.StudentJpaConfig;
|
||||
import org.baeldung.inmemory.persistence.dao.ManyStudentRepository;
|
||||
import org.baeldung.inmemory.persistence.dao.ManyTagRepository;
|
||||
import org.baeldung.inmemory.persistence.dao.StudentRepository;
|
||||
import org.baeldung.inmemory.persistence.model.KVTag;
|
||||
import org.baeldung.inmemory.persistence.model.ManyStudent;
|
||||
import org.baeldung.inmemory.persistence.model.ManyTag;
|
||||
import org.baeldung.inmemory.persistence.model.SkillTag;
|
||||
import org.baeldung.inmemory.persistence.model.Student;
|
||||
import com.baeldung.config.StudentJpaConfig;
|
||||
import com.baeldung.inmemory.persistence.dao.ManyStudentRepository;
|
||||
import com.baeldung.inmemory.persistence.dao.ManyTagRepository;
|
||||
import com.baeldung.inmemory.persistence.dao.StudentRepository;
|
||||
import com.baeldung.inmemory.persistence.model.ManyStudent;
|
||||
import com.baeldung.inmemory.persistence.model.ManyTag;
|
||||
import com.baeldung.inmemory.persistence.model.SkillTag;
|
||||
import com.baeldung.inmemory.persistence.model.Student;
|
||||
import com.baeldung.inmemory.persistence.model.KVTag;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
|
@ -1,8 +1,8 @@
|
|||
package org.baeldung.persistence.repository;
|
||||
package com.baeldung.persistence.repository;
|
||||
|
||||
import org.baeldung.config.StudentJpaConfig;
|
||||
import org.baeldung.inmemory.persistence.dao.StudentRepository;
|
||||
import org.baeldung.inmemory.persistence.model.Student;
|
||||
import com.baeldung.config.StudentJpaConfig;
|
||||
import com.baeldung.inmemory.persistence.dao.StudentRepository;
|
||||
import com.baeldung.inmemory.persistence.model.Student;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
|
@ -15,8 +15,8 @@ import javax.persistence.criteria.CriteriaBuilder;
|
|||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfig;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.config.PersistenceJPAConfig;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
|
@ -1,9 +1,9 @@
|
|||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfig;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.config.PersistenceJPAConfig;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -10,9 +10,9 @@ import javax.persistence.criteria.CriteriaBuilder;
|
|||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfig;
|
||||
import org.baeldung.persistence.model.Bar;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.config.PersistenceJPAConfig;
|
||||
import com.baeldung.persistence.model.Bar;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
@ -9,8 +9,8 @@ import javax.persistence.EntityManager;
|
|||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfig;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.config.PersistenceJPAConfig;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue