You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为特定类禁用@Entity注解?附原生查询报错解决方案咨询

不使用@Entity注解实现多表下拉数据查询的解决方案

报错原因

你调用entityManager.createNativeQuery(sql, Priority.class)时,Hibernate要求第二个参数必须是被@Entity标记的持久化类,否则会抛出Unknown entity异常——因为它找不到该类的持久化映射元数据。

以下是几种不需要给目标类加@Entity的解决方案:

方案1:手动映射查询结果到GenericDropDown

让NativeQuery返回Object数组,再手动转换为你的GenericDropDown对象:

@Repository
public class DropDownDao {
    @Autowired
    private EntityManager entityManager;

    public List<GenericDropDown> runNativeQuery() {
        List<Object[]> resultList = entityManager.createNativeQuery("select Id, PRKEY, PRVALUE from Priority")
                .getResultList();
        
        // 手动映射字段到GenericDropDown
        return resultList.stream()
                .map(row -> {
                    GenericDropDown dropDown = new GenericDropDown();
                    dropDown.setId((Integer) row[0]);
                    dropDown.setKey((String) row[1]);
                    dropDown.setValue((String) row[2]);
                    return dropDown;
                })
                .collect(Collectors.toList());
    }
}

方案2:使用@SqlResultSetMapping映射到非实体类

通过@SqlResultSetMapping定义结果映射规则(可放在项目中任意已有的@Entity类上),无需给GenericDropDown加@Entity:

  1. 在已有实体类上添加映射配置:
@Entity
@SqlResultSetMapping(
        name = "GenericDropDownMapping",
        classes = @ConstructorResult(
                targetClass = GenericDropDown.class,
                columns = {
                        @ColumnResult(name = "Id", type = Integer.class),
                        @ColumnResult(name = "PRKEY", type = String.class),
                        @ColumnResult(name = "PRVALUE", type = String.class)
                }
        )
)
public class SomeExistingEntity {
    // 类原有代码...
}
  1. 修改查询代码,使用映射名称替代实体类:
@Repository
public class DropDownDao {
    @Autowired
    private EntityManager entityManager;

    public List<GenericDropDown> runNativeQuery() {
        @SuppressWarnings("unchecked")
        List<GenericDropDown> resultList = entityManager.createNativeQuery(
                "select Id, PRKEY, PRVALUE from Priority",
                "GenericDropDownMapping"
        ).getResultList();
        
        return resultList;
    }
}

方案3:改用Spring JdbcTemplate(更简洁)

如果项目依赖Spring JdbcTemplate,直接用它处理查询,完全不需要依赖@Entity注解:

@Repository
public class DropDownDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<GenericDropDown> runNativeQuery() {
        String sql = "select Id, PRKEY, PRVALUE from Priority";
        return jdbcTemplate.query(sql, (rs, rowNum) -> {
            GenericDropDown dropDown = new GenericDropDown();
            dropDown.setId(rs.getInt("Id"));
            dropDown.setKey(rs.getString("PRKEY"));
            dropDown.setValue(rs.getString("PRVALUE"));
            return dropDown;
        });
    }
}

内容的提问来源于stack exchange,提问作者SACHIN BHALLA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 17:31:57