nociar/jpa-cloner


The JPA cloner project.

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.nociar:jpa-cloner:v1.0.3'
	}
	dependencies {
		implementation("com.github.nociar:jpa-cloner:v1.0.3")
	}
	<dependency>
	    <groupId>com.github.nociar</groupId>
	    <artifactId>jpa-cloner</artifactId>
	    <version>v1.0.3</version>
	</dependency>

                            
    libraryDependencies += "com.github.nociar" % "jpa-cloner" % "v1.0.3"
        
        

                            
    :dependencies [[com.github.nociar/jpa-cloner "v1.0.3"]]
        
        

Readme


JPA cloner

The project allows cloning of JPA entity subgraphs. Entity subgraphs are defined by string patterns. Cloned entities will have all basic properties copied by default. Advanced control over the cloning process is supported via the PropertyFilter interface.

<dependency>
    <groupId>com.github.nociar</groupId>
    <artifactId>jpa-cloner</artifactId>
    <version>1.0.3</version>
</dependency>

Example usage

Company company = entityManager.find(Company.class, companyId);
Company clone1 = JpaCloner.clone(company, "departments.*");
Company clone2 = JpaCloner.clone(company, "depa??ments");
Company clone3 = JpaCloner.clone(company, "departments+.(boss|employees).address");
// do not clone @Id and @Transient fields for the whole entity subgraph:
PropertyFilter filter = PropertyFilters.getAnnotationFilter(Id.class, Transient.class);
Company clone4 = JpaCloner.clone(company, filter, "*+");
// custom property filter
PropertyFilter myFilter = new PropertyFilter() {
    public boolean test(Object entity, String property) {
        return !"ignoredProperty".equals(property);
    }
};
Company clone5 = JpaCloner.clone(company, myFilter, "*+");

Operators

  • Dot "." separates paths: A.B.C
  • Plus "+" generates at least one preceding path: A.B+.C
  • Split "|" divides the path into two ways: A.(B|C).D
  • Terminator "$" ends the preceding path: A.(B$|C).D
  • Parentheses "(", ")" groups the paths.
  • Wildcards "*", "?" in property names: dumm?.pro*ties

Requirements

  • The JPA cloner is tested only against Hibernate.
  • Cloned entities must correctly implement equals() and hashCode().

Please refer to the JpaCloner class for more description.