Spring & MongoDB full text search dependency error - java

I'm new to spring framework and I've generated a project with JHipster online using MongoDB as database. However I'm trying to setup text search for my MongoDB collection but when importing TextCriteria my code fails at compile time with the following error:
package org.springframework.data.mongodb.core.query.text does not exist
This is the code for my UserSearchRepository
// package path hidden
import ....User;
import org.springframework.data.mongodb.core.query.text.TextCriteria;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
/**
* Spring Data MongoDB repository for the User entity.
*/
#Repository
public interface UserSearchRepository extends MongoRepository<User, String> {
List<User> search(TextCriteria textCriteria);
}
And this is the code for my search controller
#GetMapping("/_search/users/{query}")
public List<User> search(#PathVariable String query) {
TextCriteria textCriteria = TextCriteria.forDefaultLanguage();
textCriteria.matching(query);
return userSearchRepository.search(textCriteria);
}

Related

findAll() method doesn't work when forceQuote is true in spring-data-cassandra

I'm using spring-data-cassandra in my spring boot project.
I use (forceQuote = true) because I have tables with uppercase letters.
When I tried to execute findById() method, it worked without any problem, but findAll() method threw an exception. I tried to debug the application and I found that spring-data-cassandra adds extra quotes to the table name while executing the query. So, the table name becomes ""MyTable"".
The exception is given string ["MyTable"] is not a valid quoted or unquoted identifier.
Is that a bug in spring-data-cassandra?
import lombok.Data;
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;
#Table(value = "MyTable", forceQuote = true)
#Data
public class MyTable {
#PrimaryKey
private Integer key;
//some attributes.
}
import org.springframework.data.cassandra.repository.CassandraRepository;
public interface MyTableRepository extends CassandraRepository<MyTable, Integer> {
}

Spring boot static where clause with JPA repositories

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 query method but it doesn't work and gave me an excpetion
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")

Inject query ID automatically with mybatis

I want to inject query ID as a comment in SQL statement automatically to explain heavy queries.
e.g.
If there is a mapper class like this:
package com.example.mapper;
import com.example.domain.Blog;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface BlogMapper {
#Select("SELECT * FROM blog WHERE id=#{id}")
List<Blog> findById(String id);
}
I want to send a query like
/* BlogMapper.findById */SELECT * FROM blog WHERE id=5

How Do I get the DataBase Data using Hibernate

Here's my code:
import model.Usuario;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
#SuppressWarnings("deprecation")
public class HibernateMain {
public static void main(String[] args) {
Configuration configuration=new Configuration();
configuration.configure();
#SuppressWarnings("deprecation")
ServiceRegistry sr= new ServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sf=configuration.buildSessionFactory(sr);
Usuario user1=new Usuario();
user1.setNombreUsuario("nombreUsuario");
user1.setClaveUsuario("claveUsuario");
user1.setId(5);
user1.setLoginUsuario("loginUsuario");
user1.setTipoUsuario("A");
user1.setEstadoUsuario("A");
Session ss=sf.openSession();
ss.beginTransaction();
//saving objects to session
ss.getTransaction().commit();
ss.close();
}
}
I want to verify if user1 is in my DB, how do I do it?
I've already made the hibernate.cfg.xml, and the class Usuario is an entity of one of my tables in my DB(I'm using Postgres and Eclipse IDE btw)
Thanks
There are many was to retrieve data from database, its all depends upon the situation we need to use
If you want to retrieve the data's alone from the datatabase you can simply use
get(), load() method which retrieve the data from database and store it in the pojo classes , so whenever you need you can use it.
Eg.
Parent parent = (Parent)session.load(Parent.class, serializableId);\\serializableId will the reference key
More..
If you want to retriev the data based on some defined conditions you can use HQL query based retrieval concept
Eg.
Query query = session.createQuery("from Parent as a where a.fName='Raj'");
More..
If you want to retrieve a data from data , that continos based on dynamic means you can go ahead with HibernateCriteria
Criteria criteria = session.createCriteria(User.class);
More..
See also..
Precedure to retrieve data from database

Play Framework 2.1.0 - Using Ebean to query existing database tables

I am having trouble with returning data from a existing database table (SQL Server 2008 R2). I can successfully authenticate and connect to the database, I have a model created that I attempted to map to a particular table, then as a test tried to return a row count, the row count always returns 0. I think perhaps I do not understand how Play/Ebean database connectivity works. At present I have the below:
Model - Data.java:
package models;
import javax.persistence.*;
import play.db.ebean.*;
#Entity
#Table(name="someTable")
public class Data extends Model {
private static final long serialVersionUID = 1L;
#Id
public int someKey;
public String someCol;
public static Finder<Integer,Data> find = new Finder<Integer,Data>( Integer.class, Data.class );
}
Controller - Index.java
package controllers;
import java.util.*;
import models.Data;
import play.mvc.*;
public class Index extends Controller {
static int rowCount = Data.find.getMaxRows();
public static Result index() {
Result res = ok(rowCount);
return res;
}
}
Application.conf (Relevant snippets):
db.default.driver=net.sourceforge.jtds.jdbc.Driver
db.default.url="jdbc:jtds:sqlserver://server.domain.local/someDB;instance=SQL"
db.default.user=someUser
db.default.password="password"
evolutionplugin=disabled
ebean.default="models.*"
I don't profess to understand how Evolutions work, but I do database development work separately in SQL, i.e. DDL and schema creation/modifying.
Any help would be greatly appreciated.
Thanks
The thing you want to do is done by findRowCount() which returns the number of the rows your query affect. Basically a COUNT(*).
getMaxRows() is the equivalent to setMaxRows(int) which is a limitation for the number of rows returned by your query (e.g. LIMIT in MySQL).

Resources