diff --git a/.gitignore b/.gitignore index 3370dfc0..5de618cb 100644 --- a/.gitignore +++ b/.gitignore @@ -67,6 +67,7 @@ spring-data-eclipse-store-demo/storage spring-data-eclipse-store-demo/storage-person spring-data-eclipse-store-demo/storage-invoice spring-data-eclipse-store-demo/storage-complex +spring-data-eclipse-store-demo/storage-lazy spring-data-eclipse-store-jpa/storage-eclipsestore spring-data-eclipse-store-jpa/storage-h2.mv.db spring-data-eclipse-store-jpa/storage-h2.trace.db diff --git a/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/Customer.java b/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/Customer.java new file mode 100644 index 00000000..191e38c8 --- /dev/null +++ b/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/Customer.java @@ -0,0 +1,31 @@ +package software.xdev.spring.data.eclipse.store.demo.lazy; + +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; + +import org.springframework.data.annotation.Id; + + +public class Customer +{ + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private String id; + + private final String firstName; + private final String lastName; + + public Customer(final String firstName, final String lastName) + { + this.firstName = firstName; + this.lastName = lastName; + } + + @Override + public String toString() + { + return String.format( + "Customer[id=%s, firstName='%s', lastName='%s']", + this.id, this.firstName, this.lastName); + } +} diff --git a/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/CustomerRepository.java b/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/CustomerRepository.java new file mode 100644 index 00000000..7c0edc1d --- /dev/null +++ b/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/CustomerRepository.java @@ -0,0 +1,8 @@ +package software.xdev.spring.data.eclipse.store.demo.lazy; + +import software.xdev.spring.data.eclipse.store.repository.interfaces.lazy.LazyEclipseStoreCrudRepository; + + +public interface CustomerRepository extends LazyEclipseStoreCrudRepository +{ +} diff --git a/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/LazyConfiguration.java b/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/LazyConfiguration.java new file mode 100644 index 00000000..7c73884b --- /dev/null +++ b/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/LazyConfiguration.java @@ -0,0 +1,56 @@ +package software.xdev.spring.data.eclipse.store.demo.lazy; + +import java.nio.file.Path; + +import org.eclipse.serializer.reflect.ClassLoaderProvider; +import org.eclipse.store.integrations.spring.boot.types.configuration.EclipseStoreProperties; +import org.eclipse.store.integrations.spring.boot.types.factories.EmbeddedStorageFoundationFactory; +import org.eclipse.store.storage.embedded.types.EmbeddedStorage; +import org.eclipse.store.storage.embedded.types.EmbeddedStorageFoundation; +import org.eclipse.store.storage.types.Storage; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.PlatformTransactionManager; + +import software.xdev.spring.data.eclipse.store.demo.dual.storage.person.PersistencePersonConfiguration; +import software.xdev.spring.data.eclipse.store.repository.config.EclipseStoreClientConfiguration; +import software.xdev.spring.data.eclipse.store.repository.config.EnableEclipseStoreRepositories; + + +@Configuration +@EnableEclipseStoreRepositories +public class LazyConfiguration extends EclipseStoreClientConfiguration +{ + + public static final String STORAGE_PATH = "storage-lazy"; + + @Autowired + public LazyConfiguration( + final EclipseStoreProperties defaultEclipseStoreProperties, + final EmbeddedStorageFoundationFactory defaultEclipseStoreProvider, + final ClassLoaderProvider classLoaderProvider + ) + { + super(defaultEclipseStoreProperties, defaultEclipseStoreProvider, classLoaderProvider); + } + /** + * This is one option how to configure the {@link EmbeddedStorageFoundation}. + *

+ * We create a completely new foundation. That means that all configuration (e.g. properties) are not used here. + * With this method you have complete control over the configuration. + *

+ * Another example: {@link PersistencePersonConfiguration#createEmbeddedStorageFoundation()} + */ + @Override + public EmbeddedStorageFoundation createEmbeddedStorageFoundation() + { + final EmbeddedStorageFoundation storageFoundation = + EmbeddedStorage.Foundation(Storage.Configuration(Storage.FileProvider(Path.of(STORAGE_PATH)))); + // This is only needed, if a different ClassLoader is used (e.g. when using spring-dev-tools) + storageFoundation.getConnectionFoundation().setClassLoaderProvider(getClassLoaderProvider()); + return storageFoundation; + } +} diff --git a/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/LazyDemoApplication.java b/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/LazyDemoApplication.java new file mode 100644 index 00000000..abc22e42 --- /dev/null +++ b/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/LazyDemoApplication.java @@ -0,0 +1,69 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package software.xdev.spring.data.eclipse.store.demo.lazy; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import software.xdev.spring.data.eclipse.store.repository.config.EnableEclipseStoreRepositories; + + +@SpringBootApplication +public class LazyDemoApplication implements CommandLineRunner +{ + private static final Logger LOG = LoggerFactory.getLogger(LazyDemoApplication.class); + private final CustomerRepository customerRepository; + private final PetRepository petRepository; + + public LazyDemoApplication( + final CustomerRepository customerRepository, + final PetRepository petRepository + ) + { + this.customerRepository = customerRepository; + this.petRepository = petRepository; + } + + public static void main(final String[] args) + { + SpringApplication.run(LazyDemoApplication.class, args); + } + + @Override + public void run(final String... args) + { + this.customerRepository.deleteAll(); + + // save a couple of customers + this.customerRepository.save(new Customer("Stevie", "Nicks")); + this.customerRepository.save(new Customer("Mick", "Fleetwood")); + + // fetch all customers + LOG.info("Customers found with findAll():"); + this.customerRepository.findAll().forEach(c -> LOG.info(c.toString())); + + // save a pet + this.petRepository.save(new Pet("1", "Peter", 2)); + + // fetch all pets + LOG.info("Pets found with findAll():"); + this.petRepository.findAll().forEach(p -> LOG.info(p.toString())); + } +} diff --git a/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/Pet.java b/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/Pet.java new file mode 100644 index 00000000..6a36915b --- /dev/null +++ b/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/Pet.java @@ -0,0 +1,32 @@ +package software.xdev.spring.data.eclipse.store.demo.lazy; + +import org.springframework.data.annotation.Id; + + +public class Pet +{ + @Id + private String id; + + private String name; + private Integer age; + + public Pet() + { + } + + public Pet(final String id, final String name, final Integer age) + { + this.id = id; + this.name = name; + this.age = age; + } + + @Override + public String toString() + { + return String.format( + "Pet[id=%s, name='%s', age='%s']", + this.id, this.name, this.age); + } +} diff --git a/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/PetRepository.java b/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/PetRepository.java new file mode 100644 index 00000000..2cba129d --- /dev/null +++ b/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/lazy/PetRepository.java @@ -0,0 +1,8 @@ +package software.xdev.spring.data.eclipse.store.demo.lazy; + +import software.xdev.spring.data.eclipse.store.repository.interfaces.lazy.LazyEclipseStoreCrudRepository; + + +public interface PetRepository extends LazyEclipseStoreCrudRepository +{ +} diff --git a/spring-data-eclipse-store-demo/src/test/java/software/xdev/spring/data/eclipse/store/demo/lazy/complex/LazyDemoApplicationTest.java b/spring-data-eclipse-store-demo/src/test/java/software/xdev/spring/data/eclipse/store/demo/lazy/complex/LazyDemoApplicationTest.java new file mode 100644 index 00000000..f6a7cd6f --- /dev/null +++ b/spring-data-eclipse-store-demo/src/test/java/software/xdev/spring/data/eclipse/store/demo/lazy/complex/LazyDemoApplicationTest.java @@ -0,0 +1,39 @@ +package software.xdev.spring.data.eclipse.store.demo.lazy.complex; + +import java.io.File; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import software.xdev.spring.data.eclipse.store.demo.TestUtil; +import software.xdev.spring.data.eclipse.store.demo.lazy.LazyConfiguration; +import software.xdev.spring.data.eclipse.store.demo.lazy.LazyDemoApplication; +import software.xdev.spring.data.eclipse.store.repository.config.EclipseStoreClientConfiguration; + + +@SpringBootTest(classes = LazyDemoApplication.class) +class LazyDemoApplicationTest +{ + private final EclipseStoreClientConfiguration configuration; + + @Autowired + public LazyDemoApplicationTest(final LazyConfiguration configuration) + { + this.configuration = configuration; + } + + @BeforeAll + static void clearPreviousData() + { + TestUtil.deleteDirectory(new File("./" + LazyConfiguration.STORAGE_PATH)); + } + + @Test + void checkPossibilityToSimplyStartAndRestartApplication() + { + this.configuration.getStorageInstance().stop(); + LazyDemoApplication.main(new String[]{}); + } +}