通过使用占位符的 JPA 命名查询将列名作为参数传递,返回的数据不正确。

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

Passing column name as a param using the placeholder jpa named query returning incorrect data

问题

代替列值,我从下面的代码中得到了列名的列表

例如

应该得到 [12.4,54.6,65.7,88.7,44.8]
但得到 [onsite,onsite,onsite,onsite,onsite]

DAO:

public List<String> getLocation(String location, String roles, String title, String tier, Integer level) {
    
    List<String> result = new ArrayList<>();
    try {
                            
          Query query = entityManager.createNamedQuery("getLocation");
          query.setParameter("location", location); // location value onsite
          query.setParameter("title", title); 
          query.setParameter("roles", roles);
          query.setParameter("tier", tier);
          query.setParameter("level", level);
          result = query.getResultList(); 
    }
    catch(Exception e) {
        logger.error(e.getMessage());
    }
     return result;
}

实体类:

@Entity
@NamedNativeQueries({
    
    @NamedNativeQuery(name = "getLocation", 
    query = "select :location FROM myschema.test  where title=:title and roles=:roles and level=:level and tier=:tier"
      )
    
    })
@Table(name = "test", schema = "mySchema")
public class TestEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;
    
    @Column(name = "name")
    private String name;

    @Column(name = "title")
    private String title;

    @Column(name = "roles")
    private String roles;

    @Column(name = "level")
    private Integer level;
    
    @Column(name = "tier")
    private String tier;
    
    @Column(name = "diversity")
    private String diversity;
    
    @Column(name = "onsite")
    private Double onsite;

    @Column(name = "offsite")
    private Double offsite;
}

Service 代码:

List dbLocations = supplierDao.getLocation("onsite", "job role", "job title", "tier1", "8");

jpa 命名查询返回的是列名列表,而不是值。

英文:

Instead of column values i am getting the column name in a list from below code

for example

should get [12.4,54.6,65.7,88.7,44.8]

but getting [onsite,onsite,onsite,onsite,onsite]

DAO:

public List&lt;String&gt; getLocation(String location,String roles, String title,String tier,Integer level) {
		
		List&lt;String&gt; result = new ArrayList&lt;&gt;();
		try {
								
			  Query query = entityManager.createNamedQuery(&quot;getLocation&quot;);
			  query.setParameter(&quot;location&quot;, location);//location value onsite
			  query.setParameter(&quot;title&quot;,title); 
			  query.setParameter(&quot;roles&quot;,roles);
			  query.setParameter(&quot;tier&quot;, tier);
			  query.setParameter(&quot;level&quot;, level);
			  result = query.getResultList(); 
		}
		catch(Exception e) {
			logger.error(e.getMessage());
		}
		 return result;
	}

Entity class:

@Entity

@NamedNativeQueries({
	
	@NamedNativeQuery(name = &quot;getLocation&quot;, 
    query = &quot;select :location FROM myschema.test  where title=:title and roles=:roles and level=:level and tier=:tier&quot;
      )
	
	})
@Table(name = &quot;test&quot;, schema = &quot;mySchema&quot;)
public class TestEntity {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = &quot;id&quot;)
	private Long id;
	
	
	@Column(name = &quot;name&quot;)
	private String name;

	@Column(name = &quot;title&quot;)
	private String title;

	@Column(name = &quot;roles&quot;)
	private String roles;

	@Column(name = &quot;level&quot;)
	private Integer level;
	
	@Column(name = &quot;tier&quot;)
	private String tier;
	
	@Column(name = &quot;diversity&quot;)
	private String diversity;
	
	@Column(name = &quot;onsite&quot;)
	private Double onsite;

    @Column(name = &quot;offsite&quot;)
	private Double offsite;
}

Service code:

List<String> dbLocations = supplierDao.getLocation("onsite", "job role","job title","tier1","8");

jpa Named query is returning list of colum name instead of values

答案1

得分: 0

给它们在任何地方都取相同的名称,无论何时使用它们。如果你正在使用一个 HTML 文件来检索这些数据,同样也要如此。名称的歧义可能是这个问题的原因。

英文:

Give them the same name every where, whenever you are using them. If you are using a html file to retrieve these data, then also. The name ambiguity may be the reason to this problem.

答案2

得分: 0

我在项目中已经尝试过这个了。用那种方式行不通。更好的做法是在数据访问层(dao layer)创建一个类似于getLocation的方法,用于获取每一列,然后你可以根据传递的列名,在每个方法上使用switch case语句。

英文:

I have tried this already in my project.It won't work that way .Better what you can do is create a method at dao layer similar to getLocation for getting each column and the same you can call swith case on each method based on the column name passed.

huangapple
  • 本文由 发表于 2020年8月24日 14:24:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63555734.html
匿名

发表评论

匿名网友

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

确定