I'm using hibernate to store a Student object which is having an embedded type Address object. I'm using a form to get inputs name, city, state. City & State getting null while submitting the form how to capture these values in Model Object. ${student.homeAddress.city} - This value is NULL
Issue Resolved!
public class Student {
#Id
#Column(name = "student_id")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "students_seq")
#SequenceGenerator(sequenceName = "students_seq", name = "students_seq", allocationSize = 1)
private int studentId;
#Column(name = "student_name")
private String studentName;
#Embedded
private Address homeAddress;
}
#Controller
#RequestMapping("/students")
public class StudentController {
#Autowired
StudentService studentService;
#RequestMapping("/registrationForm")
public String showFormForStudentRegistration(Model model) {
Student studentObj = new Student();
Address addressObj = new Address();
studentObj.setHomeAddress(addressObj);
model.addAttribute("student", studentObj);
return "student-form";
}
#RequestMapping("/register")
public String registerStudent(#ModelAttribute("student") Student student) {
studentService.createStudent(student);
return "student-success";
}
}
**${student.address.state} -> This is how needs to be accessed after submitting it. when submitting form it should be like this <form:input path="address.state" **
Data successfully inserted.
Related
I am working on rest api. I got error while delete data by id. All code is complete but don't know why postman fire error. I can map two table with unidirectional mapping using hibernate.
Here down is error in postman:
"message": "Required request body is missing: public org.springframework.http.ResponseEntity<org.springframework.http.HttpStatus> com.rest.RestApiPojo.Controller.PojoController.deleteAddressPerson(com.rest.RestApiPojo.Entity.Person,java.lang.Integer)"
Here down is my code:
Entity
public class Person {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer person_id;
private String name;
#JsonManagedReference
#OneToOne(cascade = CascadeType.ALL, mappedBy = "person")
private Address address;
// getter setter
}
#Table(name = "address_master")
public class Address {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer address_id;
private String city;
private String country;
#JsonBackReference
#OneToOne(cascade=CascadeType.ALL, targetEntity = Person.class)
#JoinColumn(name = "person_id")
private Person person;
// getter setter
}
SeviceImpl
#Override
public void deleteAddressPerson(Integer personId) {
personRepo.deleteById(personId);
}
Controller
#RequestMapping(value = "/dltpersonaddress/{personId}", method = RequestMethod.DELETE)
public ResponseEntity<HttpStatus> deleteAddressPerson(#RequestBody Person person, #PathVariable Integer personId)
{
pojoService.deleteAddressPerson(personId);
return new ResponseEntity<>(HttpStatus.OK);
}
You have an unused #RequestBody Person person parameter in your controller method.
#RequestMapping(value = "/dltpersonaddress/{personId}", method = RequestMethod.DELETE)
public ResponseEntity<HttpStatus> deleteAddressPerson(#RequestBody Person person, #PathVariable Integer personId)
{
pojoService.deleteAddressPerson(personId);
return new ResponseEntity<>(HttpStatus.OK);
}
The error message explains that this param is obligatory, and requests without it wont be processed.
Remove the param to solve the issue.
#Entity
#Table(name="cad_paciente")
#Loader(namedQuery = "selectInicial")
#NamedNativeQuery(
name="selectInicial",
query="select p.*, fu_obter_lista_convenios_pac(p.id) as ds_convenio from cad_paciente p where p.id = ?", resultClass = Paciente.class,
resultSetMapping = "sqlResult")
#SqlResultSetMapping(
name="sqlResult",
entities={
#EntityResult(entityClass = Paciente.class, fields={
#FieldResult(name="ds_convenio",column="ds_convenio")})})
public class Paciente {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#NotNull
#Column(name="id_empresa")
private Integer id_empresa;
...
#Transient
#Column(name="ds_convenio")
private String ds_convenio;
public String getDs_convenio() {
return ds_convenio;
}
public void setDs_convenio(String ds_convenio) {
this.ds_convenio = ds_convenio;
}
My Controller method "pacientes.findAll()" won't return "ds_convenio" field with the correct value, listing "null" always in my JSON return.
What do I have to do?
Try removing the annotation #Transient and provide the column as below :
#Column(name="ds_convenio")
private String ds_convenio;
#org.springframework.data.annotation.Transient specifically states to the spring framework that the Object Mapper you are using should not include this value when converting from Java Object to JSON. Also, it means that the value is not to be persisted into the database, which means you could not query over it.
Or if you want to keep it as transient itself but does not require the value to be serialized then register the object mapper as below :
#Bean
public ObjectMapper includeTransientObjectMapper() {
Hibernate5Module hibernate5Module = new Hibernate5Module();
hibernate5Module.disable(Hibernate5Module.Feature.USE_TRANSIENT_ANNOTATION);
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(hibernate5Module);
return mapper;
}
Or in your case since you want the result of the #NamedNativeQuer in which you aliased ds_convenio, using #FieldResult might be required to get the desired result as follows :
#Entity
#Table(name="cad_paciente")
#Loader(namedQuery = "selectInicial")
#NamedNativeQuery(name="selectInicial", query="select p.*, fu_obter_lista_convenios_pac(p.id) as ds_convenio from cad_paciente p where p.id = ?", resultClass = Paciente.class)
#SqlResultSetMapping(name="Results",
entities={
#EntityResult(entityClass=com.acme.Order.class, fields={
#FieldResult(name="id", column="id"),
#FieldResult(name="id_empresa", column="id_empresa"),
........
})
public class Paciente {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#NotNull
#Column(name="id_empresa")
private Integer id_empresa;
...
#Transient
#Column(name="ds_convenio")
private String ds_convenio;
Read doc
I have 2 tables and I want to update the first table with data after that the second table will be updated automatically.
I'm a beginner in spring boot and I really need your help.
I could insert data from table 1 to table 2 but if I update some data from table 1 then table 2 coudn't be updated.
What can I do?
This is what I have done so far: The two entities of the tables and the service I worked with to insert data into table 2 from table 1.
Table 1:
#Entity
#Table(name = "formation")
public class Formation {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String cursus;
private String groupeaction;
private String module;
private String formateur;
#Temporal(TemporalType.DATE)
private Date date;
private Long nbrappart;
private Long nbrabsent;
private Long hf;
private Long jf;
private Long nbrheures;
private Long tauxh;
private Long ristourneprevis;
private Long couthebergttc;
private Long coutpausecafttc;
Table 2:
#Entity
#Table(name = "tablef")
public class Tablef {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String cursus;
private Long nbrappart;
private Long Sumnbrheures;
private Long Sumjf;
private Long jhf;
private String groupeaction;
the service i used :
public Boolean InserIntoTableF(Tablef tf) {
Long id = ThreadLocalRandom.current().nextLong();
tf.setId(id);
jdbc.execute("insert into tablef (id,cursus,groupeaction
,nbrappart,sumnbrheures,sumjf,jhf)\r\n" +
"select id,cursus,groupeaction,nbrappart,sum(nbrheures),sum(jf)
,sum(jf)*nbrappart\r\n" +
" from formation \r\n" +
"group by cursus ;");
return true;
}
The controller :
#CrossOrigin(origins = "*", maxAge = 3600)
#RestController
#RequestMapping("/api")
#PreAuthorize("hasRole('ADMIN')")
public class FormationController {
#Autowired
private FormationService formationservice;
#Autowired
private FormationRepository formationrepository;
#GetMapping("/formations")
public List<Formation> GetAll() {
return formationrepository.findAll();
}
#GetMapping("/formations/{id}")
public ResponseEntity<Formation> getFormationById(#PathVariable(value = "id") Long formationId)
throws ResourceNotFoundException {
Formation formation = formationrepository.findById(formationId)
.orElseThrow(() -> new ResourceNotFoundException("Formation not found for this id :: " + formationId));
return ResponseEntity.ok().body(formation);
}
#PostMapping("/formations")
public Formation createFormation(#Valid #RequestBody Formation formation) {
return formationrepository.save(formation);
}
// this is how i update my entity formation (table 1)
#PutMapping("/formations/{id}")
public ResponseEntity<Formation> updateFormation(#PathVariable(value = "id") Long formationId,
#Valid #RequestBody Formation formationDetails) throws ResourceNotFoundException {
Formation formation = formationrepository.findById(formationId)
.orElseThrow(() -> new ResourceNotFoundException("Formation not found for this id :: " + formationId));
formation.setCursus(formationDetails.getCursus());
formation.setGroupeaction(formationDetails.getGroupeaction());
formation.setModule(formationDetails.getModule());
formation.setFormateur(formationDetails.getFormateur());
formation.setDate(formationDetails.getDate());
formation.setNbrappart(formationDetails.getNbrappart());
formation.setNbrabsent(formationDetails.getNbrabsent());
formation.setHf(formationDetails.getHf());
formation.setJf(formationDetails.getJf());
formation.setNbrheures(formationDetails.getNbrheures());
formation.setTauxh(formationDetails.getTauxh());
formation.setRistourneprevis(formationDetails.getRistourneprevis());
formation.setCouthebergttc(formationDetails.getCouthebergttc());
formation.setCoutpausecafttc(formationDetails.getCoutpausecafttc());
final Formation updatedFormation = formationrepository.save(formation);
return ResponseEntity.ok(updatedFormation);
}
#DeleteMapping("/formations/{id}")
public Map<String, Boolean> deleteFormation(#PathVariable(value = "id") Long formationId)
throws ResourceNotFoundException {
Formation formation = formationrepository.findById(formationId)
.orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + formationId));
formationrepository.delete(formation);
Map<String, Boolean> response = new HashMap<>();
response.put("deleted", Boolean.TRUE);
return response;
}
#PostMapping(value = "/fileupload")
public ResponseEntity<Formation> uploadFile(#ModelAttribute Formation formation) {
Boolean isFlag=formationservice.saveDataFromFile(formation.getFile());
if(isFlag) {
return new ResponseEntity<>(HttpStatus.OK);
}else
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
// here where i inser data from formation(table1) to tablef (table2)
#PostMapping(value = "/test")
public Boolean AddTf(Tablef tf) {
return formationservice.InserIntoTableF(tf);
}
}
If you use spring boot jpa to persist your data then you can have a look at JPA EntityListener and #PostPersist
#Entity
#EntityListeners(MyEntityListener.class)
public class MyEntity {
#Id
#GeneratedValue
private int id;
private String field;
public MyEntity() { }
}
The MyEntityListener impl
public class MyEntityListener {
#PostPersist
void onPostPersist(MyEntity myEntity) {
// save data to second table that needs an update on myEntity save
}
}
I'm having issues getting a neo4j RelationshipEntity persisted with Spring Boot. I'm using spring-boot-starter-data-neo4j (2.1.0.RELEASE), and the neo4j docker image tagged 3.4.9.
I have a simple NodeEntity, which contains a collection for the RelationshipEntity:
#NodeEntity
public class Book {
#Id
#GeneratedValue
private Long id;
private String name;
public Book() {}
public Book(String name) {
this.name = name;
}
#Relationship(type = "PURCHASED_WITH", direction = "OUTGOING")
private Set<BookPurchase> purchases = new HashSet<>();
// getters and setters follow
}
I have another NodeEntity, which also contains a collection for the relationship entity:
#NodeEntity
public class CreditCard {
#Id
#GeneratedValue
private Long id;
private String number;
#DateString(value = "yyyy-MM-dd")
private Date expiryDate;
public CreditCard() {}
public CreditCard(String number, Date expiryDate) {
this.number = number;
this.expiryDate = expiryDate;
}
#Relationship(type = "PURCHASED_WITH", direction = "INCOMING")
private Set<BookPurchase> purchases = new HashSet<BookPurchase>();
// getters and setters follow
}
I have the RelationshipEntity, which adds references to both NodeEntity classes in the constructor:
#RelationshipEntity(type = "PURCHASED_WITH")
public class BookPurchase {
#Id
#GeneratedValue
private long id;
#DateString("yyyy-MM-dd")
Date purchaseDate;
#StartNode
private Book book;
#EndNode
private CreditCard card;
public BookPurchase(){}
public BookPurchase(CreditCard card, Book book, Date purchaseDate) {
this.card = card;
this.book = book;
this.purchaseDate = purchaseDate;
this.card.getPurchases().add(this);
this.book.getPurchases().add(this);
}
// getters and setters follow
}
And finally I have the Spring controller tying everything together:
#RestController
public class ExamplesController {
#Autowired
CreditCardRepository creditCardRepository;
#PostMapping(value="/purchases")
public String createPurchases() {
CreditCard card = new CreditCard("11111", new GregorianCalendar(2018, Calendar.FEBRUARY, 12).getTime());
Book book1 = new Book("of mice and men");
BookPurchase purchase1 = new BookPurchase(card,book1,new GregorianCalendar(2018, Calendar.MARCH, 15).getTime());
creditCardRepository.save(card);
return "Successfully created entities";
}
}
Whenever I try to curl -X POST http://localhost:8080/purchases, I just see the following in the neo4j browser - the RelationshipEntity is not persisted, only the nodes.
Can anyone assist?
Thanks to Gerrit Meier for answering this one. My RelationshipEntity was using the primitive long instead of the object/wrapper Long. Complete details here: https://community.neo4j.com/t/neo4j-relationshipentity-not-persisted/3039
I have the following Spring boot service for an object of type Report -
#Service
public class ReportService {
#Autowired
private ReportRepository reportRepository;
#Autowired
private UserRepository userRepository;
/*get all reports */
public List<Report> getAllReports(){
return reportRepository.findAll();
}
/*get a single report */
public Report getReport(Long id){
return reportRepository.getOne(id);
}
//other similar methods....
}
The problem arises while retrieving a single Report. If a report ID is send which doesn't exist, the following error is generated...
DefaultHandlerExceptionResolver : Failed to write HTTP message:
org.springframework.http.converter.HttpMessageNotWritableException: Could not
write JSON: Unable to find com.interact.restapis.model.Report with id 16;
nested exception is com.fasterxml.jackson.databind.JsonMappingException:
Unable to find com.interact.restapis.model.Report with id 16 (through
reference chain:
com.interact.restapis.model.Report_$$_jvst83c_1["fromUserId"])
Below is the code for my Report Controller
#RestController
public class ReportController {
#Autowired
private ReportService reportService;
//Get all reports
#GetMapping("/interactions")
public List<Report> getAllReports() {
return reportService.getAllReports();
}
//Get single report
#GetMapping("/interactions/{id}")
public ResponseEntity<Report> getReport(#PathVariable Long id) {
if(reportService.getReport(id) == null)
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
return new ResponseEntity<>(reportService.getReport(id), HttpStatus.OK);
}
#PostMapping("/interactions")
public ResponseEntity<Report> addReport(#RequestBody Report report) {
Report report1 = reportService.addReport(report);
if(report1 == null)
return new ResponseEntity<>(report, HttpStatus.NOT_FOUND);
return new ResponseEntity<>(report1, HttpStatus.OK);
}
//Other request methods...
}
Below is the code for my Report Model class -
#Entity
#Table (name = "report")
#EntityListeners(AuditingEntityListener.class)
#JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Report {
#Id
#Column (name = "id")
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "from_user_id")
private Long fromUserId;
#Column(name = "to_user_id")
private Long toUserId;
#Column(name = "to_user_email")
private String toUserEmail;
#Column(name = "from_user_email")
private String fromUserEmail;
#Temporal(TemporalType.DATE)
#JsonFormat(pattern = "dd-MM-yyyy hh:mm:ss")
#CreatedDate
private Date createdAt;
#Column(nullable = false)
private String observation;
#Column(nullable = false)
private String context;
private String recommendation;
#Column(nullable = false)
private String eventName;
#Temporal(TemporalType.DATE)
#JsonFormat(pattern = "dd-MM-yyyy hh:mm:ss")
#Column(nullable = false)
private Date eventDate;
private boolean isAnonymous;
#Temporal(TemporalType.DATE)
#JsonFormat(pattern = "dd-MM-yyyy hh:mm:ss")
private Date acknowledgementDate;
#OneToMany(cascade = CascadeType.ALL, targetEntity = Action.class)
#JoinColumn(name = "report_id")
private List<Action> actionList;
#Value("${some.key:0}")
private int rating; //Range 0 to 4
private int type;
/*
Getter and setter methods...
*/
}
I want to know if reportRepository.getOne(Long id) returns null so that I can actually check if a particular report doesn't exist in the database. If not, how else can I implement the above?
The JpaRepository.getOne with throw EntityNotFoundException if it couldn't find a record with the given id.
You can use CrudRepository.findById (JpaRepository is a subclass of CrudRepository) which will return an Optional<Report> which can be empty if there are no record for the given id. You can use Optional.isPresent() to check whether it a Report is available or not and take actions accordingly.
Create a method in your ReportRepository.
It will return Report by matched id else return null.
public Optional<Report> findById(Long id);
Note: findById(Long id); should match with the property name in your Report entity.
I am assuming your Report entity is as follows:
public class Entity{
private Long id;
...
}