Couchbase spring data jpa to generate composite primary key - spring-boot

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 .

Related

How to add custome/ java annotation in jdl JHipster Domain Language

With this code
entity Person {
id String
name String
post String
}
It will give me like
#Id
private String id;
#Field("name")
private String name;
#Field("post")
private String post;
but i want like
#Id
private String id;
#Field("name")
#Indexed(name = "name",unique = true)
private String name;
#Field("post")
private String post;
And this thing i have to do automatically, while importing jdl it should created automatically.
How can i do that?
I read about custome annotation in jhipster but not getting how should i implement it.
You can do this with the validator of jdl, in your case unique.
like this:
entity Person {
id String,
name String unique,
post String
}

How to Join multple table in R2DBC and Spring WebFlux?

//1. Person Entity
class Person {
private Integer id;
private String name;
Private Address addId;
}
//2. Address Entity
class Address {
private Integer addId;
private String city;
Private String state;
Private String country;
Private Integer zip;
}
How to Join multple table in R2DBC and Spring WebFlux?
Currently R2DBC doesnt support annotation such as #OneToMany. So in Address you shuld have a property called userId of type Integer and pertsist Person and then persist Address. And to get them you should first get the Person and find the Address by the userId

#ManyToOne Referencing a composite key in JPA

I have following entities
#Entity
#IdClass(SubjectId.class)
class Subject {
#Id
String name;
#Id
String volume;
........
}
class SubjectId {
String name;
String volume;
//constructor, getters and setters
..........
}
#Entity
class Student {
#Id
String studentId;
String subject;
String subjectVolume;
}
I want to map the fields subject and subjectVolume of class Student to composite primary key of class Subject as a #ManyToOne relationship. But I don't know what should I pass inside #ManyToOne(?).
Any help would be appreciated, thanks.
Edit:
I want to use the columns subjectName and subjectVolume as entity fields in class Student as well. I don't want to do student.getSubject().getSubjectName() instead I want student.getSubjectName().
You could just declare the relation this way (instead of declaring the fk fields):
class Student{
#Id
String studentId;
#ManyToOne // this is sufficient create foreign-key columns in the Student-table
Subject subject;
}
The generated columns of the Student table will have these names by default:
In case you need different column names you should look for the #JoinColumn annotation.
edit: to be able to directly call student.getSubjectName() you could still decide to include single parts of the composite foreign key additionally as entity properties, in this case you need to make sure to declare the second (duplicate) column mapping with insertable=false and updatable=false, since its value is already managed by the #ManyToOne fk:
#Entity
static class Student {
#Id
String studentId;
#ManyToOne
Subject subject;
#Column(name = "subject_name", insertable = false, updatable = false)
String subjectName;
}
However, I'd probably prefer simply declaring a custom getSubjectName() getter which just returns subject.getName().

Hibernate/JPA - Is possible to have a children with parent's ID + another field?

I am new using Hibernate and I am trying to know if is possible to change the primary key of a child component with a composite key. This is my parent component:
public class Person{
#Id
#Column(name = "id", unique = true)
private String id;
private String name;
private String surname;
}
This is my child component:
public class User extends Person {
private String email;
private String password;
}
I would like to identify my user with id+email key. Is that possible?
If your inherited entities share the same table, it wouldn't be possible, as in the database there can be only one primary key logic per table.
However if you have a inheritance strategy using joins, it would be possible I guess - check out Hibernate - Foreign and primary keys in inheritance

Spring Data for Couchbase - Metadata Information

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.

Resources