感谢您的关注,对不起我英语 :S
我正在使用 JPA 2.0 和 Hibernate 4.X 来执行一些 sql 本机查询。代码很简单:
private void doIt() throws Exception {
EntityManager em = getEntityManager();
Query q = em.createNativeQuery("A very simple native query, which return no entities, but collections of arrays");
q.setFirstResult(0);
q.setMaxResults(5);
Collection<Object> results = q.getResultList();
System.out.println("1"); //Means page 1
for (Object elem : results) {
String line = "";
Object[] row = (Object[]) elem;
for (Object object : row) {
if(object==null){
object="null";
}
line += object +"("+object.getClass()+")"+ ",";
}
System.out.println(row.length + " …Run Code Online (Sandbox Code Playgroud) org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [com.example.dto.ExampleDto]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.data.repository.query.ResultProcessor$ProjectingConverter.convert(ResultProcessor.java:293) ~[spring-data-commons-2.1.5.RELEASE.jar:2.1.5.RELEASE]
Run Code Online (Sandbox Code Playgroud)
当我在使用本机 JPA 查询中返回 2 个值的查询时,会抛出上述错误。我在下面的 DTO 中捕获查询响应:
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ExampleDto {
@Id
private String name1;
private int nameFlag;
}
Run Code Online (Sandbox Code Playgroud)
在 DAO 类中,我调用本机查询如下。该查询在 SQL Developer 中工作并返回 2 条记录。但是当如下调用时,它会抛出上述错误。
List<ExampleDto> getExampleDto = myJPARepository.
.findNameObject(uuid);
Run Code Online (Sandbox Code Playgroud)
DTO 类中有问题,我需要更改。注释?我不确定这里缺少什么,并尽我所能尝试,放入 @Entity 注释,@Data 注释,在调用查询时我无法解决此错误。
更新:与此相关的本机查询是
@Query(value = "select name1, nameFlag from NameTable",
nativeQuery = true, name = …Run Code Online (Sandbox Code Playgroud) 我有以下包含NativeQuery的地方,我需要在其中设置参数,但由于未设置参数而使错误,因此查询为
SELECT movieId, title, genres FROM movies where title like '%%'"
Run Code Online (Sandbox Code Playgroud)
因此返回所有行。怎么了
public List<T> findMovie(String keyword) {
Query q = getEntityManager().createNativeQuery("SELECT movieId, title, genres FROM movies where title like '%?%'", entityClass);
q.setParameter(1, keyword); //etc
return q.getResultList();
}
Run Code Online (Sandbox Code Playgroud) 我目前正在从事 SpringBoot + Hibernate 项目。我需要将一些值插入数据库。
我正在尝试使用本机查询来实现这一目标。下面是我的代码:
@Repository
public interface StoreDataRepository extends JpaRepository<StoreDataRepository, Long> {
@Query(value ="insert into store (id, name) values(:id, :name)", nativeQuery = true)
public void storeData(@Param("id") int id, @Param("name") String name);
Run Code Online (Sandbox Code Playgroud)
从我的服务中,我只是使用 id 和 name 参数调用此方法。
但我收到以下错误:
org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Method only for queries
Run Code Online (Sandbox Code Playgroud)
你能帮我吗?
执行以下查询时出现 SQL 异常。
@Query(value="SELECT * FROM lat_long WHERE ST_DWithin(geom :: geography,ST_SetSRID(ST_MakePoint(:lat, :lang),4326) :: geography,1000);",nativeQuery=true)
List<LatLong>find(@Param("lat")Double lat,@Param("lang")Double lang);
Run Code Online (Sandbox Code Playgroud)
休眠:
SELECT
*
FROM
lat_long
WHERE
ST_DWithin(geom : geography,ST_SetSRID(ST_MakePoint(?, ?),4326) : geography,1000);
Run Code Online (Sandbox Code Playgroud)
@Query(value="SELECT * FROM lat_long WHERE ST_DWithin(geom :: geography,ST_SetSRID(ST_MakePoint(:lat, :lang),4326) :: geography,1000);",nativeQuery=true)
List<LatLong>find(@Param("lat")Double lat,@Param("lang")Double lang);
Run Code Online (Sandbox Code Playgroud) 我想要实现的是在以下列方式定义的查询上设置结果转换器:
String hqlQueryString = "select o.id as id, o.name as objectName from MyObject";
Class resultClass = MyObject.class;
Query query = session.createQuery(hqlQueryString).setResultTransformer(
new new AliasToBeanResultTransformer(resultClass));
List result = query.list();
Run Code Online (Sandbox Code Playgroud)
MyObject看起来像这样:
public class MyObject {
private int id;
private String objectName;
public int getId() {
return id;
}
public void setId(int value) {
this.id = value;
}
public String getObjectName() {
return objectName;
}
public void setobjectName(String value) {
this.objectName = value;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,虽然我已指定id并objectName成为我的别名,但正在执行的实际查询使用不同的别名.这导致我AliasToBeanResultTransformer无法构造,MyObject …
我有一个List<Object[]> resultList = entityManager.createNativeQuery(SOME QUERY).getResultList();查询是内部联接表的结果.当我尝试访问Object []的元素时,其中一个是我尝试这样做的日期
for(Object[] obj : resultList)
{
Date createdDate = obj[7] !=null ? new Date(Long.valueOf(obj[7] + "")) : null;
}
Run Code Online (Sandbox Code Playgroud)
这给了明显的例外nested exception is java.lang.NumberFormatException: For input string: "2012-11-20"
我将Date更改为DateTime得到TO_CHAR(createdDate,'DD.MM.YYYY:HH24:MI:SS')并尝试下面的代码
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date createdDate = null;
try
{
createdDate = df.parse(obj[7] + "");
}
catch (ParseException e)
{
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
这种方法结果createdDate = null.有什么建议 ?
像这样:
public interface XXXRepository extends CrudRepository<XXX, Integer> {
@Query(value = "select * from ?1 where ...", nativeQuery = true)
List<XXX> findByXXX(String tableName, ...);}
Run Code Online (Sandbox Code Playgroud)
它在代码上给出了 MYSQL 语法错误。语法错误显示SQL中表名被“'”包围。
nativequery ×8
java ×5
hibernate ×3
jpa ×3
spring-boot ×2
alias ×1
date ×1
datetime ×1
inner-join ×1
postgis ×1
postgresql ×1
spring ×1