Skip to content

cmeza20/spring-data-generator

Repository files navigation

Spring Data Generator Maven Central

Spring Data Generator for JPA repositories and managers.

Features

  • Generate in Runtime
  • Generate by Plugin
  • Generate repositories for JPA Entities
  • Generate managers for JPA Entities
  • EntityScan wrapped annotation
  • Overwrite option
  • Support JpaSpecificationExcecutor<?>
  • Support @EmbeddedId annotation
  • @Id or @EmbeddedId primitive support
  • Package structure support
  • Additional extends support
  • Lombok support
  • Jakarta support

Dependencies

  • Java 11
  • Spring data JPA

Generate in Runtime

Download the jar through Maven:

<dependency>
  <groupId>com.cmeza</groupId>
  <artifactId>spring-data-generator</artifactId>
  <version>2.0.1</version>
</dependency>

The simple Spring Data JPA configuration with Java-Config looks like this:

@SDGenerator(
        entityPackage = "com.acme.model",
        repositoryPackage = "com.acme.repositories",
        managerPackage = "com.acme.managers",
        repositoryPostfix = "Repository",
        managerPostfix = "Manager",
        onlyAnnotations = false,
        debug = false,
        overwrite = false,
        additionalExtends = {
                QuerydslPredicateExecutor.class
        },
        lombokAnnotations = false,
        withComments = true,
        withJpaSpecificationExecutor = true
)
@SpringBootApplication
public class AppConfig {
    
    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class, args);
    }
    
}
Attribute Required Default Description
entityPackage No [] Entity scan package
repositoryPackage No "" Package where the repositories will be generated
managerPackage No "" Package where the managers will be generated
repositoryPostfix No "Repository" Postfix for repositories. example: AccountRepository
managerPostfix No "Manager" Postfix for managers. example: AccountManager
onlyAnnotations No false Scan only classes annotated with @SDGenerate or @SDNoGenerate
debug No false Enable debug log
overwrite No false Overwrite existing files
additionalExtends No [] Extension of additional interfaces
lombokAnnotations No false Support Lombok annotations
withComments No true Class comments activation
withJpaSpecificationExecutor No true JpaSpecificationExecutor activation

Generate by Plugin

Download the jar through Maven:

<build>
	<plugins>
		<plugin>
			<groupId>com.cmeza</groupId>
			<artifactId>spring-data-generator</artifactId>
			<version>2.0.1</version>
			<configuration>
				<entity-package>
				    <param>com.acme.model</param>
				</entity-package>
				<repository-package>com.acme.repository</repository-package>
				<repository-postfix>Repository</repository-postfix>
				<manager-package>com.acme.managers</manager-package>
				<manager-postfix>Manager</manager-postfix>
				<only-annotations>false</only-annotations>
				<overwrite>false</overwrite>
				<additional-extends>
                    <param>org.springframework.data.querydsl.QuerydslPredicateExecutor</param>
                </additional-extends>
                <lombok-annotations>false</lombok-annotations>
                <with-comments>true</with-comments>
                <with-JpaSpecificationExecutor>true</with-JpaSpecificationExecutor>
			</configuration>
		</plugin>
	</plugins>
</build>
Attribute Required Default Description
entity-package Yes [] Entity scan package
repository-package Yes "" Package where the repositories will be generated
manager-package Yes "" Package where the managers will be generated
repository-postfix No "Repository" Postfix for repositories. example: AccountRepository
manager-postfix No "Manager" Postfix for managers. example: AccountManager
onlyAnnotations No false Scan only classes annotated with @SDGenerate or @SDNoGenerate
overwrite No false Overwrite existing files
additional-extends No [] Extension of additional interfaces
lombok-annotations No false Support Lombok annotations
with-comments No true Class comments activation
with-JpaSpecificationExecutor No true JpaSpecificationExecutor activation

Generate repositories (terminal)

$ mvn spring-data-generator:repositories

Generate managers (terminal)

$ mvn spring-data-generator:managers

Example

Sample entity in com.acme.model

//import javax.persistence.Entity; //javax persistence support
//import javax.persistence.Id; //javax persistence support
import jakarta.persistence.Entity; //jakarta persistence support
import jakarta.persistence.Id; //jakarta persistence support

@Entity
//@SDGenerate   ->  Optional: Include to classes scan
//@SDNoGenerate   ->  Optional: Exclude to classes scan
public class Account {

    @Id
    @GeneratedValue
    private Long id;
    private String firstname;
    private String lastname;
       
    // Getters and setters
    // (Firstname, Lastname)-constructor and noargs-constructor
    // equals / hashcode
}

Generate a repository interface example in com.acme.repositories:

// With JpaSpecificationExecutor interface
@Repository
public interface AccountRepository extends JpaRepository<Account, Long>, JpaSpecificationExecutor<Account> {
}
// Without JpaSpecificationExecutor interface
@Repository
public interface AccountRepository extends JpaRepository<Account, Long> {
}

Generate a manager class example in com.acme.managers:

Without Lombok

@Component
public class AccountManager {
    
    private final AccountRepository accountRepository;
    
    @Autowired
    public AccountManager(AccountRepository accountRepository) {
        this.accountRepository = accountRepository;
    }   
}

With Lombok

@Component
@RequiredArgsConstructor
public class AccountManager {

    private final AccountRepository accountRepository;
    
}

Notes

  • The overwrite option delete the existing file to regenerate
  • The generation of repositories and managers inherits the entity package. Example:

alt tag

License

MIT