I'm using elasticsearch for indexing in my application. I'm trying to store a document which in turn contains a field of a custom data structure:
#Document(indexName="sampleIndex", type="user")
class User {
#Id
private String id;
#Field(type = FieldType.Nested)
private Person person;
}
class Person {
String firstName;
String lastName;
}
I'm usingElasticsearchRepository to save this entity. There are chances that lastName or firstName of the Person may be null. In this case I get a NullPointerException on calling repository.save(entity). Is there a way to suppress this? Because I still want to save this on elasticsearch irrespective of whether some of the fields are null or not.
Related
I am trying to develop a REST API using Java and Spring boot. I have an entity class User.
public class User implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
#Column(name = "mobile_number")
private String mobileNumber;
#Column(name = "first_name")
private String firstName;
#Column(name = "last_name")
private String lastName;
#Column(name = "email")
private String email;
#Column(name = "created_date")
private Date createdDate;
}
My requirement is to select some specific fields from this entity. These fields can be specified
dynamically in REST API url like
http://localhost:8080/getUserDetails?start=2021-01-01&end=2021-05-05&fields=firstName
http://localhost:8080/getUserDetails?start=2021-01-01&end=2021-05-05&fields=firstName,lastName
When fields=firstName , I want to return only firstName in JSON.
Similary, firstName and lastName in JSON when fields = firstName,lastName.
If i use criteria query query.select(root.get("firstName"),root.get("lastName"))
than i have to create constructors in Entity / POJO class.
There can only be one constructor with 2 String parameters. So this approach will fail when i need to select mobileNumber and email.
If i use spring Projections than i am not able to get how to select dynamic fields in Projections without adding projections = "some value" in rest endpoint url. Also if i use interface or class projections i will have to define required fields there. Fields should be dynamic in my case.
Also i am trying to avoid fetching of all columns and than filtering it out based on requested fields as it is inefficient.
So can anyone please help me out?
I am trying to store below pojo in couchbase with spring-data, but persist json is storing "user field of type User" as null.
#JsonInclude(value = Include.NON_EMPTY)
#Document
public class ProjectXYZ {
#Id
#GeneratedValue(strategy = GenerationStrategy.UNIQUE)
private String id;
#Field
private String prjname;
#Field
private User user;
//setter and getter
}
Update:
User Pojo
#JsonInclude(value = Include.NON_EMPTY)
#Document
public class User {
#Id
#Field
private String id;
#Field
private String email;
#Field
private String firstName;
#Field
private String lastName;
//setter and getter
}
And as below I am saving it, All works fine and as expected but User object get stored as null.
ProjectXYZ project = new ProjectXYZ();
project.setUser(getUser(request));
project = projectXYZService.createProject(project);
References are not directly supported through Spring data couchbase as it needs to store meta information about the reference document id.
However there is support for fetching associated entities through N1QL ANSI Joins available in current Lovelace (3.1.x) versions. Here is the documentation for it.
I'm new with Spring development and I'm trying to understand how the relation annotations work. I have two entity, User and Country. User has a #ManyToOne relation with Country.
User Entity
#Entity
#Table(name = "user")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "user_id")
private int id;
#Column(name = "name")
private String name;
#ManyToOne
#JoinColumn(name = "country", foreignKey = #ForeignKey(name = "FK_country"), referencedColumnName = "name")
private Country country;
//Other columns, Constructor, Getter, Setter omitted
}
Country Entity
#Entity
public class Country {
#Id
#Column(nullable=false)
private String name;
#Column(nullable=false)
private String continent;
//Other columns, Constructor, Getter, Setter omitted
}
It creates everything as I want in the database but my problem is when I'm trying to save the data from a form.
My form is taking as input a name and a selection between all the possible countries (previously loaded with a query). Once the name is provided and the country is selected, a row with name and a the id of the country should be written in my MySQL database.
I'm getting the following error:
java.lang.IllegalArgumentException: Parameter value [Spain] did not match expected type [com.model.Country(n/a)]
This is because it requires a Country type but I truly need to add a row with the name and the id associated to Spain. I don't need to create the whole Country object. I don't know what to do and if I'm doing correctly.
Can someone help me out, please?
Thanks in advance.
You need to save the Country class object instead of just country's name, As i could see that Country class is mapped with your User class.
For that fetch the Country Class object from the name/id of the country which you're receiving from your HTML and then save it along with your User details.
Something like this :-
User user = getUserById(user_id); //get user's details or create a new User's Object
Country country = getCountryById(country_id); //get Country's object using id or name you're receiving from your HTML-form
user.setCountry(country); //set Country's object in your User's object
//save user's object
Perhaps you can to try using custom Property Editor or Converter? It will convert string to your Country object.
Spring Custom Property Editor
I have created custom annotations that I add to my Java class fields.
When I create object of that class, I want my custom annotated fields to have value: null or "" or "customAnnotation"
For example:
#Entity
public class User implements Serializable{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "address_id")
private Long id;
#MyCustomAnnotation
private String firstname;
private String lastname;
....
And when I create the object somewhere in the project:
User user = new User();
user.setFirstname("John");
user.setLastname("Smith");
I want it to be:
String firstname = user.getFirstname() // firstname is "" or null
String lastname = user.getLastname() // lastname is "Smith"
How to create method that will find all annotated fields in all classes in the project and do the same for all of them? Where to store this method?
I am working on the Maven, Spring Boot project.
At some point in your code you will have to implement something like this:
Class<User> obj = User.class;
if (obj.isAnnotationPresent(MyCustomAnnotation.class)) {
//your business logic
}
Sensible places for something like this would be either immediately when creating users, when getting users (for whatever reason the annotation is being used) or at an interval in a timed routine.
edit: See this tutorial: https://www.mkyong.com/java/java-custom-annotations-example/
How to implement relationship between 2 classes using ORMLite? I know about foreign field, but i can't add non-string Department field to product class
Class Product
#DatabaseTable(tableName = "PRODUCTS")
public class Product {
#DatabaseField(id = true)
private String id;
#DatabaseField()
private String name;
#DatabaseField() //This field is id of Department class
private String department;
Department class
#DatabaseTable(tableName = "DEPARTMENTS")
public class Department {
#DatabaseField(id = true)
private String id;
#DatabaseField()
private String name;
How to implement relationship between 2 classes using ORMLite? I know about foreign field, but i can't add non-string Department field to product class
RTFM please. Did you look at any of the documentation or examples? Here's the docs on foreign objects. You can see plainly that you put a Department into Product not a String. The docs show the Order object having an Account field.
#DatabaseField
private Department department;
Behind the scenes, what ORMLite does is actually store just the id from the department into your Product. If you actually look at the schema then you will see a string there but you don't do that yourself.
Then when you retrieve your Product, it will have a Department field but only the id will be filled out.
There is also a foreign object example in the code that may also help.