当前位置: 首页 > 知识库问答 >
问题:

当我想要返回结果列表时,如何修复Java中的“class Java.lang.long不能强制转换为class Java.lang.integer”错误?

管翼
2023-03-14

当我想返回查询结果并放入列表时,我得到错误“java.lang.ClassCastException:类java.lang.Long不能被强制转换为类java.lang.Integer(java.lang.Long和java.lang.Integer在加载器'bootstrap'的模块java.base中)”。我尝试更改类型值,但错误没有消失。

Query query = session.createQuery("SELECT size_file FROM File WHERE storage.id =: storage_id", Number.class)
                    .setParameter("storage_id", storage.getId());

            List<Number> list=(List<Number>)query.getResultList();
@Entity
@Table(name = "FILE_STORAGE")
public class File {

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    @Column(name = "NAME")
    private String name;
    @Column(name = "FORMAT")
    private String format;
    @Column(name = "SIZE_FILE")
    private int size_file;
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "STORAGE_ID")
    private Storage storage;

    public long getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }


    public long getSize_file() {
        return size_file;
    }

    public void setSize_file(int size_file) {
        this.size_file = size_file;
    }

    public Storage getStorage() {
        return storage;
    }

    public void setStorage(Storage storage) {
        this.storage = storage;
    }

    @Override
    public String toString() {
        return "File{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", format='" + format + '\'' +
                ", size_file=" + size_file +
                ", storage=" + storage +
                '}';
    }
CREATE TABLE FILE_STORAGE(
    ID NUMBER NOT NULL ENABLE,
    CONSTRAINT FILE_PK PRIMARY KEY (ID),
    NAME NVARCHAR2(50) NOT NULL,
    FORMAT NVARCHAR2(50) NOT NULL,
    SIZE_FILE NUMBER NOT NULL,
    STORAGE NUMBER NULL,
    CONSTRAINT STORAGE_ID_FK FOREIGN KEY (STORAGE_ID) REFERENCES STORAGE(ID)
);
авг. 14, 2019 8:10:14 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.1.Final}
авг. 14, 2019 8:10:21 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
авг. 14, 2019 8:10:23 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
авг. 14, 2019 8:10:25 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@gromcode-lessons.chrtxxrhorjs.us-east-2.rds.amazonaws.com:1521:ORCL]
авг. 14, 2019 8:10:25 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {password=****, user=main}
авг. 14, 2019 8:10:25 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
авг. 14, 2019 8:10:25 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
авг. 14, 2019 8:10:30 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
авг. 14, 2019 8:10:37 AM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Exception in thread "main" java.lang.ClassCastException: class java.lang.Long cannot be cast to class java.lang.Integer (java.lang.Long and java.lang.Integer are in module java.base of loader 'bootstrap')
    at org.hibernate.type.descriptor.java.IntegerTypeDescriptor.unwrap(IntegerTypeDescriptor.java:19)
    at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$1.doBind(IntegerTypeDescriptor.java:46)
    at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:74)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:276)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:271)
    at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:53)
    at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:645)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:2006)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1920)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1898)
    at org.hibernate.loader.Loader.doQuery(Loader.java:937)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340)
    at org.hibernate.loader.Loader.doList(Loader.java:2695)
    at org.hibernate.loader.Loader.doList(Loader.java:2678)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2512)
    at org.hibernate.loader.Loader.list(Loader.java:2507)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:504)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:396)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:224)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1538)
    at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1561)
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1529)
    at org.hibernate.query.Query.getResultList(Query.java:168)
    at com.lesson3.hometask.DAO.DAO.listFileSize(DAO.java:276)
    at com.lesson3.hometask.validate.Validate.checkMaxSizeInStorage(Validate.java:36)
    at com.lesson3.hometask.Service.Service.put(Service.java:43)
    at com.lesson3.hometask.Controller.Controller.put(Controller.java:23)
    at com.lesson3.hometask.Demo.Demo.main(Demo.java:27)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

共有1个答案

艾奕
2023-03-14

将long getId()更改为int getId();

 类似资料:
  • 当我想返回查询结果并放入列表时,我得到错误"java.lang.ClassCastException: classjava.lang.Long不能转换为类java.lang.整数(java.lang.Long和java.lang.整数在加载器'bootstrap'的模块java.base)"。我试图更改类型值,但错误没有消失。

  • 我试了所有的办法,但都不起作用。应用程序一次又一次崩溃。拜托,救命。 这是用于Java和XML的应用程序代码。错误为 我的mainactivity.java代码如下所示 activity_main.xml的代码是

  • 所以,我试图建立一个报告。某些特定类型的用户的详细信息需要写在excel文件中。在到达endpoint时,它返回500,并使用以下StackTrace。 我希望将此模型用户配置文件的一些细节写入excel表中,并使其可供下载。你觉得我怎么解决这个问题。主要的错误来自这几行,特别是粗体字的那行

  • copy(文件,文件)只是复制一个文件。 java.lang.ClassCastException:sun.util.ResourceBundleEnumeration不能在java.util.Properties.Store0(未知源)~[?:1.7.0_21]在java.util.Properties.Store(未知源)~[?:1.7.0_21]在Bammerbom.UltimateCore

  • 我正在建立一个弹性搜索索引,并希望它的行为(返回结果)某种方式。我已经建立了父母/孩子的关系。 我已经用一些“父”文档和一堆父文档设置正确的“子”文档填充了它。 当我使用普通搜索查询搜索内容时,我当然会返回所有匹配的文档。父文档和子文档,但两者之间没有联系。如果我使用has_child筛选器搜索内容,它将正确地搜索子文档,并向我返回匹配的父文档: 问题是,我想搜索孩子,并在一个文档中找回父母和孩子

  • 我必须和整个AAB,付款条件,货币,检查法规和姓名的号码。 我想通过迭代结果列表来实现这一点: 我的问题是如何强制转换res对象才能获得具体的列值?我读过关于在hibernate查询中创建映射的文章,但我不知道通过添加