英文:
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
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<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 class:
@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 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论