gpt4 book ai didi

java - query.getResultList 处的 ClassCastException

转载 作者:行者123 更新时间:2023-12-01 19:11:55 28 4
gpt4 key购买 nike

我有一个使用 hibernate 在 postrgresql 数据库上运行查询的函数。

当我调用 query.getResultList() 时,我收到此错误:

java.lang.ClassCastException: class java.lang.String cannot be cast to class java.util.UUID (java.lang.String and java.util.UUID are in module java.base of loader 'bootstrap')

at org.hibernate.type.descriptor.java.UUIDTypeDescriptor.unwrap(UUIDTypeDescriptor.java:20) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final] at org.hibernate.type.PostgresUUIDType$PostgresUUIDSqlTypeDescriptor$1.doBind(PostgresUUIDType.java:74) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final] at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:73) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final] at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:276) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final] at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:271) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]

Java函数:

@Override
public StudentEntity findByAccountId(UUID accountId) {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Query query = session.createQuery("select _student from StudentEntity _student where _student.accountId = :id_cont");
query.setParameter("id_cont", accountId.toString());
System.out.println(query.getResultList());
return new StudentEntity();
}

学生实体对象定义:

@Table(name = "Studenti")
@Entity(name = "StudentEntity")
public class StudentEntity implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "genname")
@SequenceGenerator(name = "genname", sequenceName = "seqname", allocationSize = 1)
@Column(name = "Id")
private UUID Id;

@Column(name = "Nume")
private String firstName;

@Column(name = "Prenume")
private String lastName;

@Column(name = "An")
private int year;

@Column(name = "Grupa")
private String grupa;

@Column(name = "Id_cont")
private UUID accountId;
}

最佳答案

通过将 accountId 的数据类型从 UUID 更改为 String 来更新您的实体:

@Table(name = "Studenti")
@Entity(name = "StudentEntity")
public class StudentEntity implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "genname")
@SequenceGenerator(name = "genname", sequenceName = "seqname", allocationSize = 1)
@Column(name = "Id")
private UUID Id;

@Column(name = "Nume")
private String firstName;

@Column(name = "Prenume")
private String lastName;

@Column(name = "An")
private int year;

@Column(name = "Grupa")
private String grupa;

@Column(name = "Id_cont")
private String accountId;
}

将 java 函数更改为:

@Override
public StudentEntity findByAccountId(UUID accountId) {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Query query = session.createQuery("select _student from StudentEntity _student where _student.accountId = :id_cont");
query.setParameter("id_cont", accountId); // send the parameter as it is
System.out.println(query.getResultList());
return new StudentEntity();
}

关于java - query.getResultList 处的 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59472888/

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