Nested reference object is persisting as null - java

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.

Related

springboot with MongoDb ,Transient field return null

When i trying to do my RBAC job , I've made a class RolePermission like belows:
#Data
#AllArgsConstructor
#NoArgsConstructor
public class RolePermission extends BaseEntity{
private Long roleId;
private Long permissionId;
#Transient
private List<Permission> permissions;
public RolePermission(Long roleId,Long permissionId){
this.roleId = roleId;
this.permissionId = permissionId;
}
}
class Permission like belows
#AllArgsConstructor
#NoArgsConstructor
#Data
public class Permission extends BaseEntity{
public Permission(Long id, Long parentId, String name){
this.setId(id);
this.parentId = parentId;
this.name = name;
}
private String name;
private Long parentId;
private String url;
private String permission;
private Integer type;
private String icon;
private Integer status;
private Integer ord;
}
Here comes my test :
LookupOperation lookup = Aggregation.lookup("permission", "roleId", "_id", "permissions");
Aggregation aggregation = Aggregation.newAggregation(lookup);
AggregationResults<RolePermission> role_permission = mongoTemplate.aggregate(aggregation, "role_permission", RolePermission.class);
//AggregationResults<Map> role_permission = mongoTemplate.aggregate(aggregation, "role_permission", Map.class);
System.out.println(role_permission.getMappedResults());
//userService.getAllPermissions(u);
When I add #Transient , permissions comes to null
and When I remove #Transient,permissions comes back.
I don't wanna save permissions to MongoDB, so I add #Transient, is there any way i can draw the data back without saving it to the Database. Because I get the permissions data from a relationship collectionRolePermission,not itself.
The #Transient behaves similar to the transient keyword.
transient is used to denote that a field is not to be serialized (and therefore not saved in the database too).
#Transient is used to denote that a field should not persisted in database (but it is still serializable as java object).
Take look this baeldung tutorial for it: https://www.baeldung.com/jpa-transient-ignore-field
There is no way to draw the data back without saving it to the Database because it isn't persisted at all since it is what #Transient is used for.
To get the data back you have to persist it somewhere else, but the best place is in the database. If you don't want to persist it along with the other data, consider saving it in a sperate database. So, you could split user data and authentication/RBAC data.

Spring data: How to ignore field for relational database but not in elasticsearch?

I have a problem with spring data and the #Transient annotation, in fact I have a class as follows:
#Table("example")
#Document(indexName = "example")
public class Example implements Serializable {
#Id
#Column("id")
private Long id;
#Transient
#Field(type = FieldType.Nested, includeInParent = true)
private User user;
#Column("user_id")
private Long userId;
...getter and setter
}
I have a relational database and an elasticsearch.
In the database I just want to save the id and userId (and it works with this code).
but in elasticsearch I want to save the id and the user.
If I remove #Transient I get an error with R2dbc:
Nested entities are not supported in r2dbcEntityTemplate.insert
Does anyone know how to ignore it with R2dbc and take it into account with elasticsearch?

How to select specific columns dynamically in a REST API?

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?

How to send ArrayList or Sets<Integer> as list filtering in Request Body or URL in PUT Request

I'm trying to write a controller that will function as multiple seat reservation.The Integers list is used for filtering.
My Entity looks like this:
#Entity
#Id
private movieId;
private String movieName;
private String cinemaName;
private String cinemaHall;
private Intger seatingPlace;
private boolean booked;
How Can I pass list or sets in request body to access multiple update seatingPlace. Did I modyfing Enity or connect in smthing relation?
Acutally my multipleUpdate API works using JPA Query findByMovieNameAndCinemaNameAndcinemaHall and return me list wchich
I checking isnt Empty and cheking (field boolean booked) if true so ok u can booked them.
And after that i want filter by passing List seatingPlace and change boolen to false.
Based on my understanding of your requirements, a possible solution could be creating another entity (table) MovieSeatReservation and creating a One to Many relationship from your Entity. It could look like this: (You can replace Entity class name with your real entity name)
#Entity
public class Entity {
#Id
#GeneratedValue
private Long movieId;
private String movieName;
private String cinemaName;
private String cinemaHall;
#OneToMany
private List<MovieSeatReservation> reservedSeatsStatus;
// getters and setters
}
#Entity
public class MovieSeatReservation {
#Id
#GeneratedValue
private Long id;
private boolean isReserved;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "movie_id")
private Entity entity;
// getters and setters
}

Ignore field on mapping and indexing using ElasticSearch and java API

I'm developing an application using ElasticSearch, I want to index a document but ignoring some field.
#Document(indexName = "my_index", type = "user")
public class User{
#Id
private Long userId;
private String name;
private Adress adresse; //ignored field
getters and setters
}
Thanks in advance

Categories

Resources