如何排除/禁用特定类的@Entity注解

huangapple go评论53阅读模式
英文:

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 result = o.stream().map(arr -> new MyClass((Long) arr[0], (String) arr[1])).collect(Collectors.toList());


或者你也可以使用JdbcTemplate代替EntityManager:

@Autowired
private JdbcTemplate jdbcTemplate;

public List runQuery() {
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&lt;Object[]&gt; o = entityManager.createNativeQuery(&quot;select Id,PRKEY,PRVALUE from Priority&quot;).getResltList();

List&lt;MyClass&gt; result = o.stream().map(arr -&gt; 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&lt;MyClass&gt; runQuery() {
        String select = &quot;select Id,yourParameterHere from Priority&quot;;

        return jdbcTemplate.query(select, (rs, rowNum) -&gt; new MyClass(rs.getLong(&quot;Id&quot;), rs.getString(&quot;yourParameterHere&quot;)));
    }

huangapple
  • 本文由 发表于 2023年2月6日 12:37:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定