I am getting Weird output in console when fetching Data from Database - spring-data-jpa

I am trying to fetch some data ,I am using H2 data base. The model is
#Entity
public class Employee {
#Id
private long empid;
private String empname;
private Date empdoj;
private double emptotalsalary;
private String emptype;
The service class is Where I used the methods
#Autowired
EmployeeRepository repo;
public int countAllEmployees() {
return (int)repo.count();
}
public List<Employee> getPermanentEmployees(String employeetype){
Employee filter = new Employee();
filter.setEmptype(employeetype);
Example<Employee> example = Example.of(filter);
List<Employee> emplist = (List<Employee>)repo.findAll();
return emplist;
}
public List<Employee> getEmployeeNames(){
Employee filter = new Employee();
filter.setEmpname("tha");
ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(StringMatcher.CONTAINING).withIgnoreCase();
Example<Employee> example = Example.of(filter,matcher);
return (List<Employee>)repo.findAll();
}
In Console I am getting this output
Employee Count
5
Get List of permanent Employees
[com.example.demo.Employee#416c1b0, com.example.demo.Employee#a302f30, com.example.demo.Employee#4af44f2a, com.example.demo.Employee#47994b51, com.example.demo.Employee#143fe09c]
Get List of all Employees Ending with Tha
com.example.demo.Employee#416c1b0
com.example.demo.Employee#a302f30
com.example.demo.Employee#4af44f2a
com.example.demo.Employee#47994b51
com.example.demo.Employee#143fe09c
I dont understand why this is coming.please help.

I suppose by weird you mean the text like com.example.demo.Employee#143fe09c
Somewhere in your code, you are printing the Employee records. eg: System.out.println(employee) System.out.println uses the object's toString() method to print the object. Since you did not provide it in your Employee class it uses the toString method from superclass of Employee which is Object.
As mentioned in the doc, it prints the classname followed by # and the hash.
https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--
Returns a string representation of the object. In general, the
toString method returns a string that "textually represents" this
object. The result should be a concise but informative representation
that is easy for a person to read. It is recommended that all
subclasses override this method.
The toString method for class Object returns a string consisting of
the name of the class of which the object is an instance, the at-sign
character `#', and the unsigned hexadecimal representation of the hash
code of the object. In other words, this method returns a string equal
to the value of:
getClass().getName() + '#' + Integer.toHexString(hashCode())
Just override the toString method in Employee class to generate text representation of the Employee object. Your IDE already provides a nice way to generate this method.
#Override
public String toString() {
return "Employee{" + "empid=" + empid +
", empname=" + empname + "'" +
'}';
}

Related

HQL - Check if an Array contains a value

I have an array field in my first entity Class as below:
class Entity1{
private Integer col1;
private String col2;
private Integer[] col3Arr;
}
I have another entity class as below:
class Entity2{
private Integer col1;
private String col2;
private Integer col3;
}
I am fetching records by joining multiple other entities along with which I have to join Entity1 if col3Arr contains a value col3 from Entity 2
With PSQL, I could easily achieve this by following statement
//Other part of query
join Entity2 e2 on (//conditions from other joined tables//)
join Entity1 e1 on e2.col3=ANY(e1.col3Arr)
What is the HQL equivalent of ANY?
Or is there any other way in HQL to check if an array contains a specific value?
For mapping the arrays you will need a custom type. You can use the hibernate-types project for this: https://vladmihalcea.com/how-to-map-java-and-sql-arrays-with-jpa-and-hibernate/
Did you try to use e2.col3 = FUNCTION('ANY', e1.col3Arr) yet? If that doesn't work, I would suggest you create a custom SQLFunction that renders the SQL you desire e.g.
public class ArrayAny implements SQLFunction {
#Override
public boolean hasArguments() {
return true;
}
#Override
public boolean hasParenthesesIfNoArguments() {
return true;
}
#Override
public Type getReturnType(Type firstArgumentType, Mapping mapping) throws QueryException {
return firstArgumentType;
}
#Override
public String render(Type firstArgumentType, List args, SessionFactoryImplementor factory) throws QueryException {
return "any(" + args.get(0) + ")";
}
}
You will have to register the function within the Dialect.
According to the hibernate documentation:
When discussing arrays, it is important to understand the distinction between SQL array types and Java arrays that are mapped as part of the application’s domain model.
Not all databases implement the SQL-99 ARRAY type and, for this reason, Hibernate doesn’t support native database array types.
So, there is no equivalent of PSQL ANY in HQL.

JPA Query method not returning anything

I currently have it working with a custom n1ql query, however it's such a simple query, I figured I could just use the built in jpa query method, however I can't figure out the key words, because I'm not getting anything back.
This code works:
#Query("SELECT meta().id as _ID, meta().cas as _CAS, * FROM `my-bucket` mb " +
"WHERE mb.name like $1 OR ANY Parent " +
"IN mb.Parents SATISFIES Parent.name like $1 END")
List<MyObject> searchObjectByName(String name);
This however doesn't work
#N1qlPrimaryIndexed
public interface MyObjectRepository extends CouchbasePagingAndSortingRepository<MyObject, String> {
List<MyObject> findBySecondObjectNameContains(String name);
}
#Data
#Document
public class MyObject{
#Id
private String objectId;
#Field
private SecondObject secondObject;
}
#Data
public class SecondObject {
#Field
private String name;
}
My test method:
#Autowired
private MyObjectRepository myObjectRepository;
#Test
public void testFind() {
List<MyObject> myObjects = myObjectRepository.findBySecondObjectNameContains("my name");
Assert.assertNotNull(myObjects);
}
The query looks correct, a few things that might be missing:
1) In your test, are you sure that "my name" isn't supposed to be "my name%"?
2) Check if you have a primary or secondary index that covers this query (run the same query via web console)
3) When did you insert the data? If you haven't configured couchbase to be strong consistent, you might have been reading an old version of your data

Morphia (MongoDB) Datastore "get" returns null

So I started working with Morphia and I'm encountering a weird problem.
Here's my entity class
#Entity("movies")
#Indexes(#Index(value = "Name", fields = #Field("Name")))
#Converters(LocalDateConverter.class)
public class MovieDetails implements Serializable
{
#Id
public String Id;
public String Name;
public String Description;
public String ImageName;
public LocalDate ReleaseDate;
public String Director;
public int Duration;
public String Genres;
public String Actors;
public MovieDetails()
{
}
public MovieDetails(String id, String name, String description, String imageName, String director, String actors, LocalDate releaseDate, String genres, int duration)
{
this (name, description, imageName, director, actors, releaseDate, genres, duration);
Id = id;
}
public MovieDetails(String name, String description, String imageName, String director, String actors, LocalDate releaseDate, String genres, int duration)
{
Name = name;
Description = description;
ImageName = imageName;
Director = director;
Actors = actors;
ReleaseDate = releaseDate;
Genres = genres;
Duration = duration;
}
}
Here's my little test:
final Morphia morphia = new Morphia();
// tell Morphia where to find your classes
// can be called multiple times with different packages or classes
morphia.mapPackage("nimrodpasha.cinema.objects");
// create the Datastore connecting to the default port on the local host
final Datastore datastore =
morphia.createDatastore(SingleMongoClient.getInstance().getClient(),
Constants.DB.TICKET_DATABASE);
datastore.ensureIndexes();
//region new movie
MovieDetails movie = new MovieDetails("The Mask", "Stanley Ipkiss (Jim Carrey) is a bank clerk that is an incredibly nice man. Unfortunately," +
" he is too nice for his own good and is a pushover when it comes to confrontations. After one of the worst days of his life, he finds a mask that depicts Loki, " +
"the Norse night god of mischief. Now, when he puts it on, he becomes his inner, self: a cartoon romantic wild man. However, a small time crime boss, Dorian Tyrel (Peter Greene), " +
"comes across this character dubbed The Mask by the media. After Ipkiss's alter ego indirectly kills his friend in crime," +
" Tyrel now wants this green-faced goon destroyed.",
"MASK.jpg", "Chuck Russell", "Jim Carrey as Stanley Ipkiss/The Mask,Cameron Diaz as Tina Carlyle,Amy Yasbeck as Peggy Brandt,Joely Fisher as Maggie", new LocalDate(1994, 2, 1), "Action,Comedy,CrimeAction,Family,Fantasy", 88);
//endregion
// Clearing the db first
datastore.delete(datastore.createQuery(MovieDetails.class));
// Saving a new entity and getting the result saved id
String id = (String) datastore.save(movie).getId();
// This returns as null
MovieDetails movieRetrieved = datastore.get(MovieDetails.class, id);
// This returns with one item
List<MovieDetails> allMovies = datastore.createQuery(MovieDetails.class).asList();
When I use
datastore.get(MovieDetails.class, id)
I get null
When I use:
datastore.createQuery(MovieDetails.class).asList();
I do see my movie in the DB, with the Id used in the get function.
Tried the id in many variations... toString(), ObjectId(id), Key (The value returned from the save result).
The Id in the DB (viewed with Mongo Explorer) does show as something which isn't string (blue colored), suspicious:
Mongo Explorer item picture
Any ideas?
Edit:
* the Id is indeed a string, the cast works and it was verified using watch + instanceof
Edit 2:
* Somehow the cast from ObjectId to String passed and the Id wasnt really a String.
I would change your I'd field from String to a BSON ObjectId field which MongoDB will automagically assign on save. If you then do your get call with the ObjectId as the parameter, it should work. Using ObjectId as your ID field is highly recommended with Mongo.
I am guessing that Morphia is trying to marshall an ObjectId into your String Id field and there is a small bug somewhere. I would try calling datastore.get(Example.class, new ObjectId(id)).
After Nic Cottrell (Thanks!) answer I've had a new perspective of the problem.
As my #Id field was not assigned by me it was automaticaly assigned in the DB as ObjectId.
As I still want to use String I've simply assigned the #Id field on object creation.
Id = ObjectId.get().toString();
found the solution on:
MongoDB / Morphia saves technical id as ObjectId although it's a String in Java

Disadvantages of interface objected programming

class Person{
private String name;
private int age;
private String gender;
//......
}
class Student extends Person{
private String id;
private String schoolBelongTo;
//......
}
public void showInfoOf(Person person){
System.out.println(person.getName());
//......
}
When using function "showInfoOf" ,if an object of Peron is used as the param,OK.However,if it is the type Student,I cannot get access to the field id and schoolBelongTo.
So I am confused ,how to ?
Actually, I want to know is this one of its(Interface oriented programming's or Supper class oriented programming's) disadvantages???
Two possible solutions:
You can programatically check the type in showInfoOf (Person), and use a cast to access & print the desired fields; or,
You can define a method on Person which will print/provide the desired info -- and either replace showPersonInfo() with that entirely, or call it into it. This is the more OO way.
Example:
abstract class Person {
private String name;
private int age;
private String gender;
public void printInfo() {
System.out.println( name);
}
}
class Student extends Person{
private String id;
private String schoolBelongTo;
#Override
public void printInfo() {
super.printInfo();
System.out.println( id);
System.out.println( schoolBelongTo);
}
}
public void showInfoOf (Person person){
person.printInfo();
}
In this example, all functionality has moved to Person.printInfo() and there is no real functionality remaining in showInfoOf (Person).
However in the real-world, you'd probably want move versatility in a Person.provideInfo() function -- perhaps returning a LinkedHashMap of fields & values (since unlabelled values on their own, are not great design).
The showInfoOf (Person) function could then handle formatting & printing the values to the specific requirement, leaving the Person.provideInfo() function general & multi-purpose.
in showInfoOf() you would have to check that person is of type Student, then cast it as a Student to get id or schoolBelongsTo

javaee 6 rest api named query result

I have a simple JEE6 rest class that gets the data from db2. I am using Jackson in ApplicationConfig class to convert the entity objects to json. It converts with the field names as the key and the value as the right hand value. So for example:
Class Entity {
String name;
String address;
}
converts to
{name:"hello", address:"world"}
The service is as follows:
public List<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
Now I want to only return the name in json format. So I created a named query as follows in the entity class:
#NamedQuery(name = "justGetName", query = "SELECT a.name FROM Applications a")
And the service changed to
public List<T> findAll() {
return getEntityManager().createNamedQuery("justGetName").getResultList();
}
This returns the following array:
[{"first","second","third"}]
But I want to get back:
[{name:"first",name:"second",name:"third"}]
How do I write the named query so that the class field names are added to the json structure? Thank you.
You querying a list of strings from your database and this is what the service returns.
Their are multiple ways to achieve your goal.
Pure JPA
Using #JsonIgnore to tell Jackson not to serialize an attribute
class Application {
String name;
#JsonIgnore
String address;
}
Create a new Entity class that only contains the attributes you would like to share
class ApplicationName {
String name;
}
Alternatively you could introduce a separate class that only contains the attributes you would like to share and convert the results from the query into this class and return than the list of this converted values.