Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate that Kotlin entities do not extend PanacheEntity and also declare its own ID #13829

Merged
merged 1 commit into from
Dec 11, 2020
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
35 changes: 35 additions & 0 deletions extensions/panache/hibernate-orm-panache-kotlin/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,41 @@
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/test/java</source>
<source>src/test/kotlin</source>
</sourceDirs>
</configuration>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
import java.util.stream.Collectors;

import javax.persistence.EntityManager;
import javax.persistence.Id;
import javax.persistence.Transient;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Type;

import io.quarkus.arc.deployment.UnremovableBeanBuildItem;
import io.quarkus.arc.deployment.ValidationPhaseBuildItem;
import io.quarkus.arc.processor.BeanInfo;
import io.quarkus.builder.BuildException;
import io.quarkus.deployment.Feature;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
Expand All @@ -34,6 +38,7 @@
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.util.JandexUtil;
import io.quarkus.hibernate.orm.deployment.AdditionalJpaModelBuildItem;
import io.quarkus.hibernate.orm.deployment.HibernateEnhancersRegisteredBuildItem;
import io.quarkus.hibernate.orm.deployment.JpaModelPersistenceUnitMappingBuildItem;
Expand All @@ -47,7 +52,8 @@
import io.quarkus.panache.common.deployment.TypeBundle;

public final class KotlinPanacheResourceProcessor {

private static final DotName DOTNAME_ID = DotName.createSimple(Id.class.getName());
private static final DotName DOTNAME_PANACHE_ENTITY = DotName.createSimple(PanacheEntity.class.getName());
private static final Set<DotName> UNREMOVABLE_BEANS = singleton(createSimple(EntityManager.class.getName()));
static final DotName TRANSIENT = DotName.createSimple(Transient.class.getName());

Expand Down Expand Up @@ -206,4 +212,21 @@ List<AdditionalJpaModelBuildItem> produceModel() {
// only transforms classes from the application jar, so we do our own transforming
return Collections.singletonList(new AdditionalJpaModelBuildItem(PanacheEntity.class));
}

@BuildStep
ValidationPhaseBuildItem.ValidationErrorBuildItem validate(ValidationPhaseBuildItem validationPhase,
CombinedIndexBuildItem index) throws BuildException {
// we verify that no ID fields are defined (via @Id) when extending PanacheEntity
for (AnnotationInstance annotationInstance : index.getIndex().getAnnotations(DOTNAME_ID)) {
ClassInfo info = JandexUtil.getEnclosingClass(annotationInstance);
if (JandexUtil.isSubclassOf(index.getIndex(), info, DOTNAME_PANACHE_ENTITY)) {
BuildException be = new BuildException("You provide a JPA identifier via @Id inside '" + info.name() +
"' but one is already provided by PanacheEntity, " +
"your class should extend PanacheEntityBase instead, or use the id provided by PanacheEntity",
Collections.emptyList());
return new ValidationPhaseBuildItem.ValidationErrorBuildItem(be);
}
}
return null;
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.hibernate.orm.panache.kotlin.deployment.test

import io.quarkus.hibernate.orm.panache.kotlin.PanacheEntity
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

class DefaultPanacheEntityToStringTest {
@Test
fun testDefaultToStringMethod() {
val myPanacheEntityWithId = MyPanacheEntity(2912L)
Assertions.assertEquals("MyPanacheEntity<2912>", myPanacheEntityWithId.toString())
val myPanacheEntityWithNullId = MyPanacheEntity(null)
Assertions.assertEquals("MyPanacheEntity<null>", myPanacheEntityWithNullId.toString())
}

internal class MyPanacheEntity(id: Long?) : PanacheEntity() {
init {
this.id = id
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.quarkus.hibernate.orm.panache.kotlin.deployment.test

import io.quarkus.hibernate.orm.panache.kotlin.PanacheEntity
import javax.persistence.Id

class DuplicateIdEntity : PanacheEntity() {
@Id
var customId: String? = null
}
Loading