Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ relations are only working with classed which are **annotated with @Entity**!

```java

@Relation(sourceClass = Person.class)
@Relation
@Entity
@Getter
@Setter
Expand Down Expand Up @@ -108,7 +108,7 @@ This is useful for:

```java
@IgnoreRelation
@Relation(sourceClass = DisabledRelationEntity.class)
@Relation
@Entity
public class DisabledRelationEntity {

Expand All @@ -124,7 +124,7 @@ Implement the relationIdentity, each dynamic relation need a Long id and a Strin

```java

@Relation(sourceClass = Person.class)
@Relation
@Entity
@Getter
@Setter
Expand All @@ -142,14 +142,15 @@ public class Person implements RelationIdentity {

```

## <a name="ImportConfig"></a> Import Config Module for Component Scan
## <a name="ImportConfig"></a> Import Configuration Module for Component Scanning and Relation Support

Import the DrmConfig in your Spring Boot Application, so that you can use the RelationService
Enable the `DrmConfig` configuration in your Spring Boot application by using the custom `@EnableDynamicRelation` annotation.
This activates component scanning for the `at.drm.*` packages and makes the `RelationService` and related beans available for use.

```java

@EnableDynamicRelation
@SpringBootApplication
@Import(DrmConfig.class)
public class App {

public static void main(String[] args) {
Expand Down
2 changes: 1 addition & 1 deletion dynamic-relations/src/main/java/at/drm/DrmConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

@Configuration
@ComponentScan(basePackageClasses = DrmConfig.class)
public class DrmConfig {
class DrmConfig {
}
14 changes: 14 additions & 0 deletions dynamic-relations/src/main/java/at/drm/EnableDynamicRelation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package at.drm;

import org.springframework.context.annotation.Import;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(DrmConfig.class)
public @interface EnableDynamicRelation {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface Relation {
Class<?> sourceClass();
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private RelationMetaData createEntityMetaData(Element relationElement) {
Relation relationAnnotation = relationElement.getAnnotation(Relation.class);
String elementPackage = processingEnv.getElementUtils()
.getPackageOf(relationElement).getQualifiedName().toString();
TypeName sourceObjectName = getSourceObjectTypeName(relationAnnotation);
TypeName sourceObjectName = ClassName.get(relationElement.asType());
String sourceObjectWithoutPackages = sourceObjectName.toString().replace(elementPackage + ".", "");
String generatedEntityName = sourceObjectWithoutPackages + "Relation";
return new RelationMetaData(sourceObjectName, elementPackage, generatedEntityName, relationAnnotation);
Expand Down Expand Up @@ -203,12 +203,6 @@ private static FieldSpec createTargetIdField() {
.build();
}

private TypeName getSourceObjectTypeName(Relation annotation) {
TypeMirror typeMirror = getSourceClass(annotation);
assert typeMirror != null;
return ClassName.get(typeMirror);
}

private FieldSpec createSourceObjectField(TypeName typeName) {
return FieldSpec.builder(typeName, "sourceObject", Modifier.PRIVATE)
.addAnnotation(ManyToOne.class)
Expand Down Expand Up @@ -255,16 +249,4 @@ private void createJavaClass(JavaFile javaFile) {
e.getMessage());
}
}


//TODO refactor with the more right way see:
// https://stackoverflow.com/questions/7687829/java-6-annotation-processing-getting-a-class-from-an-annotation
private TypeMirror getSourceClass(Relation annotation) {
try {
annotation.sourceClass(); // this should throw
} catch (MirroredTypeException mte) {
return mte.getTypeMirror();
}
return null; // can this ever happen ??
}
}
2 changes: 1 addition & 1 deletion dynamic-relations/src/test/resources/TestFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Relation(sourceClass = TestFile.class)
@Relation
@Entity
public class TestFile {
@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import jakarta.persistence.Id;

@IgnoreRelation
@Relation(sourceClass = TestFileWithIgnoreRelation.class)
@Relation
@Entity
public class TestFileWithIgnoreRelation {
@Id
Expand Down
2 changes: 1 addition & 1 deletion testing/src/main/java/at/test/drm/AnnotationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Relation(sourceClass = AnnotationTest.class)
@Relation
@Entity
@Data
public class AnnotationTest implements RelationIdentity {
Expand Down
2 changes: 1 addition & 1 deletion testing/src/main/java/at/test/drm/AnnotationTest2.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Relation(sourceClass = AnnotationTest2.class)
@Relation
@Entity
@Data
public class AnnotationTest2 implements RelationIdentity {
Expand Down
2 changes: 1 addition & 1 deletion testing/src/main/java/at/test/drm/AnnotationTest3.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Relation(sourceClass = AnnotationTest3.class)
@Relation
@Entity
@Data
public class AnnotationTest3 implements RelationIdentity {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package at.test.drm;

import at.drm.DrmConfig;
import at.drm.EnableDynamicRelation;
import at.drm.model.RelationLink;
import at.drm.service.RelationService;
import io.zonky.test.db.AutoConfigureEmbeddedDatabase;
Expand All @@ -9,15 +9,14 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;

import java.util.List;
import java.util.Set;

@SpringBootTest
@ActiveProfiles("integration")
@Import(DrmConfig.class)
@EnableDynamicRelation
@AutoConfigureEmbeddedDatabase(provider = DatabaseProvider.ZONKY)
class ApplicationIntegrationTest {

Expand Down