Skip to content

Commit

Permalink
Add LazyRepo demo
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesRabauer committed Dec 9, 2024
1 parent 2c36144 commit 2eedf70
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<Customer, String>
{
}
Original file line number Diff line number Diff line change
@@ -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}.
* <p>
* 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.
* </p>
* 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;
}
}
Original file line number Diff line number Diff line change
@@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<Pet, String>
{
}
Original file line number Diff line number Diff line change
@@ -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[]{});
}
}

0 comments on commit 2eedf70

Please sign in to comment.