[BAEL-2048] Rearranging code
This commit is contained in:
@@ -21,8 +21,8 @@
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
<source>10</source>
|
||||
<target>10</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
package com.baeldung;
|
||||
|
||||
import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class)
|
||||
public class Application {
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
public static void main(String[] args) {
|
||||
applicationContext = SpringApplication.run(Application.class, args);
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+40
-21
@@ -1,16 +1,20 @@
|
||||
package org.baeldung.spring;
|
||||
package com.baeldung.config;
|
||||
|
||||
import com.baeldung.services.IBarService;
|
||||
import com.baeldung.services.impl.BarSpringDataJpaService;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl;
|
||||
import com.baeldung.services.IFooService;
|
||||
import com.baeldung.services.impl.FooService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.context.annotation.*;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.JpaVendorAdapter;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
@@ -20,32 +24,31 @@ import javax.sql.DataSource;
|
||||
import java.util.Properties;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan({"com.baeldung.dao", "com.baeldung.services"})
|
||||
@EnableTransactionManagement
|
||||
@PropertySource({ "classpath:persistence-${envTarget:h2}.properties" })
|
||||
@ComponentScan({ "org.baeldung.persistence" })
|
||||
// @ImportResource("classpath*:springDataPersistenceConfig.xml")
|
||||
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao")
|
||||
public class PersistenceConfig {
|
||||
@EnableJpaRepositories(basePackages = {"com.baeldung.dao"}, repositoryBaseClass = ExtendedRepositoryImpl.class)
|
||||
@EnableJpaAuditing
|
||||
@PropertySource("classpath:persistence.properties")
|
||||
public class PersistenceConfiguration {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
public PersistenceConfig() {
|
||||
public PersistenceConfiguration() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource());
|
||||
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
||||
final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
|
||||
emf.setDataSource(dataSource());
|
||||
emf.setPackagesToScan("com.baeldung.domain");
|
||||
|
||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
// vendorAdapter.set
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
||||
em.setJpaProperties(additionalProperties());
|
||||
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
emf.setJpaVendorAdapter(vendorAdapter);
|
||||
emf.setJpaProperties(hibernateProperties());
|
||||
|
||||
return em;
|
||||
return emf;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -63,7 +66,6 @@ public class PersistenceConfig {
|
||||
public PlatformTransactionManager transactionManager() {
|
||||
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
|
||||
|
||||
return transactionManager;
|
||||
}
|
||||
|
||||
@@ -72,11 +74,28 @@ public class PersistenceConfig {
|
||||
return new PersistenceExceptionTranslationPostProcessor();
|
||||
}
|
||||
|
||||
final Properties additionalProperties() {
|
||||
@Bean
|
||||
public IBarService barSpringDataJpaService() {
|
||||
return new BarSpringDataJpaService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IFooService fooService() {
|
||||
return new FooService();
|
||||
}
|
||||
|
||||
private final Properties hibernateProperties() {
|
||||
final Properties hibernateProperties = new Properties();
|
||||
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||
|
||||
hibernateProperties.setProperty("hibernate.show_sql", "true");
|
||||
// hibernateProperties.setProperty("hibernate.format_sql", "true");
|
||||
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
|
||||
|
||||
// Envers properties
|
||||
hibernateProperties.setProperty("org.hibernate.envers.audit_table_suffix", env.getProperty("envers.audit_table_suffix"));
|
||||
|
||||
return hibernateProperties;
|
||||
}
|
||||
|
||||
+9
-11
@@ -1,9 +1,6 @@
|
||||
package org.baeldung.config;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
package com.baeldung.config;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -16,16 +13,17 @@ import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
|
||||
@Configuration
|
||||
@PropertySource({ "classpath:persistence-multiple-db.properties" })
|
||||
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.multiple.dao.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
|
||||
public class ProductConfig {
|
||||
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.dao.repositories.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
|
||||
public class PersistenceProductConfiguration {
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
public ProductConfig() {
|
||||
public PersistenceProductConfiguration() {
|
||||
super();
|
||||
}
|
||||
|
||||
@@ -35,7 +33,7 @@ public class ProductConfig {
|
||||
public LocalContainerEntityManagerFactoryBean productEntityManager() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(productDataSource());
|
||||
em.setPackagesToScan(new String[] { "org.baeldung.persistence.multiple.model.product" });
|
||||
em.setPackagesToScan("com.baeldung.domain.product");
|
||||
|
||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
||||
+9
-11
@@ -1,9 +1,6 @@
|
||||
package org.baeldung.config;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
package com.baeldung.config;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -17,16 +14,17 @@ import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
|
||||
@Configuration
|
||||
@PropertySource({ "classpath:persistence-multiple-db.properties" })
|
||||
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.multiple.dao.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")
|
||||
public class UserConfig {
|
||||
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.dao.repositories.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")
|
||||
public class PersistenceUserConfiguration {
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
public UserConfig() {
|
||||
public PersistenceUserConfiguration() {
|
||||
super();
|
||||
}
|
||||
|
||||
@@ -37,7 +35,7 @@ public class UserConfig {
|
||||
public LocalContainerEntityManagerFactoryBean userEntityManager() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(userDataSource());
|
||||
em.setPackagesToScan(new String[] { "org.baeldung.persistence.multiple.model.user" });
|
||||
em.setPackagesToScan("com.baeldung.domain.user");
|
||||
|
||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package org.baeldung.persistence.dao;
|
||||
package com.baeldung.dao;
|
||||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.domain.Foo;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.repository;
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import com.baeldung.domain.Article;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
+15
-15
@@ -1,15 +1,15 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.Item;
|
||||
|
||||
@Repository
|
||||
public interface CustomItemRepository {
|
||||
|
||||
void deleteCustom(Item entity);
|
||||
|
||||
Item findItemById(Long id);
|
||||
|
||||
void findThenDelete(Long id);
|
||||
}
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.Item;
|
||||
|
||||
@Repository
|
||||
public interface CustomItemRepository {
|
||||
|
||||
void deleteCustom(Item entity);
|
||||
|
||||
Item findItemById(Long id);
|
||||
|
||||
void findThenDelete(Long id);
|
||||
}
|
||||
+13
-13
@@ -1,13 +1,13 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.ItemType;
|
||||
|
||||
@Repository
|
||||
public interface CustomItemTypeRepository {
|
||||
|
||||
void deleteCustom(ItemType entity);
|
||||
|
||||
void findThenDelete(Long id);
|
||||
}
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.ItemType;
|
||||
|
||||
@Repository
|
||||
public interface CustomItemTypeRepository {
|
||||
|
||||
void deleteCustom(ItemType entity);
|
||||
|
||||
void findThenDelete(Long id);
|
||||
}
|
||||
+4
-2
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.extended.persistence.dao;
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
@@ -8,5 +8,7 @@ import org.springframework.data.repository.NoRepositoryBean;
|
||||
|
||||
@NoRepositoryBean
|
||||
public interface ExtendedRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
|
||||
public List<T> findByAttributeContainsText(String attributeName, String text);
|
||||
|
||||
List<T> findByAttributeContainsText(String attributeName, String text);
|
||||
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import com.baeldung.domain.Student;
|
||||
|
||||
public interface ExtendedStudentRepository extends ExtendedRepository<Student, Long> {
|
||||
}
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import com.baeldung.persistence.model.Bar;
|
||||
import com.baeldung.domain.Bar;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import java.io.Serializable;
|
||||
+10
-10
@@ -1,10 +1,10 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.ItemType;
|
||||
|
||||
@Repository
|
||||
public interface ItemTypeRepository extends JpaRepository<ItemType, Long>, CustomItemTypeRepository, CustomItemRepository {
|
||||
}
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.ItemType;
|
||||
|
||||
@Repository
|
||||
public interface ItemTypeRepository extends JpaRepository<ItemType, Long>, CustomItemTypeRepository, CustomItemRepository {
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.repository;
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
+15
-15
@@ -1,15 +1,15 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
import com.baeldung.domain.Location;
|
||||
|
||||
@org.springframework.stereotype.Repository
|
||||
public interface ReadOnlyLocationRepository extends Repository<Location, Long> {
|
||||
|
||||
Optional<Location> findById(Long id);
|
||||
|
||||
Location save(Location location);
|
||||
}
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
import com.baeldung.domain.Location;
|
||||
|
||||
@org.springframework.stereotype.Repository
|
||||
public interface ReadOnlyLocationRepository extends Repository<Location, Long> {
|
||||
|
||||
Optional<Location> findById(Long id);
|
||||
|
||||
Location save(Location location);
|
||||
}
|
||||
+13
-13
@@ -1,13 +1,13 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.Store;
|
||||
|
||||
@Repository
|
||||
public interface StoreRepository extends JpaRepository<Store, Long> {
|
||||
List<Store> findStoreByLocationId(Long locationId);
|
||||
}
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.Store;
|
||||
|
||||
@Repository
|
||||
public interface StoreRepository extends JpaRepository<Store, Long> {
|
||||
List<Store> findStoreByLocationId(Long locationId);
|
||||
}
|
||||
+32
-32
@@ -1,32 +1,32 @@
|
||||
package com.baeldung.repository.impl;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.Item;
|
||||
import com.baeldung.repository.CustomItemRepository;
|
||||
|
||||
@Repository
|
||||
public class CustomItemRepositoryImpl implements CustomItemRepository {
|
||||
|
||||
@Autowired
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Override
|
||||
public void deleteCustom(Item item) {
|
||||
entityManager.remove(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item findItemById(Long id) {
|
||||
return entityManager.find(Item.class, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void findThenDelete(Long id) {
|
||||
final Item item = entityManager.find(Item.class, id);
|
||||
entityManager.remove(item);
|
||||
}
|
||||
}
|
||||
package com.baeldung.dao.repositories.impl;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.Item;
|
||||
import com.baeldung.dao.repositories.CustomItemRepository;
|
||||
|
||||
@Repository
|
||||
public class CustomItemRepositoryImpl implements CustomItemRepository {
|
||||
|
||||
@Autowired
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Override
|
||||
public void deleteCustom(Item item) {
|
||||
entityManager.remove(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item findItemById(Long id) {
|
||||
return entityManager.find(Item.class, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void findThenDelete(Long id) {
|
||||
final Item item = entityManager.find(Item.class, id);
|
||||
entityManager.remove(item);
|
||||
}
|
||||
}
|
||||
+31
-31
@@ -1,31 +1,31 @@
|
||||
package com.baeldung.repository.impl;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.ItemType;
|
||||
import com.baeldung.repository.CustomItemTypeRepository;
|
||||
|
||||
@Repository
|
||||
public class CustomItemTypeRepositoryImpl implements CustomItemTypeRepository {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CustomItemTypeRepositoryImpl.class);
|
||||
|
||||
@Autowired
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Override
|
||||
public void deleteCustom(ItemType itemType) {
|
||||
entityManager.remove(itemType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void findThenDelete(Long id) {
|
||||
ItemType itemTypeToDelete = entityManager.find(ItemType.class, id);
|
||||
entityManager.remove(itemTypeToDelete);
|
||||
}
|
||||
}
|
||||
package com.baeldung.dao.repositories.impl;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.ItemType;
|
||||
import com.baeldung.dao.repositories.CustomItemTypeRepository;
|
||||
|
||||
@Repository
|
||||
public class CustomItemTypeRepositoryImpl implements CustomItemTypeRepository {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CustomItemTypeRepositoryImpl.class);
|
||||
|
||||
@Autowired
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Override
|
||||
public void deleteCustom(ItemType itemType) {
|
||||
entityManager.remove(itemType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void findThenDelete(Long id) {
|
||||
ItemType itemTypeToDelete = entityManager.find(ItemType.class, id);
|
||||
entityManager.remove(itemTypeToDelete);
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.extended.persistence.dao;
|
||||
package com.baeldung.dao.repositories.impl;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
@@ -10,6 +10,7 @@ import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
import com.baeldung.dao.repositories.ExtendedRepository;
|
||||
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
|
||||
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package org.baeldung.persistence.multiple.dao.product;
|
||||
package com.baeldung.dao.repositories.product;
|
||||
|
||||
import org.baeldung.persistence.multiple.model.product.Product;
|
||||
import com.baeldung.domain.product.Product;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface ProductRepository extends JpaRepository<Product, Integer> {
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package org.baeldung.persistence.multiple.dao.user;
|
||||
package com.baeldung.dao.repositories.user;
|
||||
|
||||
import org.baeldung.persistence.multiple.model.user.Possession;
|
||||
import com.baeldung.domain.user.Possession;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface PossessionRepository extends JpaRepository<Possession, Long> {
|
||||
+11
-11
@@ -1,6 +1,6 @@
|
||||
package org.baeldung.persistence.repository;
|
||||
package com.baeldung.dao.repositories.user;
|
||||
|
||||
import org.baeldung.persistence.model.User;
|
||||
import com.baeldung.domain.user.User;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
@@ -8,24 +8,25 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Repository("userRepository")
|
||||
public interface UserRepository extends JpaRepository<User, Integer> {
|
||||
|
||||
Stream<User> findAllByName(String name);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = 1")
|
||||
Collection<User> findAllActiveUsers();
|
||||
|
||||
@Query(value = "SELECT * FROM USERS u WHERE u.status = 1", nativeQuery = true)
|
||||
@Query(value = "SELECT * FROM USERS.USERS u WHERE u.status = 1", nativeQuery = true)
|
||||
Collection<User> findAllActiveUsersNative();
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = ?1")
|
||||
User findUserByStatus(Integer status);
|
||||
|
||||
@Query(value = "SELECT * FROM Users u WHERE u.status = ?1", nativeQuery = true)
|
||||
@Query(value = "SELECT * FROM USERS.Users u WHERE u.status = ?1", nativeQuery = true)
|
||||
User findUserByStatusNative(Integer status);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = ?1 and u.name = ?2")
|
||||
@@ -34,7 +35,7 @@ public interface UserRepository extends JpaRepository<User, Integer> {
|
||||
@Query("SELECT u FROM User u WHERE u.status = :status and u.name = :name")
|
||||
User findUserByStatusAndNameNamedParams(@Param("status") Integer status, @Param("name") String name);
|
||||
|
||||
@Query(value = "SELECT * FROM Users u WHERE u.status = :status AND u.name = :name", nativeQuery = true)
|
||||
@Query(value = "SELECT * FROM USERS.Users u WHERE u.status = :status AND u.name = :name", nativeQuery = true)
|
||||
User findUserByStatusAndNameNamedParamsNative(@Param("status") Integer status, @Param("name") String name);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = :status and u.name = :name")
|
||||
@@ -46,7 +47,7 @@ public interface UserRepository extends JpaRepository<User, Integer> {
|
||||
@Query("SELECT u FROM User u WHERE u.name like :name%")
|
||||
User findUserByNameLikeNamedParam(@Param("name") String name);
|
||||
|
||||
@Query(value = "SELECT * FROM users u WHERE u.name LIKE ?1%", nativeQuery = true)
|
||||
@Query(value = "SELECT * FROM USERS.users u WHERE u.name LIKE ?1%", nativeQuery = true)
|
||||
User findUserByNameLikeNative(String name);
|
||||
|
||||
@Query(value = "SELECT u FROM User u")
|
||||
@@ -55,7 +56,7 @@ public interface UserRepository extends JpaRepository<User, Integer> {
|
||||
@Query(value = "SELECT u FROM User u ORDER BY id")
|
||||
Page<User> findAllUsersWithPagination(Pageable pageable);
|
||||
|
||||
@Query(value = "SELECT * FROM Users ORDER BY id", countQuery = "SELECT count(*) FROM Users", nativeQuery = true)
|
||||
@Query(value = "SELECT * FROM USERS.Users ORDER BY id", countQuery = "SELECT count(*) FROM USERS.Users", nativeQuery = true)
|
||||
Page<User> findAllUsersWithPaginationNative(Pageable pageable);
|
||||
|
||||
@Modifying
|
||||
@@ -63,7 +64,6 @@ public interface UserRepository extends JpaRepository<User, Integer> {
|
||||
int updateUserSetStatusForName(@Param("status") Integer status, @Param("name") String name);
|
||||
|
||||
@Modifying
|
||||
@Query(value = "UPDATE Users u SET u.status = ? WHERE u.name = ?", nativeQuery = true)
|
||||
@Query(value = "UPDATE USERS.Users u SET u.status = ? WHERE u.name = ?", nativeQuery = true)
|
||||
int updateUserSetStatusForNameNative(Integer status, String name);
|
||||
|
||||
}
|
||||
+31
-40
@@ -1,7 +1,6 @@
|
||||
package com.baeldung.persistence.model;
|
||||
package com.baeldung.domain;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.hibernate.annotations.OrderBy;
|
||||
import org.hibernate.envers.Audited;
|
||||
import org.jboss.logging.Logger;
|
||||
@@ -23,31 +22,6 @@ import java.util.Set;
|
||||
public class Bar implements Serializable {
|
||||
|
||||
private static Logger logger = Logger.getLogger(Bar.class);
|
||||
|
||||
public enum OPERATION {
|
||||
INSERT, UPDATE, DELETE;
|
||||
private String value;
|
||||
|
||||
OPERATION() {
|
||||
value = toString();
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static OPERATION parse(final String value) {
|
||||
OPERATION operation = null;
|
||||
for (final OPERATION op : OPERATION.values()) {
|
||||
if (op.getValue().equals(value)) {
|
||||
operation = op;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return operation;
|
||||
}
|
||||
};
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "id")
|
||||
@@ -55,30 +29,23 @@ public class Bar implements Serializable {
|
||||
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
@OrderBy(clause = "NAME DESC")
|
||||
// @NotAudited
|
||||
private Set<Foo> fooSet = Sets.newHashSet();
|
||||
|
||||
@Column(name = "operation")
|
||||
private String operation;
|
||||
|
||||
@Column(name = "timestamp")
|
||||
private long timestamp;
|
||||
|
||||
@Column(name = "created_date", updatable = false, nullable = false)
|
||||
@CreatedDate
|
||||
private long createdDate;
|
||||
|
||||
@Column(name = "modified_date")
|
||||
@LastModifiedDate
|
||||
private long modifiedDate;
|
||||
|
||||
@Column(name = "created_by")
|
||||
@CreatedBy
|
||||
private String createdBy;
|
||||
|
||||
@Column(name = "modified_by")
|
||||
@LastModifiedBy
|
||||
private String modifiedBy;
|
||||
@@ -93,12 +60,12 @@ public class Bar implements Serializable {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public Set<Foo> getFooSet() {
|
||||
return fooSet;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public void setFooSet(final Set<Foo> fooSet) {
|
||||
this.fooSet = fooSet;
|
||||
}
|
||||
@@ -123,6 +90,10 @@ public class Bar implements Serializable {
|
||||
return OPERATION.parse(operation);
|
||||
}
|
||||
|
||||
public void setOperation(final String operation) {
|
||||
this.operation = operation;
|
||||
}
|
||||
|
||||
public void setOperation(final OPERATION operation) {
|
||||
this.operation = operation.getValue();
|
||||
}
|
||||
@@ -167,10 +138,6 @@ public class Bar implements Serializable {
|
||||
this.modifiedBy = modifiedBy;
|
||||
}
|
||||
|
||||
public void setOperation(final String operation) {
|
||||
this.operation = operation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
@@ -226,4 +193,28 @@ public class Bar implements Serializable {
|
||||
setTimestamp((new Date()).getTime());
|
||||
}
|
||||
|
||||
public enum OPERATION {
|
||||
INSERT, UPDATE, DELETE;
|
||||
private String value;
|
||||
|
||||
OPERATION() {
|
||||
value = toString();
|
||||
}
|
||||
|
||||
public static OPERATION parse(final String value) {
|
||||
OPERATION operation = null;
|
||||
for (final OPERATION op : OPERATION.values()) {
|
||||
if (op.getValue().equals(value)) {
|
||||
operation = op;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return operation;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+3
-3
@@ -1,11 +1,11 @@
|
||||
package com.baeldung.persistence.model;
|
||||
package com.baeldung.domain;
|
||||
|
||||
import org.hibernate.envers.Audited;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
|
||||
@NamedNativeQueries({ @NamedNativeQuery(name = "callGetAllFoos", query = "CALL GetAllFoos()", resultClass = Foo.class), @NamedNativeQuery(name = "callGetFoosByName", query = "CALL GetFoosByName(:fooName)", resultClass = Foo.class) })
|
||||
@NamedNativeQueries({@NamedNativeQuery(name = "callGetAllFoos", query = "CALL GetAllFoos()", resultClass = Foo.class), @NamedNativeQuery(name = "callGetFoosByName", query = "CALL GetFoosByName(:fooName)", resultClass = Foo.class)})
|
||||
@Entity
|
||||
@Audited
|
||||
// @Proxy(lazy = false)
|
||||
@@ -16,7 +16,7 @@ public class Foo implements Serializable {
|
||||
@Column(name = "id")
|
||||
private long id;
|
||||
|
||||
@Column(name = "name")
|
||||
@Column(name = "name", nullable = false)
|
||||
private String name;
|
||||
|
||||
@ManyToOne(targetEntity = Bar.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.inmemory.persistence.model;
|
||||
package com.baeldung.domain;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.inmemory.persistence.model;
|
||||
package com.baeldung.domain;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.inmemory.persistence.model;
|
||||
package com.baeldung.domain;
|
||||
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Entity;
|
||||
+2
-2
@@ -1,11 +1,11 @@
|
||||
package org.baeldung.persistence.multiple.model.product;
|
||||
package com.baeldung.domain.product;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(schema = "spring_jpa_product")
|
||||
@Table(schema = "products")
|
||||
public class Product {
|
||||
|
||||
@Id
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.multiple.model.user;
|
||||
package com.baeldung.domain.user;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
@@ -7,7 +7,7 @@ import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(schema = "spring_jpa_user")
|
||||
@Table(schema = "users")
|
||||
public class Possession {
|
||||
|
||||
@Id
|
||||
+20
-15
@@ -1,30 +1,20 @@
|
||||
package org.baeldung.persistence.multiple.model.user;
|
||||
package com.baeldung.domain.user;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(schema = "spring_jpa_user")
|
||||
@Table(name = "users", schema = "users")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
private int age;
|
||||
@Column(unique = true, nullable = false)
|
||||
private String email;
|
||||
|
||||
private int age;
|
||||
|
||||
private Integer status;
|
||||
@OneToMany
|
||||
List<Possession> possessionList;
|
||||
|
||||
@@ -32,6 +22,12 @@ public class User {
|
||||
super();
|
||||
}
|
||||
|
||||
public User(String name, String email, Integer status) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
@@ -56,6 +52,14 @@ public class User {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Integer status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
@@ -78,4 +82,5 @@ public class User {
|
||||
builder.append("User [name=").append(name).append(", id=").append(id).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user