有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java提供了错误类型Spring的id

我为这个问题挣扎了几天,希望你能帮助我解决它。也许我在泛型类型上犯了一些错误,但可能是Spring的一些不可预测的行为

这种情况非常奇怪,因为当我使用自动配置或自己配置实体管理器时,我发现应用程序的工作方式不同。在这两种情况下,它都不能正常工作,但当我尝试从数据库中获取实体时出现异常:

org.hibernate.TypeMismatchException: Provided id of the wrong type for class com.example.repoinherit.model.DocTwo. Expected: class com.example.repoinherit.model.DocOneId, got class com.example.repoinherit.model.DocTwoId

在第二种情况下,它只是在数据库中找不到具有特定ID的实体。在这两种情况下,保存数据都正常,并且所有数据都在数据库中

这种情况取决于我如何配置数据库连接。若我使用自动配置,我会得到异常,若我有自定义实体管理器,我找不到实体。不幸的是,昨天它的作用完全相反——它可能与下一个“有趣”的事实有关

我有3个实体,但问题总是只有一个。更有趣的是,这取决于他们的名字。当我更改类名时,可能会更改他们的顺序,而另一个类会停止工作

下面你们可以找到代码的摘录。我还准备了一个测试项目,你可以在这里找到https://gitlab.com/pin54/springproblem.git 这里没有测试,而是一个“真正的”控制器

每个实体(DocOne、DocTwo、DocThree)都是文档的派生类,而文档的派生类是AggregateRoot的派生类。它们看起来几乎相同,所以下面您只能找到一个实现。每个实体的ID(DocOneId、doctwid、docthreId)都是AggregateId的派生类

提前感谢您的支持

@MappedSuperclass
public abstract class AggregateRoot<ID extends AggregateId> {

    @EmbeddedId
    @Type(type = "org.hibernate.type.UUIDCharType")
    protected ID id;

    public AggregateRoot(ID id) {
        this.id = id;
    }

    public AggregateRoot() {

    }
}
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", columnDefinition = "varchar(10)")
@Table(name = "documents")
public abstract class Document<ID extends AggregateId> extends AggregateRoot<ID> {

    protected String number;

    public Document(ID id) {
        super(id);
    }

    public Document() {

    }
}
@Entity
@DiscriminatorValue("one")
public class DocOne extends Document<DocOneId> {

    public String docOneField;

    public DocOne(DocOneId id) {
        super(id);
        docOneField = "one";
    }

    public DocOne() {

    }
}
@Embeddable
@MappedSuperclass
public class DocOneId extends AggregateId {

    public DocOneId(String id) { super(id); }

    public DocOneId(UUID id) { super(id); }

    public DocOneId() {

    }
}
@Repository
public interface DocOneRepository extends JpaRepository<DocOne, DocOneId> {

}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = {
                "com.example.repoinherit.model"
        },
        entityManagerFactoryRef = "productionEntityManagerFactory",
        transactionManagerRef= "productionTransactionManager"
)
public class DatabaseConfigurationProduction {

    @Bean
    @Primary
    @ConfigurationProperties("app.datasource.production")
    public DataSourceProperties productionDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    @ConfigurationProperties("app.spring.jpa.production")
    public Map<String, String> productionJpaProperties() {
        return new HashMap<>();
    }

    @Bean
    @Primary
    @ConfigurationProperties("app.datasource.production.configuration")
    public DataSource productionDataSource() {
        return productionDataSourceProperties().initializeDataSourceBuilder()
                .type(HikariDataSource.class).build();
    }

    @Bean(name = "productionEntityManagerFactory")
    @Primary
    public LocalContainerEntityManagerFactoryBean productionEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        System.out.println(productionJpaProperties());
        return builder
                .dataSource(productionDataSource())
                .packages(new String[] {
                        "com.example.repoinherit.model"
                })
                .properties(productionJpaProperties())
                .build();
    }

    @Primary
    @Bean
    public PlatformTransactionManager productionTransactionManager(
            final @Qualifier("productionEntityManagerFactory") LocalContainerEntityManagerFactoryBean productionEntityManagerFactory) {
        return new JpaTransactionManager(productionEntityManagerFactory.getObject());
    }

}

编辑: 可能是我写错了,有一次它找不到实体——它可以。所以,当我为实体管理器配置时,它可以正常工作,当我不使用它时,它会抛出一个异常。但在第二个项目中,当我使用实体管理器的配置(相同的配置!)时,它会引发异常


共 (0) 个答案