【问题标题】:JPA Could not serializeJPA 无法序列化
【发布时间】:2014-11-03 20:44:27
【问题描述】:

我有这个类,但是当我进行查询时会抛出异常:

org.hibernate.type.SerializationException: 无法序列化

@Entity
public class Google implements Serializable{

@Id
String nombre;
String pass;

public Google() {
    nombre = "defecto";
    pass = "defecto";
}

public Google(String anom, String apass) {
    nombre = anom;
    pass = apass;
} 
//Getters, setters..

}

这是查询,我正在使用 JPA、hibernate 和 MySQL DB,并且该类实现了 Serializable 我不知道是哪个问题。

public void findNombresGoogle(Map<String, Amigo> anombresAmigos){
    

        List<Google> resultados = new LinkedList<Google>();
        Map<String, Amigo> nombresAmigos = anombresAmigos;
        Query query = entityManager.createNativeQuery("FROM Google google WHERE "
                + "google.nombre IN (?1)", Google.class);
        query.setParameter(1, nombresAmigos);
        resultados = (List<Google>) query.getResultList();
}

【问题讨论】:

    标签: java mysql hibernate jpa serialization


    【解决方案1】:

    在下面一行:

    query.setParameter(1, nombresAmigos);
    

    nombresAmigos 应该是 List&lt;String&gt;,而不是 Map&lt;String, Amigo&gt;

    【讨论】:

    • 我改变了这个,但现在它抛出另一个错误:org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    • 不确定这个,但您可以尝试在查询字符串的开头添加“select google”。我认为只有 HQL 可以从 FROM 开始,而不是本地查询(JPQL 都不是)。
    • @AlexisHassler 你是对的,只有 HQL 以 FROM 开头。 See here。原生查询必须以 select 开头。
    • 它抛出了同样的异常。在日志中我有这个: SQL 错误:1064,SQLState:42000 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 ')' 附近使用正确的语法 我尝试的最后一个查询是: Query query = entityManager.createQuery("SELECT g FROM Google g WHERE g.nombre IN (?1) ", 谷歌.class); query.setParameter(1, 名词); //名词是 List
    • 不是同样的错误...而且我看到您已从 nativeQuery 切换到常规 Query。你确定 nombre 不是一个空的 lsit 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多