All Projects → kuros → random-jpa

kuros / random-jpa

Licence: GPL-3.0 license
Create random test data for JPA/Hibernate entities.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to random-jpa

test-data-loader
A Groovy DSL for creating test data via JPA
Stars: ✭ 12 (-47.83%)
Mutual labels:  jpa, entity, test-data, test-data-generator
Generatedata
A powerful, feature-rich, random test data generator.
Stars: ✭ 1,883 (+8086.96%)
Mutual labels:  test-data, data-generator, test-data-generator
Hunt Entity
An object-relational mapping (ORM) framework for D language (Similar to JPA / Doctrine), support PostgreSQL and MySQL.
Stars: ✭ 51 (+121.74%)
Mutual labels:  jpa, entity, hibernate
Hibernate Orm Tutorials
40+ source code Examples/Tutorials/Guides of Hibernate ORM Framework
Stars: ✭ 106 (+360.87%)
Mutual labels:  jpa, hibernate
Hibernate Performance
Samples for "Hibernate performance tuning" talk
Stars: ✭ 87 (+278.26%)
Mutual labels:  jpa, hibernate
Jplusone
Tool for automatic detection and asserting "N+1 SELECT problem" occurences in JPA based Spring Boot Java applications and finding origin of JPA issued SQL statements in general
Stars: ✭ 91 (+295.65%)
Mutual labels:  jpa, hibernate
Query Validator
Compile time validation for HQL and JPQL queries in Java code
Stars: ✭ 70 (+204.35%)
Mutual labels:  jpa, hibernate
MiniDao
An powerful enhanced toolkit of SpringJdbc for simplify development
Stars: ✭ 200 (+769.57%)
Mutual labels:  jpa, hibernate
Spring Kotlin Exposed
playground for spring-boot 2.*, kotlin , jetbrains-exposed, postgres, jsonb, flyway, docker
Stars: ✭ 106 (+360.87%)
Mutual labels:  jpa, hibernate
phoenix-hibernate-dialect
An Apache Phoenix Hibernate dialect
Stars: ✭ 20 (-13.04%)
Mutual labels:  jpa, hibernate
Minidao
轻量级JAVA持久层,类似Mybatis一样的用法,基于SpringJdbc实现更轻量
Stars: ✭ 177 (+669.57%)
Mutual labels:  jpa, hibernate
ranger
Ranger is contextual data generator used to make sensible data for integration tests or to play with it in the database
Stars: ✭ 59 (+156.52%)
Mutual labels:  test-data, data-generator
Torpedoquery
Type safe Hibernate query builder (HQL)
Stars: ✭ 77 (+234.78%)
Mutual labels:  jpa, hibernate
Sample Boot Hibernate
Spring Boot + JPA ( Hibernate ) + Java8 [ DDD Sample ]
Stars: ✭ 97 (+321.74%)
Mutual labels:  jpa, hibernate
Curso Sistemas Web Com Spring Javascript Bootstrap
Stars: ✭ 74 (+221.74%)
Mutual labels:  jpa, hibernate
Spring Boot 2 Oauth2 Authorization Jwt
Spring Boot 2 OAuth2 JWT Authorization server implementation with Database for Users and Clients (JPA, Hibernate, MySQL)
Stars: ✭ 115 (+400%)
Mutual labels:  jpa, hibernate
Hibernate Reactive
A reactive API for Hibernate ORM, supporting non-blocking database drivers and a reactive style of interaction with the database.
Stars: ✭ 167 (+626.09%)
Mutual labels:  jpa, hibernate
Jpa Hibernate Tutorials
Hibernate Tutorials with Spring Boot and Spring-Data-JPA
Stars: ✭ 186 (+708.7%)
Mutual labels:  jpa, hibernate
Spring Framework Petclinic
A Spring Framework application based on JSP, Spring MVC, Spring Data JPA, Hibernate and JDBC
Stars: ✭ 251 (+991.3%)
Mutual labels:  jpa, hibernate
Kotlin Spring Boot Jpa Rest Api Demo
Build a Restful API with Kotlin, Spring Boot, Mysql, Jpa and Hibernate
Stars: ✭ 67 (+191.3%)
Mutual labels:  jpa, hibernate

Random-JPA Build Status Coverage Status Maven Central

It has been always been a challenge to create a test data. This project aims at providing easier mechanism to create test data.

Maven Group Plugin java version Latest Version
com.github.kuros.random-jpa 1.5+ v0.6.6
com.github.kuros.random-jpa 1.8+ v1.0.4

Feature

  1. Uses table's foreign key relations to maintain creation order dynamically
  2. Creates in memory creation plan which can be used to modify column values before persist.
  3. Provides facility to add custom dependency.
  4. Provides facility to add random generator (both at class level and attribute level).
  5. You can print the creation plan and persisted object hierarchy and easily access the respective objects by index.

Supported Database

  1. Microsoft SQL Server
  2. MySQL
  3. Oracle
  4. Postgres
  5. NONE (You can still use functionality, but the dependency has to provided/maintained manually).

Supported JPA Implementation

  1. Hibernate (Version - 4.x, 5.x)
  2. EclipseLink

Usage

In order to use.

Initialize JPAContextFactory

JPAContextFactory accepts two parameters Database and EntityManager. I am using MS_SQL_SERVER for demo.

