gpt4 book ai didi

java - 当我想要返回结果列表时,如何修复Java中的 "class java.lang.Long cannot be cast to class java.lang.Integer"错误?

转载 作者:行者123 更新时间:2023-12-02 09:36:14 25 4
gpt4 key购买 nike

当我想要返回查询结果并将其放入列表中时,出现错误“java.lang.ClassCastException:类 java.lang.Long 无法转换为类 java.lang.Integer(java.lang.Long 和 java.lang.Integer)” 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)

最佳答案

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

关于java - 当我想要返回结果列表时,如何修复Java中的 "class java.lang.Long cannot be cast to class java.lang.Integer"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57488941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com