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

如何修复"类java.lang.长不能转换为类java.lang.整数"错误Java,当我想返回结果列表?

古弘
2023-03-14

当我想返回查询结果并放入列表时,我得到错误"java.lang.ClassCastException: classjava.lang.Long不能转换为类java.lang.整数(java.lang.Long和java.lang.整数在加载器'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();

 类似资料: