Spring Data for Couchbase - Metadata Information - spring-boot

I am new using Spring Data for Couchbase, I defined this object
public class Building {
#NotNull
#Id
private String id;
#NotNull
#Field
private String name;
#NotNull
#Field
private String companyId;
}
but I am not sure if the id defined in the object will be the same of the Couchbase Metadata Information and how to define the format of it
.cas
.expiration
.flags
.id
.type

You don't need to worry about the meta attributes, they are automatically created by couchbase. Just the #Id is mandatory.

Related

JPA Entity class without "#Column" annotation

I have this piece of code where the author creates a JPA Entity. Somehow, it's successfully working and that seems weird to me since not every field is annotated with "#Column". Based on that, here's my question:
How it is possible for this class to work properly (all data is being successfully recorded in the database) without every field not having a "Column" annotation (excepting "id")?
/**
* #author Ram Alapure
* #since 05-04-2017
*/
#Entity
#Table(name="User")
public class User {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name = "id", updatable = false, nullable = false)
private long id;
private String firstName;
private String lastName;
private LocalDate dob;
private String gender;
private String role;
private String email;
private String password;
#Override
public String toString() {
return "User [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", dob=" + dob + ", email="
+ email + "]";
}
}
For instance, here's another class which is working properly as well. But this time, with a "#Column" annotation in every field. I thought it was a pre-requisite to JPA create a column from the field.
#Entity
#Table(name = "address")
public class Address {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "address_id")
private long id;
#Column(name = "rua")
private String street;
#Column(name = "number")
private int number;
#Column(name = "complement")
private String complement;
#Column(name = "suburb")
private String suburb;
#Column(name = "city")
private String city;
#Column(name = "state")
private String state;
#Column(name = "country")
private String country;
#Column(name = "cep")
Getters and Setters were hidden from all pieces of code since they just look like regular getters and setters, not being necessary to be shown.
If you don't specify #Column annotation (Optional) then Hibernate uses default naming stretegy by using camel case.
firstName field becomes first_name column in Database.
You can also define your own naming stretegy according to your needs.
From the documentation.
strategy ; Hibernate 5 defines a Physical and Implicit naming strategies. Spring Boot configures SpringPhysicalNamingStrategy by default. This implementation provides the same table structure as Hibernate 4: all dots are replaced by underscores and camel cases are replaced by underscores as well.
this annotation means create table in database #Entity with all field in class same the name
the spring boot scan this annotation #Entity after scan
is inject and create Bean in IOC with fields
but if used #Column this help for mapping with Table field if change colume name in dataBase
and spring boot when scan component and see this annotation the bean is change with Colume name to name in database becouse we need success mapping with data base

join table and get data based on field value JPA

I have two entities
#Entity
public class Module {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String descr;
#OneToMany(cascade = {CascadeType.ALL})
#JoinColumn(name="ID", referencedColumnName="ID")
private List<Permissions> perms;
}
#Entity
public class Permissions {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private Integer clientId;
private String role;
private Character endorseCreate;
private Character endorseUpdate;
private Character endorseDelete;
private Character endorseView;
I am trying to fetch data by first Module.name and Permissions.clientId and Permissions.role
Something like in sql
select * from Module m, Permissions p where m.id=p.id and p.clientId=1 and p.role='ADMIN'
How can I achieve using JPA, I am using spring data aswell.
Can a method be declared in CRUD Repo provided by spring data?
I think the issue is I am unable to find way to pass values to fetch data.
Any help is greatly appreciated

Couchbase spring data jpa to generate composite primary key

Using Couchbase with Spring Data and JPA,
In my entity class How to create a Composite Key using two fields.
currently in Person class id is the primary key #Id
where I want combination of id and name will be primary key.
#Document
public class Person {
#Id
private String id;
#Field
private String name;
#Field
private String city;
I think you should be able to achieve what you want using the following -
#Document
public class Person {
#Id
#GeneratedValue()
String key;
#IdPrefix
String id;
#Field
#IdAttribute
String name;
#Field
private String city;
}
Use #IdSuffix if you want the value in id to be after the name . When constructing the above object don't assign a value to key variable yourself as the value doesn't get autogenerated if you provide one of your own as per docs - https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.autokeygeneration.configuration .

Spring + hibernate one to one mapping

I am developing web application using spring and hibernate. I am using one to one mapping between two tables employee and PersonelDetails.
below are my bean classes
=======================Employee=====================================
#Entity
#Table(name="employee")
public class Employee {
#Id
#Column
#GeneratedValue
private int empid;
#Column
private String firstName;
#Column
private String lastName;
#Column
private String email;
#Column
private String password;
#Column
private boolean isAdmin;
#Column
private boolean isActive;
#Column
private boolean isLocked;
//getter setters
====================PersonalDetails class====================
#Entity
#Table(name="PersonalDetails")
public class PersonalDetails {
#Column
#Id
private int empid;
#Column
private String personalEmail;
#Column
private String mob;
#Column
private String permenantAdress;
#Column
private String currentAddress;
#Column
private String gender;
#Column
private String maritialStatus;
#MapsId
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "empid", referencedColumnName = "empid")
#ForeignKey(name="empid")
private Employee employee;
//getter setters
In my application table employee is filled by Admin user while creating new employee after that employyee himself fill personalDetails table by login to his accountCreated by Admin)
Now when I try to send personal details bean to hibernate layer first I have to get the employee bean from employee table then call setEmployee method over personalDetails class and save employee bean in personalDetails and send to hibernate layer for saving in database.
So while getting employee bean from database and again send back through personalDetails bean leads to a performance issue.
Can anyone help here to clarify while saving data in child table(PersonalDetails) is it really mandatory to pass parent object(Employee) ?
=======================code to store personalDetails===============
#RequestMapping(value="addpersonal")
public ModelAndView addPersonalDetails(#ModelAttribute("personalDetails") PersonalDetails personalDetails) {
//personalDetails.setEmpid(1);
personalDetails.setCurrentAddress("niljyoti");
personalDetails.setMob("9405715872");
personalDetails.setPermenantAdress("address");
Employee e = empService.getEmployeebyUserName(uname);
personalDetails.setEmployee(e);
personalDetailsService.addPersonalDetails(personalDetails);
return new ModelAndView("home");
}
On read:
You can change fetch strategy if you are worried.
Based od JPA spec, default fetch type for #OneToOne is EAGER.
By setting fetch = FetchType.LAZY, instead of real PersonalDetails object, an object of a subclass behaving as a proxy is returned. Hence, selecting from employee table starts only after getEmployee is called.
On write:
You need to specify connection between entities, in your model, the only way is the employee field. However, you can specify mappedBy, see answer to this question:
Java: Hibernate #OneToOne mapping

Spring MVC + Hibernate one to one mapping

I am trying to develop an application using spring mvc and hibernate.
I have following entity classes
Employee
EmployeePeronellDetails
EmployeeSallaryDetails
EmployeeProfesionalDetails
I have an integer employeeID as a primary key in employee table which further acts as foreign key in other three tables
I have created 4 different forms to fill information to above tables.
4 forms opened one after the other
How my entity classes should look like?
I followed few tutorials they suggested to create Employee class object in other classes.
So after successfully inserting data in employee table. Do I need to save respective Employee object in session?
After this while creating EmployeePeronellDetails bean pass that session's employee object to EmployeePeronellDetails's setter method/constructor.
Below are my employee and EmployeePeronellDetails classes
#Entity
#Table(name="employee")
public class Employee {
#Id
#Column
#GeneratedValue
private int empid;
#Column
private String firstName;
#Column
private String lastName;
#Column
private String email;
#Column
private String password;
#Column
private boolean isAdmin;
#Column
private boolean isActive;
#Column
private boolean isLocked;
//geter setters
========================PersonalDetails class===========================
#Entity
#Table(name="PersonalDetails")
public class PersonalDetails {
#Column
#Id
private int empId;
#Column
private String personalEmail;
#Column
private String mob;
#Column
private String permenantAdress;
#Column
private String currentAddress;
#Column
private String gender;
#Column
private String maritialStatus;
#OneToOne
#JoinColumn(name="empid")
private Employee employee;
Now when I tried to call sessionFactory.getCurrentSession().saveOrUpdate(personalDetails);
It gives
org.hibernate.TransactionException: nested transactions not supported]
Exception.

Resources