简体   繁体   English

返回Java中通用对象(例如列表)的更干净的解决方案

[英]Cleaner solution to returning generic objects (e.g. Lists) in Java

I'm working with the JPA API (Hibernate-backed), and have the following code: 我正在使用JPA API(由Hibernate支持),并且具有以下代码:

public List<?> getGenericListing(Class<?> clazz) {
    //Other logic comes before...
    Query qry = entityManager.createNativeQuery(sql, clazz);
    return qry.getResultList(); //Returns an untyped list, part of JPA spec
}

public List<SpecificObject> getSpecificListing() {
    return (List<SpecificObject>) getGenericListing(SpecificObject.class);
}

Is there a better way to return the List<?> other than type-casting it against the List of SpecificObject? 除了针对SpecificObject的列表进行类型转换之外,还有没有更好的方法来返回List<?>

You can put the cast into getGenericListing : 您可以将演员表放入getGenericListing

public <T> List<T> getGenericListing(Class<T> clazz) {
    //Other logic comes before...
    Query qry = entityManager.createNativeQuery(sql, clazz);
    return (List<T>) qry.getResultList(); //Returns an untyped list, part of JPA spec
}

what's the nature of your sql? 你的sql的本质是什么? If dynamic queries feature isn't necessary to you then you can use this solution: 如果您不需要动态查询功能,则可以使用以下解决方案:

public <T> List<T> getGenericListing(Class<T> clazz) {
    //Other logic comes before...
    TypedQuery<T> qry = entityManager.createNamedQuery(sql, clazz);
    return qry.getResultList();
} 

where 'sql' is a name of NamedNativeQuery, for example declared as 其中“ sql”是NamedNativeQuery的名称,例如声明为

@NamedNativeQuery( 
    name="sql_name", 
    query="SELECT * " + 
          "FROM foo_table t " + 
          "WHERE t.foo_field = ? ", 
    resultClass=fooEntityClass.class 
) 

Here you don't need to cast the result, and namedQueryName is just the name of named query. 在这里,您不需要强制转换结果,namedQueryName只是命名查询的名称。

public <T> List<T> getGenericListing(final String namedQueryName,Class<T> clazz) {
      TypedQuery<T> qry = em.createNamedQuery(namedQueryName, clazz);
      return qry.getResultList(); 
}   

Additionally I prefer using xml to store named queries rather than strings in java class - it's more maintainable, so my persistence.xml has: 另外,我更喜欢使用xml而不是Java类中的字符串来存储命名查询-它更易于维护,因此我的persistence.xml具有:

        <mapping-file>META-INF/Finders.xml</mapping-file>

Following this approach you can change query without compiling project which I find helpful, excerpt from Finder.xml 按照这种方法,您可以在不编译项目的情况下更改查询,而我发现对项目没有帮助。

<named-query name="Country.findCountryByShortNameAndLocale">
    <query>
        SELECT c FROM Country c WHERE c.Country.shortName = :shortName AND c.language.locale = :locale
    </query>
</named-query>

暂无
暂无

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

相关问题 有没有办法将Java方法标记(例如注释)为永不返回? - Is there a way to mark (e.g. annotate) a Java method as never returning? 用Java重新排列对象(例如,用于Minecraft NBT文件) - Rearranging objects in Java (e.g. for Minecraft NBT files) Java泛型和数组类型,不是你想的(例如来自泛型类型的数组) - Java generic and array types, no not what you are thinking (e.g. arrays from generic types) 用Java转换通用类型(例如T值=(T)inputParam) - Casting Generic Types in Java (e.g. T value = (T) inputParam) java.lang.IllegalArgumentException:响应必须包括泛型类型(例如,响应<string> )</string> - java.lang.IllegalArgumentException: Response must include generic type (e.g., Response<String>) 用于泛型类型的Java转换“.class”-operator,例如列表,到“Class <List <?>>”和“Class <List <Integer >>” - Java casting “.class”-operator used on a generic type, e.g. List, to “Class<List<?>>” and to “Class<List<Integer>>” KeyEvent.getKeyText()在OSX中返回特殊字符(例如&#39;VK_ENTER&#39;),但在Windows XP上没有,任何解决方案? - KeyEvent.getKeyText() is returning special characters in OSX (e.g. for 'VK_ENTER') but not on Windows XP, any solution? 没有括号的泛型静态方法,例如在JavaFX 8中 - Generic static methods without brackets, e.g. in JavaFX 8 Java SonarQube 规则 squid:S1948 - 如何在不绑定到 ArrayList 的情况下使用可序列化列表 - Java SonarQube rule squid:S1948 - how to use serializable lists without being bound to e.g. ArrayList 如何使用Java中的Web服务(例如Axis2)发送复杂对象的数组或集合? - How do I send an array or collection of complex objects using web services in Java (e.g. Axis2)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM