@DiscriminatorValue 类型不匹配:无法将 Integer 转换为 String

huangapple go评论77阅读模式
英文:

@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。

@DiscriminatorValue 类型不匹配:无法将 Integer 转换为 String

英文:

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=&quot;ITEMS&quot;)
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name=&quot;ITEM_CATEGORY&quot;,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:

&lt;parent&gt;
	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
	&lt;version&gt;2.0.0.RELEASE&lt;/version&gt;
	&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;

&lt;properties&gt;
	&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
	&lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
	&lt;java.version&gt;1.8&lt;/java.version&gt;
	&lt;spring.version&gt;5.0.2.RELEASE&lt;/spring.version&gt;
&lt;/properties&gt;

&lt;dependencies&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-data-rest&lt;/artifactId&gt;
	&lt;/dependency&gt;

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

@DiscriminatorValue 类型不匹配:无法将 Integer 转换为 String

答案1

得分: 1

Java是一种静态类型语言。因此,discriminatorType=DiscriminatorType.INTEGER无法更改DiscriminatorValuevalue的类型。

例如在这里有解释
> DiscriminatorValue注解为每个类指定了鉴别器值
> 虽然此注解的值始终为字符串,但实现将根据DiscriminatorColumn上面的discriminatorType属性进行解析。

因此,您必须按以下方式指定鉴别器值:

@DiscriminatorValue(value=&quot;&quot;+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=&quot;&quot;+ItemCategory.Values.LEARNING_DUTY)

huangapple
  • 本文由 发表于 2020年9月1日 12:24:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63681367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定