英文:
How to exclude/disable @Entity Annotation for particular class
问题
I want to disable @Entity Annotation for particular class.
我想要禁用 @Entity 注解对特定类。
The above class is used for fetching data from multiple tables for rendering different dropdown lists from different tables.
上述类用于从多个表中获取数据,以渲染不同表的下拉列表。
How can I achieve this without @Entity Annotation?
如何在没有 @Entity 注解的情况下实现这一点?
Error: Unknown entity: com.min.test.Project.entity.Priority; nested exception is org.hibernate.MappingException: Unknown entity: com.min.test.Project.entity.Priority
错误: 未知实体:com.min.test.Project.entity.Priority;嵌套异常是 org.hibernate.MappingException:未知实体:com.min.test.Project.entity.Priority
英文:
I want to disable @Entity Annotation for particular class.
Here is my sample code.
@Component
public class GenericDropDown{
private Integer id;
private String key;
private String value;
// Standard getter and setter
The above class is used for fetching data from multiple table for rendering different dropdown list from different tables.
How I can achieve this without @Entity Annotation
Here is my sample code.
@Component
public class GenericDropDown{
private Integer id;
private String key;
private String value;
// Standard getter and setter
@Repository
public class DropDownDao {
@Autowired
private EntityManager entityManager;
public Object runNativeQuery() {
@SuppressWarnings("unchecked")
List<Priority> o= entityManager.createNativeQuery("select Id,PRKEY,PRVALUE from Priority",Priority.class)
.getResultList();
return o;
}
}
**Error:**Unknown entity: com.min.test.Project.entity.Priority; nested exception is org.hibernate.MappingException: Unknown entity: com.min.test.Project.entity.Priority
答案1
得分: 2
你可以选择列表对象数组并自行映射它们。
List<Object[]> o = entityManager.createNativeQuery("select Id,PRKEY,PRVALUE from Priority").getResultList();
List
或者你也可以使用JdbcTemplate代替EntityManager:
@Autowired
private JdbcTemplate jdbcTemplate;
public List
String select = "select Id,yourParameterHere from Priority";
return jdbcTemplate.query(select, (rs, rowNum) -> new MyClass(rs.getLong("Id"), rs.getString("yourParameterHere")));
}
英文:
You can select List of Objects array and map them yourself.
List<Object[]> o = entityManager.createNativeQuery("select Id,PRKEY,PRVALUE from Priority").getResltList();
List<MyClass> result = o.stream().map(arr -> new MyClass((Long) arr[0], (String) arr[1])).collect(Collectors.toList());
Or you also can use a JdbcTemplate instead of EntityManager:
@Autowired
private JdbcTemplate jdbcTemplate;
public List<MyClass> runQuery() {
String select = "select Id,yourParameterHere from Priority";
return jdbcTemplate.query(select, (rs, rowNum) -> new MyClass(rs.getLong("Id"), rs.getString("yourParameterHere")));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论