I'm new in Spring. I want to create a repository that return Page of object array like this:
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface A extends JpaRepository<Object, Long> {
#Query("SELECT a, b FROM EntityA a FULL OUTER JOIN EntityB b ON "
+ "a.id = b.id")
Page<Object[]> findAll(Pageable pageable);
}
But I get:
nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object
Thank you
Related
Im devloping a crud application in spring-boot and i was wondering if i can do these two queries and methods in one:
package com.celulaweb.crud.repository;
import com.celulaweb.crud.domain.Cidade;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
#Repository
public interface CidadeRepository extends JpaRepository<Cidade, Long> {
#Query("select c from Cidade c where c.estado is null or c.estado = :estado")
Page<Cidade> listarPorEstado(#Param("estado") String estado, Pageable pageable);
#Query("select c from Cidade c where c.nome is null or c.nome like :nome")
Page<Cidade> listarPorNomeTendo(#Param("nome") String nome, Pageable pageable);
}
These are queries from org.springframework.data.jpa.repository.Query
How can i merge these two?
If you have the same Pageable on this methods try this
#Query("""
select c from Cidade c
where (c.estado is null or (c.estado = :estado))
and (c.nome is null or (c.nome like :nome))
"""
)
Page<Cidade> listarEntries(
#Param("estado") estado: String,
#Param("nome") nome: String,
pageable: Pageable
)
i have a class called "Invoice" and a MongoRepository
and what i want is to extract from my mongo database all validated invoices (those created in a given time range)
so here is my mongo repository :
import java.util.Date;
import java.util.List;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import Invoices.Invoice;
#Repository
public interface InvoiceRepositoryMongo extends MongoRepository<Invoice,Integer>{
#Query("db.invoices_bis.find({createdAt : {$gte : new ISODate('2013-04-30T17:24:16.000+00:00') , $lte : new ISODate('2013-05-30T17:24:16.000+00:00')}})")
List<Document> testrequete(Date start, Date ed);
}
dont pay too much attention to the query it is just for testing , but the problem is when i run this , i have this error :
nested exception is org.springframework.data.mapping.PropertyReferenceException: No property testrequete found for type Invoice!
i think the problem is that the method return a list of but i'm not sure
thanks !
i think te problem is that your Entity calls Invoice,
MongoRepository<Invoice,Integer>
so the result should be something like :
List<Invoice> testrequete(Date start, Date ed);
I'm new to Spring.
I wrote a class:
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller
public class Kontroller
{
#Query( value="select * from teszt"
, nativeQuery = true
)
List<Teszt> osszesSor(); // <-- error
//.....
#RequestMapping("/szam")
public #ResponseBody String szamossag()
{
List<Teszt> sokasag = osszesSor();
return("számosság="+sokasag.size());
}
}
And it says (it = the IDE (STS), and at runtime when I call osszesSor()):
This method requires a body instead of a semicolon.
The Teszt class is:
import java.io.Serializable;
import javax.persistence.*;
#Entity
#IdClass(Teszt.class)
public class Teszt implements Serializable {
#Id
private String kulcs;
#Id
private String ertek;
//getters, setters
}
It's a very simple table (that's why it becomes very complex in spring, because it has not a one-column key):
create table TESZT
(
KULCS VARCHAR2(2000) not null,
ERTEK VARCHAR2(2000) not null,
constraint TESZT_UN
unique (KULCS, ERTEK)
)
Now I know.
I had to create a repo interface:
public interface TesztRepo extends Repository<Teszt, Teszt>
{
#Query( value="select * from teszt"
, nativeQuery = true
)
List<Teszt> sokasag();
//...
}
and autowire it in the controller.
#Autowired
TesztRepo dao;
//...
List<Teszt> sokasag = dao.sokasag();
Not that complex.
I have a repository looking like this (with other CRUD methods stripped)
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.StringPath;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
import org.springframework.data.querydsl.binding.QuerydslBindings;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.lang.NonNull;
import org.springframework.security.access.prepost.PreAuthorize;
import java.util.Optional;
import java.util.UUID;
#PreAuthorize("hasRole('" + Constants.ROLE_USER + "')")
public interface ProjectRepository extends CrudRepository<Project, UUID>, QuerydslPredicateExecutor<Project>, QuerydslBinderCustomizer<QProject> {
#Override
default void customize(#NonNull QuerydslBindings bindings, #NonNull QProject root) {
bindings.bind(String.class).first(
(StringPath path, String value) -> path.containsIgnoreCase(value));
bindings.including(root.name);
bindings.including(root.description);
}
#Override
#Query("select p from Project p left join p.roles r left join r.account a where ?#{principal.username} = a.username")
#NonNull
Page<Project> findAll(#NonNull Predicate predicate, #NonNull Pageable pageable);
}
As you can see I have a #Query annotation that limits the response of findAll based on who the user is. This causes the Predicate to be ignored entirely. So if I search for anything, it still returns all objects the user has access to. If I remove the #Query annotation then the searching works correctly. But of course I want my security to be applied. Is this a bug in QueryDsl? Or simply a limitation? How could I make this work?
I'm working on a RESTful Spring Boot project that fetches data from MySQL DB.
I want to print just all categories that have active field equal to 1
and I want to apply that for all method in CategoryRepository Class: findAll, findByParentId ..etc.
package com.userService.repositories;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import com.userService.entities.Category;
public interface CategoryRepo extends JpaRepository<Category, Integer> {
#Query("where active =1")
public List<Category> findByParentId(int id);
}
I tried to use the query method but it doesn't work and gave me an exception
If you are using Hibernate as your persistence provider, you can take advantage of #Where clause on the entity level:
#Where(clause = "active =1")
#Entity
public class Category{
This will be applied to all queries that go through the persistence provider.
it could be helpful to you if you are using the query method you should specify
select alias_name from Category c where condition
or
from Category where condition
or
directly by using method
findByActive(int id);
public interface CategoryRepo extends JpaRepository<Category, Integer> {
#Query("select c from Category c where c.active =1")
public List<Category> findByParentId(int id);
}
#Query annotation allows to execute native queries. So, I think, you should specify the complete sql as follows
#Query("select c from Category c where c.active =1")