简体   繁体   English

如何使用 JPA 实体防止 ClassCastException?

[英]How to prevent ClassCastException with JPA entities?

I get a ClassCastException when trying to query my JPA entity class.尝试查询 JPA 实体类时出现 ClassCastException。 I only want json to show two columns.我只希望 json 显示两列。 That is name and address.那是姓名和地址。 How do I only show selected columns in JPA?如何仅在 JPA 中显示选定的列? From debugging it has to be the for loop.从调试来看,它必须是 for 循环。 So List is the object and I need to make the right side an object instead of a list correct?所以 List 是对象,我需要使右侧成为对象而不是列表正确吗?

Entity实体

@Entity
@Table(name = "Personnel")
public class User implements Serializable {

private String id;
private String name;
private String address;

public User(String id, String name, String address) 
{
this.id = id;
this.name = name;
this.address = address;
}

@Id
@Column(name = "name", unique = true, nullable = false)
public String getName() {
    return this.name;
}....
 //setters getters

Query/Impl查询/实现

public List<User> getRecords(User ent){

     String sql = "select "
                + " usr.name, usr.address "                 
                + " from User usr"           
                + " where usr.id = '1' ";

    List<User> records = this.getSession().createQuery(sql).list();
    for ( User event : records ) {
        System.out.println( "Records (" + event.getName + ") );
    }

    return records;
 }

Update更新

This is my attempt to declare the result object as List.这是我尝试将结果对象声明为 List。 Does the method have to be an object instead of ?该方法是否必须是一个对象而不是 ?

public List<User> getRecords(User ent){
     String sql = "select "
                + " usr.name, usr.address "                 
                + " from User usr"           
                + " where usr.id = '1' ";

       Map<String, String> results = new HashMap<String, String>();
       List<Object[]> resultList = this.getSession().createQuery(sql).list();

       // Place results in map
       for (Object[] items: resultList) {
          results.put((String)items[0], (String)items[1]);
          results.toString();
       }
       return (List<User>) results;

You can pass a DTO class to the SELECT clause like this:您可以将 DTO 类传递给 SELECT 子句,如下所示:

List<UserDTO> resultList = this.getSession().createQuery("""
    select
        new my.package.UserDTO(usr.name, usr.address)
    from User usr
    where usr.id = :userId
    """)
.setParameter("userId", userId)
.getResultList();

You should read the complete object:您应该阅读完整的对象:

String sql = " from User usr"           
            + " where usr.id = '1' ";

Or declare the result object as List<Object[]>或者将结果对象声明为List<Object[]>

您可以使用泛型(我保证您使用的是java 1.5及更高版本),并且您会得到结果,您可以进行类型检查并向下转换为所需的类型。

If You don't want to read the complete object you have to use Criteria and Projection on specific properties.如果您不想读取完整的对象,则必须对特定属性使用 Criteria 和 Projection。

See this answer it will help看到这个答案会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM