-
I am trying to create mapped cart entity.but shows 404 error associated with Injection of autowired dependencies failed
Check my source and help me ro solve it..
Mapping plan:
Customer-----one to one------>Cart--------onetomany--->Product
Customer
package com.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
#Entity
public class Customer {
#Id #GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="Cid")
private int customerId;
#Column(name="password")
#NotEmpty(message="Name is mandatory")
private String password;
#Column(name="Email")
#NotEmpty(message="Name is mandatory")
private String email;
#NotEmpty(message="First Name is mandatory")
#Column(name="firstname")
private String firstName;
#NotEmpty(message="Last Name is mandatory")
#Column(name="lastname")
private String lastName;
#Column(name="Mobile")
#Size(min = 10)
#NotEmpty(message="Mobile is mandatory")
private String mobile;
#ManyToOne(cascade=CascadeType.ALL)
#JoinColumn(name="address_id")
private Address delAdderss;
private boolean enabled;
private String role;
#OneToOne(cascade=CascadeType.ALL)
#JoinColumn(name="cart_id")
private Cart cart;
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public Address getDelAdderss() {
return delAdderss;
}
public void setDelAdderss(Address delAdderss) {
this.delAdderss = delAdderss;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public Cart getCart() {
return cart;
}
public void setCart(Cart cart) {
this.cart = cart;
}
}
Cart
package com.model;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
#Entity
public class Cart {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private int cart_id;
#Column
private double total;
#OneToOne(cascade=CascadeType.ALL)
#JoinColumn(name="CID")
private Customer customer;
#OneToMany(cascade = CascadeType.ALL)
#JoinColumn(name="id")
private List<Product> product;
public int getCart_id() {
return cart_id;
}
public void setCart_id(int cart_id) {
this.cart_id = cart_id;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public List<Product> getProduct() {
return product;
}
public void setProduct(List<Product> product) {
this.product = product;
}
}
Product
package com.model;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;
import org.springframework.web.multipart.MultipartFile;
#Entity
public class Product {
#Id #GeneratedValue(strategy=GenerationType.AUTO)
private int id;
#Column
private String product_Name;
#Column
private String descripction;
#Column
private int price;
#Column
private Date mfg_Date;
#Transient
private MultipartFile image;
#ManyToOne(cascade = CascadeType.ALL)
private List<Cart> cart;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProduct_Name() {
return product_Name;
}
public void setProduct_Name(String product_Name) {
this.product_Name = product_Name;
}
public String getDescripction() {
return descripction;
}
public void setDescripction(String descripction) {
this.descripction = descripction;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Date getMfg_Date() {
return mfg_Date;
}
public void setMfg_Date(Date mfg_Date) {
this.mfg_Date = mfg_Date;
}
public MultipartFile getImage() {
return image;
}
public void setImage(MultipartFile image) {
this.image = image;
}
public List<Cart> getCarts() {
return cart;
}
public void setCarts(List<Cart> carts) {
this.cart= carts;
}
}
Related
The contact class as an entity that would be linked with address class
package asmt1.demo.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import asmt1.demo.dto.UserStatus;
//#Entity annotation specifies that the class is an entity and is mapped to a database table.
//#Table annotation specifies the name of the database table to be used for mapping
#Entity
#Table(name="Contactdetail")
public class Contact {
//#Id is used to specify the primary key
#Id
//Generated value is used to generate pk value ie. id to be autogenerated and assign identity column(id)
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
//#column is used to specify the column condition
//#Column( unique = true)
private String firstName;
private String lastName;
//#Column(unique = true)
private long contactNo;
private String mailId;
//list of named constant ie. status
#Enumerated(EnumType.STRING)
private UserStatus status;
//it is used to create one-to-one relationship between the contact and address table
//fetch type.lazy tells Hibernate to only fetch the related entities from the database when you use the relationship
#ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
#JoinTable(name="conadd",
joinColumns = {#JoinColumn(name="id")},
inverseJoinColumns = {#JoinColumn(name="addid")})
//To handle the problem related to the serialization of the model using Jackson API when the model attributes have a lazy loading defined,
//we have to tell the serializer to ignore the chain or helpful garbage that Hibernate adds to classes, so it can manage lazy loading of data by declaring #JsonIgnoreProperties
#JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
private Set<Address> address=new HashSet<>();
//generate getters,setters, toString() and constructor using fields
public Contact() {}
public Contact(String firstName, String lastName, long contactNo, String mailId, UserStatus status,
Set<Address> address) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.contactNo = contactNo;
this.mailId = mailId;
this.status = status;
this.address = address;
}
public Set<Address> getAddress() {
return address;
}
public void setAddress(Set<Address> address) {
this.address = address;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public long getContactNo() {
return contactNo;
}
public void setContactNo(long contactNo) {
this.contactNo = contactNo;
}
public String getMailId() {
return mailId;
}
public void setMailId(String mailId) {
this.mailId = mailId;
}
public UserStatus getStatus() {
return status;
}
public void setStatus(UserStatus status) {
this.status = status;
}
#Override
public String toString() {
return "Contact [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", contactNo=" + contactNo
+ ", mailId=" + mailId + "]";
}
}
the address class which is the entity
package asmt1.demo.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
//#Entity annotation specifies that the class is an entity and is mapped to a database table.
//#Table annotation specifies the name of the database table to be used for mapping
#Entity
#Table(name="addressDetail")
public class Address {
//#Id is used to specify the primarykey
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long addid;
private String street1;
private String street2;
private long zipcode;
private String city;
private String state;
private String Country;
//mappedby is used to specify to relationship
#ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL,mappedBy = "address")
private Set<Contact> contact=new HashSet<>();
//generate getters,setters, toString() and constructor using fields
public long getId() {
return addid;
}
public Set<Contact> getContact() {
return contact;
}
public void setContact(Set<Contact> contact) {
this.contact = contact;
}
public void setId(long id) {
this.addid = id;
}
public String getStreet1() {
return street1;
}
public void setStreet1(String street1) {
this.street1 = street1;
}
public String getStreet2() {
return street2;
}
public void setStreet2(String street2) {
this.street2 = street2;
}
public long getZipcode() {
return zipcode;
}
public void setZipcode(long zipcode) {
this.zipcode = zipcode;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return Country;
}
public void setCountry(String country) {
Country = country;
}
public Address(String street1, String street2, long zipcode, String city, String state, String country) {
super();
this.street1 = street1;
this.street2 = street2;
this.zipcode = zipcode;
this.city = city;
this.state = state;
Country = country;
}
public Address() {}
}
a request class where I was calling both for data uploading
package asmt1.demo.dto;
import java.util.HashSet;
import java.util.Set;
import asmt1.demo.entity.Address;
import asmt1.demo.entity.Contact;
public class AddressReq {
private Set<Address> address=new HashSet<>();
private Set<Contact> contact=new HashSet<>();
public Set<Address> getAddress() {
return address;
}
public void setAddress(Set<Address> address) {
this.address = address;
}
public Set<Contact> getContact() {
return contact;
}
public void setContact(Set<Contact> contact) {
this.contact = contact;
}
public AddressReq(Set<Address> address, Set<Contact> contact) {
super();
this.address = address;
this.contact = contact;
}
public AddressReq() {}
#Override
public String toString() {
return "AddressReq [address=" + address + ", contact=" + contact + "]";
}
}
enum class for status
package asmt1.demo.dto;
//constant value for userstatus class
public enum UserStatus {
ACTIVE,INACTIVE
}
controller class
package asmt1.demo.controller;
import java.util.List;
import java.util.NoSuchElementException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import asmt1.demo.converter.ContactConverter;
import asmt1.demo.dto.AddressReq;
import asmt1.demo.dto.ContactDto;
import asmt1.demo.entity.Contact;
import asmt1.demo.repository.AddressRepo;
import asmt1.demo.repository.ContactRepo;
//#restcontroller is used for restful services
#RestController
//#RequestMapping is used for creating baseurl for controller will be used
#RequestMapping("/contact")
public class ContactController {
//#Autowired will search the object of class
#Autowired
private ContactRepo ctrepo;
#Autowired
private AddressRepo addrepo;;
#Autowired
private ContactConverter converter;
//#Requestbody is used to map/bind methods with pojo pr value to return value to the web
//#postmapping is used to add data to database from web
#PostMapping("/add")
public List<Contact> newcontact(#RequestBody AddressReq req) {
return ctrepo.saveAll(req.getContact());
}
//#getmapping is used to get the details/records from database on web page
#GetMapping("/contactlist")
public List<Contact> getcontactlist(){
return ctrepo.findAll(Sort.by(Sort.Direction.ASC, "firstName","lastName"));
}
#GetMapping("/contactdto")
public List<ContactDto> getcontactlistdto(){
List<Contact> findAll=ctrepo.findAll();
return converter.entitytodto(findAll);
}
#GetMapping("/contactlist/{id}")
public ResponseEntity<Contact> get(#PathVariable Long id) {
try {
Contact contact = ctrepo.getOne(id);
return new ResponseEntity<Contact>(contact, HttpStatus.OK);
} catch (NoSuchElementException e) {
return new ResponseEntity<Contact>(HttpStatus.NOT_FOUND);
}
}
#GetMapping("/contactdto/{id}")
public ContactDto getbyid(#PathVariable Long id) {
Contact orElse=ctrepo.findById(id).orElse(null);
return converter.entitytodto(orElse);
}
#GetMapping("/orderlist")
public List<Contact> getcontactlistbyorder(){
return ctrepo.findAllByOrderByIdDesc();
}
#PostMapping("/save")
public ContactDto savedto(#RequestBody ContactDto dto) {
Contact contact=converter.dtotoentity(dto);
contact=ctrepo.save(contact);
return converter.entitytodto(contact);
}
//#deletemapping is used to delete the records/details from database by web page
#DeleteMapping("/delete/{id}")
public String deletebyid(#PathVariable long id){
if (ctrepo.findById(id)==null) {
return "Id not found.....Please enter correct id";
}
ctrepo.deleteById(id);
return "Successfully deleted "+id;
}
//#putmapping is used to change/update the records/details in database by web page
#PutMapping("/edit")
public List<Contact> editcontactbyid(#RequestBody AddressReq req ){
return ctrepo.saveAll(req.getContact());
}
}
here is the Json format which I was uploading the data but its showing me error that
at [Source: (PushbackInputStream); line: 1, column: 2]]
2021-05-04 12:57:07.799 WARN 876 --- [nio-9090-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of java.util.HashSet<asmt1.demo.entity.Contact> out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.HashSet<asmt1.demo.entity.Contact> out of START_OBJECT token
at [Source: (PushbackInputStream); line: 1, column: 13] (through reference chain: asmt1.demo.dto.AddressReq["contact"])]
{"contact":{
"firstName":"tomu",
"lastName":"shawn",
"contactNo":9124245,
"mailId":"ggia#gmail.com",
"status":"INACTIVE",
"address":{
"street1":"A/wing-24",
"street2":"plotno-4",
"city":"Mumbai",
"state":"Maharashtra",
"country":"India",
"zipcode":705
}}}
In your AddressReq class contact is set that is collection but in your pay load you are sending an object which should be collection of object.
Based on the AddressReq class the pay load should be
{["contact":{
"firstName":"tomu",
"lastName":"shawn",
"contactNo":9124245,
"mailId":"ggia#gmail.com",
"status":"INACTIVE",
"address":{
"street1":"A/wing-24",
"street2":"plotno-4",
"city":"Mumbai",
"state":"Maharashtra",
"country":"India",
"zipcode":705
}
}]
}
or if your request is always single entry of contact then you can change the contact property to single instance not the collection of Contact instance.
I have two entities: Project.java and Employee.java that are ManyToMany mapped with each other (internally using the Hibernate generated table emp_project_mapping).
Now, I have a new one: Role.java that is to be mapped with above mentioned entities such that an employee in a project can have only one role. Also, this mapping should use that same mapping_table emp_project_mapping.
I have no idea how to achieve this scenario. Please help me out.
Employee.java
package com.ayush.springbootApp.bootCrudApi.model;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.ayush.springbootApp.bootCrudApi.model.Project;
import com.ayush.springbootApp.bootCrudApi.model.Workstation;
#Entity
#Table (name="employee_table")
//model class for Employee
public class Employee {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column
private Integer id;
#Column
private String name;
#Column
private String gender;
#Column
private String department;
#Column
private String designation;
#Column
private Date dob;
#Column(name="empcode",nullable=false,unique=true)
private String empCode;
#OneToOne(mappedBy="emp")
private Workstation workStation;
#ManyToMany
#JoinTable(name="emp_project_mapping",joinColumns=#JoinColumn(name="emp_id"),inverseJoinColumns=#JoinColumn(name="project_id"))
private Set<Project> prj=new HashSet<Project>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String getEmpCode() {
return empCode;
}
public void setEmpCode(String empCode) {
this.empCode = empCode;
}
public Set<Project> getProject()
{
return this.prj;
}
public void setProject(Set<Project> prj)
{
this.prj=prj;
}
#Override
public String toString() {
return "Employee [id=" + id + ", code="+ empCode + ", name=" + name + ", gender=" + gender + ", department=" + department
+ ", designation=" + designation + ", dob=" + dob + "]";
}
}
Project.java
package com.ayush.springbootApp.bootCrudApi.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import com.ayush.springbootApp.bootCrudApi.model.Employee;
#Entity
#Table(name="projects_table")
public class Project{
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column
private Integer id;
#Column(nullable=false)
private String oea_number;
#Column(nullable=false)
private String project_name;
#Column
private String client_name;
#Column
private String origin_country;
#Column(nullable=false)
private Integer po_amount;
#Column
private int project_active;
#ManyToMany(mappedBy="prj")
private Set<Employee> emp=new HashSet<Employee>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getOea_number() {
return oea_number;
}
public void setOea_number(String oea_number) {
this.oea_number = oea_number;
}
public String getProject_name() {
return project_name;
}
public void setProject_name(String project_name) {
this.project_name = project_name;
}
public String getClient_name() {
return client_name;
}
public void setClient_name(String client_name) {
this.client_name = client_name;
}
public String getOrigin_country() {
return origin_country;
}
public void setOrigin_country(String origin_country) {
this.origin_country = origin_country;
}
public Integer getPo_amount() {
return po_amount;
}
public void setPo_amount(Integer po_amount) {
this.po_amount = po_amount;
}
public int getProject_active()
{
return this.project_active;
}
public void setProject_active(int project_active)
{
this.project_active=project_active;
}
public Set<Employee> getEmployee()
{
return this.emp;
}
public void setEmployee(Set<Employee> emp)
{
this.emp=emp;
}
#Override
public String toString() {
return "Project [id=" + id + ", oea_number=" + oea_number + ", project_name=" + project_name + ", client_name="
+ client_name + ", origin_country=" + origin_country + ", po_amount=" + po_amount + "]";
}
}
Role.java
package com.ayush.springbootApp.bootCrudApi.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToOne;
import javax.persistence.Table;
#Entity
#Table(name="Employee_roles")
public class Role
{
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
#Column(name="description",nullable=false)
private String desc;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
emp_project_mapping table
I have data model like below
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import org.hibernate.annotations.DiscriminatorOptions;
#SuppressWarnings("deprecation")
#Entity
#Table
#Inheritance(strategy = InheritanceType.JOINED)
#DiscriminatorColumn(name="DTYPE", discriminatorType=DiscriminatorType.INTEGER)
#DiscriminatorValue("0")
#DiscriminatorOptions(force=true)
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
public Employee() {
}
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "EMPLOYEE_ID")
private int empID;
private String firstName;
private String lastName;
private Integer age;
private String email;
private String city;
private String phNum;
public String getPhNum() {
return phNum;
}
public void setPhNum(String phNum) {
this.phNum = phNum;
}
public int getEmpID() {
return empID;
}
public void setEmpID(int empID) {
this.empID = empID;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
Patient.java
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import org.hibernate.annotations.DiscriminatorOptions;
import org.hibernate.validator.constraints.NotEmpty;
#SuppressWarnings("deprecation")
#Entity
#Table
#PrimaryKeyJoinColumn(name = "EMPLOYEE_ID")
#DiscriminatorValue("6")
public class Patient extends Employee {
private static final long serialVersionUID = 1L;
#NotEmpty(message = "DOJ cannot be null")
private String doj;
private String primaryDoctor;
public String getPrimaryDoctor() {
return primaryDoctor;
}
public void setPrimaryDoctor(String primaryDoctor) {
this.primaryDoctor = primaryDoctor;
}
public String getDoj() {
return doj;
}
public void setDoj(String doj) {
this.doj = doj;
}
#OneToMany(cascade = CascadeType.ALL, mappedBy = "patient", fetch = FetchType.LAZY)
private List<Encounter> encounterList;
public List<Encounter> getEncounterList() {
return encounterList;
}
public void setEncounterList(List<Encounter> encounterList) {
this.encounterList = encounterList;
}
}
Hibernate query at runtime
select useraccoun0_.empID as empID1_13_2_, useraccoun0_.EMPLOYEE_ID as EMPLOYEE6_13_2_,
useraccoun0_.fullName as fullName2_13_2_, useraccoun0_.password as password3_13_2_,
useraccoun0_.role as role4_13_2_, useraccoun0_.userName as userName5_13_2_,
employee1_.EMPLOYEE_ID as EMPLOYEE2_2_0_, employee1_.age as age3_2_0_,
employee1_.city as city4_2_0_, employee1_.email as email5_2_0_, employee1_.firstName as firstNam6_2_0_,
employee1_.lastName as lastName7_2_0_, employee1_.phNum as phNum8_2_0_, employee1_4_.doj as doj1_11_0_,
employee1_4_.primaryDoctor as primaryD2_11_0_, employee1_5_.specialization as speciali1_0_0_,
employee1_.DTYPE as DTYPE1_2_0_, encounterl2_.EMPLOYEE_ID as EMPLOYEE4_3_4_,
encounterl2_.EID as EID1_3_4_, encounterl2_.EID as EID1_3_1_, encounterl2_.labTest_testID as labTest_2_3_1_,
encounterl2_.medication_mid as medicati3_3_1_, encounterl2_.EMPLOYEE_ID as EMPLOYEE4_3_1_,
encounterl2_.vitalSign_EID as vitalSig5_3_1_ from UserAccount useraccoun0_
left outer join Employee employee1_ on useraccoun0_.EMPLOYEE_ID=employee1_.EMPLOYEE_ID
left outer join Pharmacist employee1_1_ on employee1_.EMPLOYEE_ID=employee1_1_.EMPLOYEE_ID
left outer join Nurse employee1_2_ on employee1_.EMPLOYEE_ID=employee1_2_.EMPLOYEE_ID
left outer join LabAssistant employee1_3_ on employee1_.EMPLOYEE_ID=employee1_3_.EMPLOYEE_ID
left outer join Patient employee1_4_ on employee1_.EMPLOYEE_ID=employee1_4_.EMPLOYEE_ID
left outer join Doctor employee1_5_ on employee1_.EMPLOYEE_ID=employee1_5_.EMPLOYEE_ID
left outer join Encounter encounterl2_ on employee1_.EMPLOYEE_ID=encounterl2_.EMPLOYEE_ID
where useraccoun0_.empID=4
In this query Discriminator column(DTYPE) is coming and its value is coming as 6. But it is giving me below exception
org.hibernate.WrongClassException: Object [id=null] was not of the specified subclass [com.*.*.Employee]: the class of the given object did not match the class of persistent copy
So not sure why it is giving me this exception and why id is coming as null.
User.java
package com.spring.demo.model;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
#Entity
#Table(name="user")
public class User {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="user_id")
private int id;
private String fName;
private String lName;
#Column(unique=true,nullable=true)
private String email;
#Column(unique=true,nullable=true)
private long mobile;
private Date dob;
#Lob
private byte[] image;
#Transient
private String base64Image;
#OneToOne(cascade=CascadeType.ALL,fetch =FetchType.EAGER)
#JoinColumn(name="userCredential_id")
private UserCredential userCredential;
#OneToOne(cascade=CascadeType.ALL,fetch =FetchType.EAGER)
#JoinColumn(name="add_id")
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getMobile() {
return mobile;
}
public void setMobile(long mobile) {
this.mobile = mobile;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
public UserCredential getUserCredential() {
return userCredential;
}
public void setUserCredential(UserCredential userCredential) {
this.userCredential = userCredential;
}
}
UserCredential.java
package com.spring.demo.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import com.fasterxml.jackson.annotation.JsonIgnore;
#Entity
#Table(name="usercredential")
public class UserCredential {
#Id
#Column(name="credential_id")
#GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
#Column(unique=true,nullable=true)
private String username;
private String password;
private String cnfrmpassword;
#JsonIgnore
#OneToOne(cascade=CascadeType.ALL,fetch =FetchType.EAGER)
#JoinColumn(name="user_id",nullable=true)
private User user;
public UserCredential() {
super();
// TODO Auto-generated constructor stub
}
public UserCredential(int id, String username, String password, String cnfrmpassword, User user) {
super();
this.id = id;
this.username = username;
this.password = password;
this.cnfrmpassword = cnfrmpassword;
this.user = user;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCnfrmpassword() {
return cnfrmpassword;
}
public void setCnfrmpassword(String cnfrmpassword) {
this.cnfrmpassword = cnfrmpassword;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
Address.java
package com.spring.demo.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
#Entity
#Table(name="address")
public class Address {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="add_id")
private int id;
#Column(name="city")
private String city;
#Column(name="state")
private String state;
#Column(name="house_no")
private String h_no;
#JsonIgnore
#OneToOne(cascade=CascadeType.ALL,fetch =FetchType.EAGER)
#JoinColumn(name="user_id", nullable=true)
private User user;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getH_no() {
return h_no;
}
public void setH_no(String h_no) {
this.h_no = h_no;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
Here we have user as a parent table and (usercredential and address) are child classes in a relationship. When I insert data into tables then every primary key automatically incremented and get the appropriate value while the foreign key (user_id) always remains zero.
https://i.stack.imgur.com/Pivlm.jpg
https://i.stack.imgur.com/fAPth.jpg
https://i.stack.imgur.com/l37mr.jpg
My concern is user_id(foreign key) in child tables should not be null and equals to primary key(user_id) in parent table. Please look for every cascading(delete, update) operation should be implemented well on table.
Further information I am using Json for inserting data into tables.
{
"fName":"sur kumst",
"lName":"adfdf",
"mobile":45106,
"email":"ksusjasd1sd#gmail.com",
"dob":"2012-04-23T18:25:43.511Z",
"address":{
"city":"noida",
"state":"up",
"h_no":"1243"
},
"userCredential":{
"username":"kr0302",
"password":"12345",
"cnfrmpassword":"12345"
}
}
The issue is with the back reference. Hibernate cannot maintain this for you. Say you save your user object. it creates a credential row and generates id. it creates address and id. it updates the cred_id and add_id on the user object and then creates a row for it and generates id and returns that value. at this point you need to add your user object to credential and address and save those again.
It seems you are trying to model two bidirectional relationships:
User <-> UserCredentials and:
User <-> UserAddress.
But what you are really creating the following four relationships:
User -> UserCredentials
User <- UserCredentials
User -> UserAddress
User <- UserAddress
In order to fix this you need to use mappedBy. See this question for reference.
I have coded three classes (User, UserCredential, Address) where I want to store data into tables using mapping. I am using JSON to store data into tables.
When I store data, data are stored in all tables but in user id it shows 1, in UserCredential id it shows 3 and in Address id it shows 2 while in first entry all id should be 1.
classes are
package com.spring.demo.model;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
#Entity
#Table(name="user")
public class User {
#Id
#Column(name="user_id")
#GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String fName;
private String lName;
#Column(unique=true,nullable=true)
private String email;
#Column(unique=true,nullable=true)
private long mobile;
private Date dob;
#Lob
private byte[] image;
#Transient
private String base64Image;
#OneToOne(cascade=CascadeType.ALL,fetch =FetchType.EAGER)
#JoinColumn(name="userCredential_id")
private UserCredential userCredential;
#OneToOne(cascade=CascadeType.ALL,fetch =FetchType.EAGER)
#JoinColumn(name="add_id")
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getMobile() {
return mobile;
}
public void setMobile(long mobile) {
this.mobile = mobile;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
public UserCredential getUserCredential() {
return userCredential;
}
public void setUserCredential(UserCredential userCredential) {
this.userCreenter code heredential = userCredential;
}
}
UserCredential.java
package com.spring.demo.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import com.fasterxml.jackson.annotation.JsonIgnore;
#Entity
public class UserCredential {
#Id
#Column(name="credential_id")
#GeneratedValue(strategy=GenerationType.AUTO)
private int id;
#Column(unique=true,nullable=true)
private String username;
private String password;
private String cnfrmpassword;
#JsonIgnore
#OneToOne(cascade=CascadeType.ALL)
#JoinColumn(name="user_id")
private User user;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCnfrmpassword() {
return cnfrmpassword;
}
public void setCnfrmpassword(String cnfrmpassword) {
this.cnfrmpassword = cnfrmpassword;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
Address.java
package com.spring.demo.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
#Entity
#Table(name="address")
public class Address {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="add_id")
private int id;
#Column(name="city")
private String city;
#Column(name="state")
private String state;
#Column(name="house_no")
private String h_no;
#ManyToOne
#JoinColumn(name="user_id", nullable=true)
private User user;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getH_no() {
return h_no;
}
public void setH_no(String h_no) {
this.h_no = h_no;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
i really don't understand how to overcome this problem.
JSON format to store data
{
"fName":"suresh kumst",
"lName":"dingh",
"mobile":4595498366,
"email":"ksuraj1sd00#gmail.com",
"dob":"2012-04-23T18:25:43.511Z",
"address":{
"city":"noida",
"state":"up",
"h_no":"123"
},
"userCredential":{
"username":"ksuraj1asd002",
"password":"12345",
"cnfrmpassword":"12345"
}
}
and the response with different id while they should be 1 in first entry and user id should have value
{
"id": 1,
"fName": "suresh kumst",
"lName": "dingh",
"email": "ksuraj1sd00#gmail.com",
"mobile": 4595498366,
"dob": "2012-04-23T18:25:43.511+0000",
"image": null,
"userCredential": {
"id": 3,
"username": "ksuraj1asd002",
"password": "12345",
"cnfrmpassword": "12345"
},
"address": {
"id": 2,
"city": "noida",
"state": "up",
"h_no": "123",
"user": null
}
}
Here, you are using #GeneratedValue(strategy=GenerationType.AUTO).
Some DB's will use a common sequence to generate and assign the sequence no's. Hibernate will create a table hibernate_sequence and all the entity tables will refer to this table to get the next sequence no. So, the primary keys will be scattered among the entities.
To have the primary key start from 1 for each entity, use #GeneratedValue(strategy=GenerationType.IDENTITY) with each entity.
When you use #GeneratedValue(strategy=GenerationType.AUTO) the underlying ORM framework will use either identity column, sequence or table, depending of the underlying DB. In your case, I guess it is using a sequence (a single sequence for generating IDs for all the tables). If that is the case, in order to achieve that each table IDs are independent from other tables, you'll have to give different sequence name to each entity.
If you're using Hibernate, consider using #GenericGenerator. Example from other SO answer:
#GenericGenerator(
name = "wikiSequenceGenerator",
strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
parameters = {
#Parameter(name = "sequence_name", value = "WIKI_SEQUENCE"),
#Parameter(name = "initial_value", value = "1000"),
#Parameter(name = "increment_size", value = "1")
}
)
#Id
#GeneratedValue(generator = "wikiSequenceGenerator")
Kindly use DB sequence so that you have better control of ID generated, Here I'm just sharing sample of one assuming back end DB is Oracle/mysql
#Id
#GeneratedValue(strategy=GenerationType.AUTO, generator = "employee_sequence")
#SequenceGenerator(name = "employee_sequence", sequenceName = "EMP_SN")
private Long empNo;
Here EMP_SN is DB Sequence.
Hope this will solve your problem.