JPAContext jpaContext = JPAContextFactory.newInstance(Database.MS_SQL_SERVER, entityManager)
    .generate();

Initialization with Dependencies

Let us say that you want have a custom dependency between Person and Employee tables but you do not have any foreign key relationship between them. We will have create Dependencies for this using javax.persistence.metamodel.Attribute objects. (Here Person_ & Employee_ are SingularAttributes)

final Dependencies dependencies = Dependencies.newInstance();
dependencies.withLink(Link.newLink(Person_.id, Employee_.personId))

JPAContext jpaContext = JPAContextFactory.newInstance(Database.MS_SQL_SERVER, entityManager)
    .with(dependencies)
    .generate();

Creating RandomGenerators

There are two ways in which you can control the random generation behavior.

  • At Class level
  • At Attribute level.

Let us say that you want all the dates to be current date. And all the Long/Integer values to be positive between 0-1000

final Generator generator = Generator.newInstance();
generator.addClassGenerator(new RandomClassGenerator() {
            @Override
            public Collection<Class<?>> getTypes() {
                final List<Class<?>> classes = Lists.newArrayList();
                classes.add(Date.class);
                return classes;
            }

            @Override
            public Object doGenerate(final Class<?> aClass) {
                return new Date();
            }
        });

generator.addClassGenerator(new RandomClassGenerator() {
            @Override
            public Collection<Class<?>> getTypes() {
                final List<Class<?>> classes = Lists.newArrayList();
                classes.add(Long.class);
                classes.add(Integer.class);
                return classes;
            }

            @Override
            public Object doGenerate(final Class<?> aClass) {
                return org.apache.commons.lang.RandomUtils.nextInt(1000);
            }
        });

You can also override random generation for specific attributes, to do that use RandomAttributeGenerator. Let us say that you want all the employee name to start with "Test-" followed by random string.

generator.addAttributeGenerator(new RandomAttributeGenerator() {
            @Override
            public List<? extends Attribute> getAttributes() {
                final List<SingularAttribute> singularAttributes = new ArrayList<SingularAttribute>();

                singularAttributes.add(Employee_.state);
                return singularAttributes;
            }

            @Override
            public Object doGenerate() {
                return "Test-" + RandomStringUtils.randomAlphanumeric(2);
            }
        });

Initializing RandomGenerators

final JPAContext jpaContext = JPAContextFactory.newInstance(Database.MS_SQL_SERVER, entityManager)
                .with(generator)
                .generate();

Adding Preconditions

There are scenario's where we want to create some schema in advance before creating our table, and this schema doesn't fall under the hierarchy of main table. To handle such scenarios, we can define PreConditions

final JPAContext jpaContext = JPAContextFactory.newInstance(Database.MS_SQL_SERVER, entityManager)
                .withPreconditions(Before.of(Employee.class)
                                            .create(Entity.of(Department.class), Entity.of(XXX.class)),
                                   Before.of(Person.class)
                                            .create(Entity.of(Y.class), Entity.of(Z.class)))
                .generate();

Using JPAContext

Creating Plan

Once JPAContext is initialized, you can use it to create plans and persist them accordingly. Let us say that we want to create two different Employees referring to single Person Or

CreationPlan creationPlan = jpaContext.create(
            Entity.of(Employee.class, 2).with(Employee_.Country, INDIA),
            Entity.of(Person.class).with(Person_.gender, "Male"));

Modify the creationPlan

Let us say that I want to persist these two employees with different name.

creationPlan.set(Employee_.name, "Employee 1");

creationPlan.set(1, Employee_.name, "Employee 2");

Printing the creationPlan

You need to provide a printer, which will print the string.

creationPlan.print(new Printer() {
            @Override
            public void print(final String string) {
                System.out.println(string); // you can use logger
            }
        });

it will print the hierarchy with the index number of the object followed by

└── *ROOT*
    └── com.github.kuros.entity.Person|0
        ├── com.github.kuros.entity.Employee|0
        └── com.github.kuros.entity.Employee|1

Persisting the creationPlan

final ResultMap resultMap = jpaContext.persist(creationPlan);

You can also create and persist plans

final ResultMap resultMap = jpaContext.createAndPersist(
            Entity.of(Employee.class, 2).with(Employee_.Country, INDIA),
            Entity.of(Person.class).with(Person_.gender, "Male"));

Fetching the persisted objects

Employee emp1 = resultMap.get(Employee.class);
System.out.println(emp1.getName()); // Prints - Employee 1

Employee emp2 = resultMap.get(Employee.class, 1);
System.out.println(emp2.getName()); //Prints - Employee 2

Printing the Persisted objects

You need to provide a printer, which will print the string.

resultMap.print(new Printer() {
            @Override
            public void print(final String string) {
                System.out.println(string); // you can use logger
            }
        });

it will print the hierarchy with the index number of the object followed by the primary key of the persisted objects

└── *ROOT*
    └── com.github.kuros.entity.Person|0 [personId: 1]
        ├── com.github.kuros.entity.Employee|0 [employeeId: 1]
        └── com.github.kuros.entity.Employee|1 [employeeId: 2]

** Id's might not get printed for all the loaded entities, since some entities are lazily initialized.

For More details please follow Random-JPA Blogls -l

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].