查询:参数中的属性名称 [英] Query : property name in parameter

查看:106
本文介绍了查询:参数中的属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过此查询,我成功检索了数据库中的电话号码:

With this query, I succeed to retrieve a phone number in database:

import java.util.List;
import org.springframework.data.jpa.repository.JpaReposit ory;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.mc.appcontacts.domain.hibernate.Contact;

public interface ContactRepository extends JpaRepository<Contact, Integer> {


@Query("SELECT c.phoneNumber from Contact c WHERE LOWER(c.name) = LOWER(:name)")
String find(@Param("name") String name);

但是可以在参数中动态指定要检索的属性的名称吗?

But is it possible to specify dynamically name of the property i want to retrieve in parameter?

在网上阅读的所有tuto中,我了解到我们可以在参数中传递属性的值(在我的示例中:@Param("name")字符串名) 但是我要传递的参数是属性的名称而不是值!

In all tuto i've read on the net, i learn we can pass the value of the property in parameter (In my exemple : @Param("name") String name ) but what i want to pass in parameter is the name of the property not the value !

我知道下面的例子是不正确的,但这是为了给出大致的想法:

I know the exemple below is not correct but it's to give the general idea :

@Query(从联系人c WHERE LOWER(c.name)= LOWER(:name)中选择c.(属性)") 字符串查找(@Param("name")字符串名称,@ Param("property")字符串属性);

@Query("SELECT c.(property) from Contact c WHERE LOWER(c.name) = LOWER(:name)") String find(@Param("name") String name, @Param("property") String property);

具有property = phoneNumber(或我的表格的其他属性).

With property = phoneNumber (or an other property of my table).

谢谢您的帮助!

我不知道该怎么做(一切对我来说都是新的):

I don't understand how to do that (everything is new for me):

我已经阅读(并尝试过)像这样定义jpql:

I have read (and try) that jpql is defined like this :

import com.mysema.query.jpa.impl.JPAQuery;
import com.mc.appcontacts.repository.ContactRepository;  // managed by spring data 
                                                         //jpa repository

public class ServicesC { 

      @Autowired
      private ContactRepository repository;

      @PersistenceContext                        // try
      private EntityManager em;                  // try
      JPAQuery query = new JPAQuery(em);     // try

      public Contact getOne(Integer id) {
             return repository.findOne(id);
      }

      public String findAtt(String att, String key){
            String jpql = "SELECT c." + att + " from Contact c WHERE LOWER(c.name) = LOWER(:key)";   // try
            List<Contact> l = (List<Contact>) em.createQuery(jpql);   // try
            return "test";
    }
}

但是它不起作用(我不感到惊讶...):

But it doesn't work (i'm not surprised...) :

2014-02-24 18:18:34.567:WARN::Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appMapping': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mc.appcontacts.service.ServiceC com.mc.appcontacts.mvc.MappingService.service; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Service' defined in file [C:\Professional\Workspaces\Eclipse\ContactMain\ContactCore\target\classes\com\mc\appcontacts\service\ServiceC.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.mc.appcontacts.service.ServiceC]: Constructor threw exception; nested exception is java.lang.NullPointerException:
    java.lang.NullPointerException
at com.mysema.query.jpa.impl.JPAProvider.get(JPAProvider.java:72)
at com.mysema.query.jpa.impl.JPAProvider.getTemplates(JPAProvider.java:80)
at com.mysema.query.jpa.impl.JPAQuery.<init>(JPAQuery.java:46)

我必须仅为jpql定义第二个EntityManager吗? (有可能吗?这是正确的方法吗?我不这样认为...) 我已经在xml文件中为Spring-data定义了EntityManager:

Must i define a second EntityManager only for jpql ? (Is it possible ? is it the right way ? I don't think so...) I have already a EntityManager defin for Spring-data in xml file :

<tx:annotation-driven transaction-manager="transactionManager" />   

<!-- Activate Spring Data JPA repository support -->
<jpa:repositories base-package="com.mc.appcontacts.repository" />

<!-- Declare a JPA entityManagerFactory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:META-INF/contacts/hibernate/persistence.xml" />
    <property name="persistenceUnitName" value="hibernatePersistenceUnit" />
    <!-- <property name="dataSource" ref="dataSource" /> -->
    <property name="jpaVendorAdapter" ref="hibernateVendor" />
</bean>

<!-- Specify our ORM vendor -->
<bean id="hibernateVendor" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="${hibernate.showSql}" />
</bean>

<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

请帮助我...它是如何工作的?

Please help me ... how does it work ?

推荐答案

对于这种动态构建查询的用例,我认为最好的选择是探索Criteria API,它非常适合此类情况. http://docs.oracle.com/javaee/6/tutorial/doc/gjitv.html

I think for this use case of building queries dynamically your best bet would be to explore Criteria API, which is very suitable for such things. http://docs.oracle.com/javaee/6/tutorial/doc/gjitv.html

这篇关于查询:参数中的属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