英文:
@DiscriminatorValue Type mismatch: cannot convert from Integer to String
问题
尽管我已经定义了父类来使用类型为整数的鉴别器列,但编译器一直在为使用整数鉴别值的子类报错:类型不匹配:无法从整数转换为字符串
。
@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 文件:
<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>
</dependencies>
我尝试了清理项目并重新构建,重启 IDE,但问题仍然存在。我正在使用 STS 4。
英文:
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
答案1
得分: 1
Java是一种静态类型语言。因此,discriminatorType=DiscriminatorType.INTEGER
无法更改DiscriminatorValue
的value
的类型。
例如在这里有解释:
> DiscriminatorValue
注解为每个类指定了鉴别器值
> 虽然此注解的值始终为字符串,但实现将根据DiscriminatorColumn
上面的discriminatorType
属性进行解析。
因此,您必须按以下方式指定鉴别器值:
@DiscriminatorValue(value=""+ItemCategory.Values.LEARNING_DUTY)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论