IN and = operator in JPA query language - java

I have a problem regarding jpa query.
There are two tables i.e. Post table and Tag table
There is many to many relationship between Post and Tag
Now I want to write a query such that when multiple tags are chosen then all the posts associated with those tags should be selected.
For example,
post1 has tags friends and motivation
post2 has tags motivation and pune
post3 has tag boxing
if tags friends and pune are chosen then post1 and post 2 should be retrieved
if tag boxing is chosen then only post 3 should be retrieved
if tags boxing and motivation are chosen then all three posts should be retrieved.
I tried following things
SELECT DISTINCT p FROM Post p JOIN p.tags tags WHERE p.tags IN :tags
but it gives validator error that
The state field path 'p.tags' cannot be resolved to a collection type.
If I try like this
SELECT DISTINCT p FROM Post p JOIN p.tags tags WHERE p.tags = :tags
then it complies fine but after passing a list of tags it gives error
java.lang.IllegalArgumentException: You have attempted to set a value of type class java.util.ArrayList for parameter tags with expected type of class com.justme.model.entities.Tag from query string SELECT DISTINCT p FROM Post p JOIN p.tags tags WHERE p.tags = :tags.
Thank you for reading this much :) can you please guide me on this?
how can I achieve the results mentioned above?
my persistence provider is eclipseLink
This is Post entity
#Entity
#NamedQueries({
#NamedQuery(name = "Post.selectAllPosts", query = "SELECT p FROM Post p ORDER BY p.dateCreated DESC"),
#NamedQuery(name = "Post.selectPostForUser", query = "SELECT p FROM Post p WHERE p.user = :user ORDER BY p.dateCreated DESC"),
#NamedQuery(name = "Post.selectPostsByTags", query = "SELECT DISTINCT p FROM Post p JOIN p.tags tags WHERE p.tags IN :tags") })
public class Post implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int idpost;
#Lob
private String content;
private String title;
// bi-directional many-to-one association to User
#ManyToOne(cascade = CascadeType.PERSIST)
#JoinColumn(name = "iduser")
private User user;
// bi-directional many-to-many association to Tag
#ManyToMany(cascade = CascadeType.PERSIST)
#JoinTable(name = "post_tag", joinColumns = #JoinColumn(name = "idpost"), inverseJoinColumns = #JoinColumn(name = "idtag"))
private List<Tag> tags = new ArrayList<Tag>();
#Temporal(TemporalType.DATE)
private Date date = null;
#Temporal(TemporalType.TIMESTAMP)
private Date dateCreated = new Date();
public Post() {
}
public int getIdpost() {
return this.idpost;
}
public void setIdpost(int idpost) {
this.idpost = idpost;
}
public String getContent() {
return this.content;
}
public void setContent(String content) {
this.content = content;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
public List<Tag> getTags() {
return this.tags;
}
public void setTags(List<Tag> tags) {
this.tags = tags;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Date getDateCreated() {
return dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
#Override
public String toString() {
return "Post [idpost=" + idpost + ", content=" + content + ", title="
+ title + ", date=" + date + "]";
}
}
This is Tag Entity
#Entity
#NamedQueries({
#NamedQuery(name = "Tag.selectTags", query = "SELECT tag FROM Tag tag WHERE tag.tagName LIKE :keyword"),
#NamedQuery(name = "Tag.selectMatchingTags", query = "SELECT t.tagName FROM Tag t WHERE t.tagName LIKE :keyword"),
#NamedQuery(name = "Tag.selectTagByName", query = "SELECT tag FROM Tag tag WHERE tag.tagName = :tagName"),
#NamedQuery(name = "Tag.selectTagsForAllPosts", query = "SELECT DISTINCT tag FROM Tag tag, Post post JOIN tag.posts posts WHERE post.user = :user")})
public class Tag implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int idtag;
private String tagName;
// bi-directional many-to-many association to Post
#ManyToMany(mappedBy = "tags", cascade = CascadeType.PERSIST)
private List<Post> posts;
public Tag() {
}
public Tag(String tagName) {
this.tagName = tagName;
}
public int getIdtag() {
return this.idtag;
}
public void setIdtag(int idtag) {
this.idtag = idtag;
}
public String getTagName() {
return this.tagName;
}
public void setTagName(String tagName) {
this.tagName = tagName;
}
public List<Post> getPosts() {
return this.posts;
}
public void setPosts(List<Post> posts) {
this.posts = posts;
}
#Override
public String toString() {
return tagName;
}
}

Try:
...
#NamedQuery(name = "Post.selectPostsByTags", query =
"SELECT DISTINCT p FROM Post p JOIN p.tags tags WHERE tags IN (:tags)") })
public class Post implements Serializable {
...
Use it like this:
#PersistenceContext
public EntityManager em;
...
List<Tag> ltags = new ArrayList<Tag>();
ltags.add(tagOne);
ltags.add(tagTwo);
List<?> list = em.createNamedQuery("Post.selectPostsByTags")
.setParameter("tags", ltags)
.getResultList();
for (Object object : list) {
System.out.println("Results: "+object);
}

Related

update query using #namedquery does not work properly

This is my DUsers class:
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import java.util.Date;
import java.util.Objects;
#Entity
#Table(name = "d_users")
#NamedQueries({
#NamedQuery(name = "bonsai.dropwizard.dao.d.DUsers.findAll",
query = "select e from DUsers e"),
#NamedQuery(name = "bonsai.dropwizard.dao.d.DUsers.findById",
query = "select e from DUsers e "
+ "where e.oAuthId = :id "),
#NamedQuery(name = "bonsai.dropwizard.dao.d.DUsers.findByOAuthId",
query = "select e from DUsers e "
+ "where e.oAuthId = :oAuthId "),
#NamedQuery(name = "bonsai.dropwizard.dao.d.DUsers.findByEmail",
query = "select e from DUsers e "
+ "where e.email = :email "),
#NamedQuery(name="bonsai.dropwizard.dao.d.DUsers.confirm",
query = "update DUsers set status = 'HELLO' where oAuthId = :id")
})
public class DUsers implements IDdbPojo{
#Id
#GeneratedValue(generator="system-uuid")
#GenericGenerator(name="system-uuid", strategy = "uuid")
private String id;
private String oAuthId;
private String oAuthType;
private String firstName;
private String secondName;
private String city;
private String phone;
private String email;
private String profileLink;
private String profilePic;
private String status;
private String notificationToken;
private boolean confirmed;
private String password;
private String notes;
private java.util.Date created_timestamp;
private java.util.Date updated_timestamp;
.. getters and setters on-going
As you can see, I have defined a few #NamedQueries and they all work properly except the last one that needs to update my database. In order to run this query, I defined two functions:
private void confirmMailDAO(String id) {
namedQuery("bonsai.dropwizard.dao.d.DUsers.confirm").setParameter("id", id);
}
public void confirmMailInternal(String id) {
Session session = sessionFactory.openSession();
try{
ManagedSessionContext.bind(session);
Transaction transaction = session.beginTransaction();
try{
confirmMailDAO(id);
transaction.commit();
}catch (Exception e) {
transaction.rollback();
throw new RuntimeException(e);
}
} finally {
session.close();
ManagedSessionContext.unbind(sessionFactory);
}
}
After this I defined a path followed by a POST request that should update my database but sadly it doesn't.
#POST
#Path("/confirm/{id}")
public void confirmMail(#NotNull #PathParam("id") String id){
DUsers user = AppConfig.getInstance().getdUsersDAO().findByIdInternal(id);
if (user == null) {
throw new NotAuthorizedException("Error");
}
AppConfig.getInstance().getdUsersDAO().confirmMailInternal(id);
}
Does anyone know where am I getting wrong?
You have set param in named query but forgot to execute it.
Pass session to your method and execute like:
private void confirmMailDAO(Session session, String id) {
Query query = session.getNamedQuery("bonsai.dropwizard.dao.d.DUsers.confirm").setParameter("id", id);
query.executeUpdate();
}

how to change object to string(java spring boot)

when i run the code i got the object
[[Ljava.lang.Object;#8f17f7c, [Ljava.lang.Object;#6c0a4f24,
[Ljava.lang.Object;#be4886c, [Ljava.lang.Object;#1760591d,
[Ljava.lang.Object;#14e9ce12, [Ljava.lang.Object;#2aa4c0c4,
[Ljava.lang.Object;#5ac9a14]
so.. i want to get the String result which in the below plz teach me the way
[Dataset_info(Ds_id=1111, ds_code=a, ds_name=e, ds_category=g, ds_stru=q, insert_ddtt=null, update_ddtt=null), Dataset_info(Ds_id=11111, ds_code=z, ds_name=eww, ds_category=g, ds_stru=q, insert_ddtt=null, update_ddtt=null)]
#Data
#Entity
#Table(name = "category")
#DynamicInsert
#DynamicUpdate
#NoArgsConstructor
#AllArgsConstructor
public class Category {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "category_id", columnDefinition = "INT(11)")
private Integer Category_id;
#Column(name = "name", columnDefinition = "VARCHAR(20)")
private String name;
#Column(name = "parent", columnDefinition = "int(11)")
private Integer parent;
}
this is my Category Code
#RestController
#RequestMapping(value = "/Category")
#Slf4j
public class CategoryController {
#Autowired CategoryRepository categoryRepository;
#RequestMapping(value = "/all", method =
RequestMethod.GET)
#ResponseBody
public String getCategoryList() {
List < Object[] > all =
this.categoryRepository.findByCategory();
return all.toString();
//log.info(query);
//return "Test";
}
}
this is my CategoryController code
import java.util.List;
#Repository
public interface CategoryRepository extends
JpaRepository < Category, Integer > {
public static final String FIND_PROJECTS = "SELECT t1.name
AS lev1,
t2.name as lev2,
t3.name as lev3,
t4.name as lev4
FROM category AS t1 LEFT JOIN category AS t2 ON t2.parent =
t1.category_id LEFT JOIN category AS t3 ON t3.parent =
t2.category_id LEFT JOIN category AS t4 ON t4.parent =
t3.category_id WHERE t1.name = 'ROOT'
";
#Query(value = FIND_PROJECTS, nativeQuery = true)
public List < Object[] > findByCategory();
}
this is my CategoryRepository Code
private void mysql2() {
this.categoryRepository.findByCategory();
}
this is my application Code for running
so plz teach me i crave to know the way
thank you
You can use Projection to contain these values :
public interface CategoryProjection {
public String getLev1();
public String getLev2();
public String getLev3();
public String getLev4();
}
Then use the interface Projection with Repository :
//...
#Query(value = FIND_PROJECTS, nativeQuery = true)
public List<CategoryProjection> findByCategory();
How to access values in Projections
Because It's a interface, only have getter method.
Using Loop (foreach loop, fori loop, ...)
ex :
List<CategoryProjection> list = categoryRepository.findByCategory();
list.forEach(c -> {
System.out.println(c.getLev1() + " - " + c.getLev2());
});
// loop i
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getLev1() + " - " + list.get(i).getLev2());
}
Using index
ex : to get Object Category in index 0
String lev1 = list.get(0).getLev1();
UPD: In your case, I think you can change the method return type to List, HttpMessageConverter would convert the result as JSON String to client. Hope it help.
#ResponseBody
public List getCategoryList() {
List<Object[]> all = this.categoryRepository.findByCategory();
return all;
}
Override toString() method in your POJO object. For example:
public class Category {
private Integer Category_id;
private String name;
private Integer parent;
//omitted getter/setter
#Override
public String toString() {
return "Category{" +
"Category_id=" + Category_id +
", name='" + name + '\'' +
", parent=" + parent +
'}';
}
}

Dynamically Query from List or two different entities

I have an entity called Person, inside that basic metadata, then inside that Tag and Language. I want to get all rows that contain specific tag name and language.
I came to know about Criteria Query about. How can we interlink two different entities together?
Example: Get all rows having the tag as Model and language as English.
#Entity
public Person {
#Id
private String id;
private BasicMetadata basicMetadata;
-----------
}
Basic Metadata table
#Entity
public BasicMetadata {
#Id
private String id;
private List<Tag> tags;
private List<Language> language;
-------------
}
Tag Table
#Entity
public Tag {
#Id
private String id;
private String name;
-------------
}
Language Table
#Entity
public Language{
#Id
private String id;
private String name;
-------------
}
I created a simple method for specification Query is that correct
private Specification<Person> containsText(String keyword) {
return (root,query, builder) -> {
String finalText = keyword.toLowerCase();
if (!finalText.contains("%")) {
finalText = "%" + finalText + "%";
}
Predicate genreExp = builder.like(builder.lower(root.get("basicMetadata").get("tags")), finalText);
return builder.or(genreExp);
};
you can write your specification like this
public class PersonSpecifications {
public static Specification<Person> hasTag(String keyword) {
return (root, query, builder) -> {
String finalText = keyword.toLowerCase();
if (!finalText.contains("%")) {
finalText = "%" + finalText + "%";
}
Join<Person, BasicMetaData> md = root.join("basicMetaData");
return builder.like(builder.lower(md.join("tags").get("name")), finalText);
}
}
}
and you can use this specification to get the filtered results like this
repository.findAll(PersonSpecifications. hasTag("abc"),PageRequest,of(0,10));

spring data native query interesting bug with Lob column

I have an entity:
#Entity public class KnowledgeBase {
private Long id;
private String link;
private String content;
#Id
#SequenceGenerator(name = "knowledgebase_id_generator", sequenceName = "knowledgebase_id_sequence", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "knowledgebase_id_generator")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
And I have a spring data repository
#Repository public interface KnowledgeBaseRepository
extends AbstractRepository<KnowledgeBase, Long> {
#Query(value = "SELECT c.id as id,c.link as link, c.content as content"
+ " from knowledgebase c where content=?1", nativeQuery = true)
List<KnowledgeBase> findRelevantRecords(String searchString);
}
Please note that
where content=?1
is just a sample, where clause was different for testing.
The issue is if I run this repository method, everything just fine, but content column contains large text amount, I want it to be lazy loaded. If I do that I get error that value is wrong for Long: ''. So my entity is:
#Lob #Basic(fetch = LAZY) String content;
If I remove this, everything just fine.
How to prevent content column from being loaded every time and have spring data repository search properly?
try this:
Create a constructor in your entity which accepts only the required fields
public class KnowledgeBase{
//default constructor
public KnowledgeBase(){}
public KnowledgeBase(Long id,String link){
this.id=id;
this.link=link;
}
}
and use this constructor signature in your query in your repository
#Query(value = "SELECT new #{#entityName} (c.id as id,c.link as link) from #{#entityName} c "
+ " from knowledgebase c where content=?1", nativeQuery = true)
List<KnowledgeBase> findRelevantRecordsWithoutContent(String searchString);

Mapping hibernate entity with Jackson annotation

I'm working with Spring, hibernate and MySql but I have some problem with seralization of query result.
First in my entity I added #JsonManagedReference on Set structure (#OneToMany side) and #JsonBackReference on single object reference (#ManyToOne side) and it works but I wasn't be able to retrieve all needed information (for example #ManyToOne reference).
So i swapping #JsonBackReference on set structure and #JsonManagedReference on single object but I retrieve
No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.model.tablesField.TableUI["data"]->java.util.ArrayList[0]->com.domain.Car["carType"]->com.domain.CarType_$$_jvst744_f["handler"])
I tried also with #JsonIgnore on Set structure but it doesn't work for the same issues.
This is my spring configuration
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
// properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
properties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
properties.put("hibernate.enable_lazy_load_no_trans",true);
return properties;
and this is part of one of my several entities:
/**
* Car generated by hbm2java
*/
#Entity
#Table(name = "car", catalog = "ATS")
public class Car implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer idCar;
#JsonManagedReference
private CarType carType;
#JsonManagedReference
private Fleet fleet;
private String id;
private int initialKm;
private String carChassis;
private String note;
#JsonBackReference
private Set<Acquisition> acquisitions = new HashSet<Acquisition>(0);
public Car() {
}
public Car(CarType carType, Fleet fleet, int initialKm, String carChassis) {
this.carType = carType;
this.fleet = fleet;
this.initialKm = initialKm;
this.carChassis = carChassis;
}
public Car(CarType carType, Fleet fleet, String id, int initialKm, String carChassis, String note,
Set<Acquisition> acquisitions) {
this.carType = carType;
this.fleet = fleet;
this.id = id;
this.initialKm = initialKm;
this.carChassis = carChassis;
this.note = note;
this.acquisitions = acquisitions;
}
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "id_car", unique = true, nullable = false)
public Integer getIdCar() {
return this.idCar;
}
public void setIdCar(Integer idCar) {
this.idCar = idCar;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "id_carType", nullable = false)
public CarType getCarType() {
return this.carType;
}
public void setCarType(CarType carType) {
this.carType = carType;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "id_fleet", nullable = false)
public Fleet getFleet() {
return this.fleet;
}
public void setFleet(Fleet fleet) {
this.fleet = fleet;
}
#Column(name = "id", length = 5)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
#Column(name = "initialKm", nullable = false)
public int getInitialKm() {
return this.initialKm;
}
public void setInitialKm(int initialKm) {
this.initialKm = initialKm;
}
#Column(name = "carChassis", nullable = false, length = 20)
public String getCarChassis() {
return this.carChassis;
}
public void setCarChassis(String carChassis) {
this.carChassis = carChassis;
}
#Column(name = "note", length = 100)
public String getNote() {
return this.note;
}
public void setNote(String note) {
this.note = note;
}
#OneToMany(fetch = FetchType.LAZY, mappedBy = "car")
public Set<Acquisition> getAcquisitions() {
return this.acquisitions;
}
public void setAcquisitions(Set<Acquisition> acquisitions) {
this.acquisitions = acquisitions;
}
}
one method that uses the query:
#Override
#RequestMapping(value = { "/cars/{idFleet}"}, method = RequestMethod.GET)
public #ResponseBody TableUI getCars(#PathVariable int idFleet) {
TableUI ajaxCall=new TableUI();
try {
ajaxCall.setData(fleetAndCarService.findCarsByIdFleet(idFleet));
return ajaxCall;
} catch (QueryException e) {
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in FleetAndCarControllerImpl::addCar :" + errorResponse.getStacktrace());
return ajaxCall;
}
}
two class for the query:
public interface DefRdiRepository extends JpaRepository<DefRdi, Integer>{
//#Query("SELECT CASE WHEN COUNT(c) > 0 THEN true ELSE false END FROM DefRdi c WHERE c.parName = ?1 AND c.description= ?2")
//Boolean existsByParNameAndDescription(String parName, String description);
//Query method of spring, I put findBy and then the key of research
DefRdi findByParNameAndDescription(String parName, String description);
}
public interface CarRepository extends JpaRepository<Car, Integer>, CarRepositoryCustom {
//Query method of spring, I put findBy and then the key of research
List<Car> findByFleetIdFleet(int idFleet);
}
Where is my error? I don't want Set object but only the single reference. The problem is only when I serialize. Thanks
UPDATE:
I use #JSonIgnore on all set collectionts and Eager instead lazy ad all works fine, but is there a way to retrieve all the information only when I want, for example having two different query?
So it doesn't work
#Override
#Transactional
public List<Car> findByFleetIdFleet(int idFleet) {
List<Car> carList= carRepository.findByFleetIdFleet(idFleet);
for (Car car:carList){
Hibernate.initialize(car.getCarType());
Hibernate.initialize(car.getFleet());
}
return carList;
// return carRepository.findByFleetIdFleet(idFleet);
}
All collections need to be fetched eagerly when loading them from data base, in order to get serialized by Spring. Make sure you fetch them eagerly (e.g. FetchMode.JOIN). You could also swap #JsonManagedReference from wanted fields with #JsonIgnore to black listed fields, Spring automatically serialises every field without annotation.
Update:
Changing the data repository to something like that should work, I am not sure it compiles, but I think you will get the point:
#EntityGraph(value = "some.entity.graph", type = EntityGraph.EntityGraphType.FETCH)
#Query(
value = "SELECT c FROM Car c INNER JOIN FETCH c.acquisitions WHERE c.id = :idFleet"
)
public interface CarRepository extends JpaRepository<Car, Integer>, CarRepositoryCustom {
//Query method of spring, I put findBy and then the key of research
List<Car> findByFleetIdFleet(int idFleet);
}
For more information look at this post and read the official documentation.
Workaround:
There seems to be a workaround, however fetching those collections eager like shown above should have a positive performance impact, since there is no need for loading proxies afterwards. Also no open transactions are needed at controller level.

Categories

Resources