Construct an object when submiting a form - spring

I have a class called Gamer:
Private Long id;
private String name;
private LocalDate date;
#OneToOne
private Adresse adresse
For the adresse class:
private String city;
private String postCode;
On my form using Thymeleaf I've created all the needed inputs to create a gamer.
My inputs:
id, name, date, city, post code
In the Gamer constructor I've set it like this:
public Gamer(String name, LocalDate date,String city,String postCode){
this.name=name;
this.date=date;
this.adresse=new Adresse(city,postCode);
}
All works fine. My PostMapping works fine. The only issue is adresse is null.
I know the issue that an instance of adresse should be created within clicking submit for gamer creation form.
Any solution please?

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 do I link two entities within a #RestController?

I have the following endpoint:
#PostMapping
Employee createEmployee(#Valid #RequestBody Employee newEmployee, BindingResult bindingResult) {
return employeeRepository.save(newEmployee);
}
I can send POST requests as JSON and have an employee created, but how do I link associated entities, such as a Department entity?
When I send a post request, it looks like this (I know it's not valid JSON, it's just browser console output):
departmentId: 1
emailAddress: "dillon#james.com"
firstName: "Dillon"
lastName: "James"
phoneNumber: undefined
The department exists in my database, and has an ID of 1. I thought that Spring would automatically set the departmentId somehow on my Employee model, but it doesn't seem to do that, or I must be missing something.
I'm basically trying to set the department of the newly created employee, but how I have it currently doesn't seem to do so.
For reference, this is how my Employee entity is defined:
#Entity
#Getter
#Setter
public class Employee {
#Id
#GeneratedValue
private Long id;
#NotNull
private String firstName;
#NotNull
private String lastName;
#NotNull
#Column(unique = true)
private String emailAddress;
#ManyToOne
private Department department;
private String phoneNumber;
}

Want to implement Search function in springboot restapi

So i have two entity classes Employee and Address and they are in #OneToOne Relation I'm able to search a Employee's with different parameter like name,designation,department. But i want to search a employee using address fields like Areaname,pincode, phone etc.. How to implement this functionality i'm currently using JpaRepo with Mysql.
#Entity
public class Employee{
#Id#GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String Name;
private String Designation;
private String Department;
#OneToOne(cascade=CascadeType.ALL)
private Address address;}
#Entity
public class Address{
private String Streetname;
private String Areaname;
private String City;
private Long pincode;
#Id
private Long phone;
}
try like this:
#Query("FROM Employee AS emp LEFT JOIN emp.address AS adr WHERE adr.areaname= ?1")
public List<Employee > FindAllEmployeeByAreaName(String areaname);

POJO dataset nested rendering

My POJO Datasource basically contains following structure.
// Company.java
public class Company implements Serializable {
private static final long serialVersionUID = 3130918429913376956L;
private String name;
private String address;
private String contactPerson;
private String mobile;
private String fax;
private String bankDetails;
private String email;
private List<Employee> emps;
//getter and setter.
}
// Employee.java
public class Employee implements Serializable{
/**
*
*/
private static final long serialVersionUID = -4473328670062370497L;
private String name;
private int age;
private String designation;
//getter and setter
}
My scenario is like following
One PDF report may have more that one Company (ie List< Company >)
In case of more than one Company, it should start at new page.
If Employee list goes to next page then it should repeat Header on the next page.
Layout -
Layout xml source
Output Page 1
Page 2
There are two issues with this design
Employee Name header is getting repeated for every employee.
Company Header (Comp Name -> Company A ) should be rendered only once.
Can anyone suggest me correct approach ? Thanks in advance.
Move the 'company name' to the Header row and set the header property to not repeating on new pages. Move the 'Employee name' out of the grouping with 'Employee', so up to the level where 'company name' is now.
I think you know everything to solve this, you just have to fiddle with the groupings a bit.

Multipart file and number of form parameters are not converting as ModelAttribute in Rest controller (Spring 4)

I am using Chrome postman client for rest calls, here I am sending 15 form parameters like fname, last name.. so on, and also two files as file type.
I am submitting my request POST method, body type is form data selected and content type header is empty.
In the server side, I am consume this as method
#RequestMapping(value = "/create"
method = RequestMethod.POST,
consumes = {"multipart/form-data"},
produces = MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
public ResponseEntity<ResponseTO> create( #ModelAttribute RequestTO updateTripRequestTO, HttpServletRequest request);
public class RequestTO implements Serializable {
private UUID id private Date createdate;
private String createdby;
private String updatedby;
private Date updateddate;
private String visibility;
private String name;
private MultipartFile tripimage;
public class RequestTO implements Serializable {
private UUID id private Date createdate;
private String createdby;
private String updatedby;
private Date updateddate;
private String visibility;
private String name;
private MultipartFile tripimage;
( and Set and get Methods)
When I debug the TO, its giving comma separated values in TO.
If I give fname is "Test", lname "Ltest" in the postman, when I debug the TO its coming fname as "Test,Test" and lname as "Ltest,Ltest " for all form fields. Can some suggest me why like this and any solution please.

Resources