2

Though I defined the parent class to use discriminator column of Type Integer, the compiler keeps giving error : Type mismatch: cannot convert from Integer to String for child class using Integer discriminator value

@Table(name="ITEMS")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="ITEM_CATEGORY",discriminatorType=DiscriminatorType.INTEGER)
public class Item { ....}


@Entity
@DiscriminatorValue(value=ItemCategory.Values.LEARNING_DUTY)
public class LearningDuty extends Item {...}


public static class Values {
        
        public static final Integer LEARNING_DUTY = 3;

    }

pom file:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring.version>5.0.2.RELEASE</spring.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>

I tried cleaning the project and rebuilding, restarting the IDE, but issue persists I am using STS 4

enter image description here

osama yaccoub
  • 1,884
  • 2
  • 17
  • 47

1 Answers1

1

Java is a statically typed language. So discriminatorType=DiscriminatorType.INTEGER cannot change the type of DiscriminatorValue's value.

It's explained e.g. here:

The DiscriminatorValue annotation specifies the discriminator value for each class. Though this annotation's value is always a string, the implementation will parse it according to the DiscriminatorColumn's discriminatorType property above.

Therefore you have to specify the discriminator value as follows:

@DiscriminatorValue(value=""+ItemCategory.Values.LEARNING_DUTY)
howlger
  • 31,050
  • 11
  • 59
  • 99