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

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
}

Related

#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().

Spring boot using Autowire Cofiguration property class in Entity class

I have to use some set data member class in Spring entity class
Current Entity class
Entity(name="users")
public class Users{
#Id
#GeneratedValue
#Column(name=ID")
private long Id;
#Column(name=NAME")
private String name;
#Column(name=AGE")
private String age;
#Column(name=PIN")
private String pin;
public Users(String name, String age, String pin)
{
this.name = name;
this.age = age;
this.pin = pin;
}
}
Now I need to add a new Member which is unique to that place
areaId, we run sperate application per each area so this will be passed from command line arguments or config properties during application starts.
My properties class looks like below
#Component
#ConfigurationProperties("user.info")
public class UserProperties{
public String areaId;
public String getAreaID(){return this.areaId;}
public void setAreaID(String areaId){ this.areaId = areaId;}
}
users:
info:
areaId:124
I have to store this and initializes also during Users object constructing, here I am trying to make simple
Entity(name="users")
public class Users{
#Id
#GeneratedValue
#Column(name=ID")
private long Id;
#Column(name=NAME")
private String name;
#Column(name=AGE")
private String age;
#Column(name=PIN")
private String pin;
#Column(name=AREAID")
private String areaId;
public Users(String name, String age, String pin)
{
this.name = name;
this.age = age;
this.pin = pin;
this.areaId = ""//?? how to get area id directly ?
}
}
I can not change the constructor of Users because it demands changes in the other application which are using this lib
Want to Autowire a users properties class inside Entity class(but this is not suggestable as read in some articles )
What would be the best way to initialize that default kind of variable?
It seems that your property is something static.
Then get it from a static way at start.
There's several ways to get command line value at start directly or in a static block:
static {
(your code here)
}
You'll can put #Column on a getter on this property after that.

N1QL Ansi join using spring data couchbase on document id

I'm trying to do a join using spring data couchbase.
The documentation has this example
#Document
public class Author {
#Id
String id;
String name;
#N1qlJoin(on = "lks.name=rks.authorName")
List<Book> books;
#N1qlJoin(on = "lks.name=rks.name")
Address address;
...
}
I need to do the same, except my model uses the document key:
#Document
public class Author {
#Id
String id;
String addressId;
//#N1qlJoin(on = "lks.addressId=meta(rks).id") this doesn't work
#N1qlJoin(on = "lks.addressId=rks.id") //nor this
Address address;
...
}

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 .

Query child record from parent

I have 2 classes: Staff and State.
public class State {
#Id
private String id;
private String name;
}
public class Staff {
#Id
#Column(nullable = false, columnDefinition="VARCHAR(100)")
private String id;
private String name;
private String phoneNo;
private String address;
#OneToOne
#JoinColumn(name = "state_id")
private State state;
}
How to get query using JpaRepository to filter by State.name. Since it is mapped using state_id?
Something similar to this:
//current DAO to filter by staff's name
List<Staff> findByNameContaining(String name);
But I want to filter by name of Staff and by name of State.
try this.
List<Staff> findByNameAndState_Name(String staffName, String stateName)
You can do that as well. try to create a method as following. pass the state name.
List findByStateName(String stateName);

Resources