Error accessing field X by reflection for persistent property X - java

I have 2 classes, TransportOrder and LardiSendedOrder. It's bounded by field innerId, and I just want list all records from LardiSendedOrder table, but got this error.
Error accessing field [private java.lang.Integer com.example.model.LardiSendedOrder.innerId] by reflection for persistent property [com.example.model.LardiSendedOrder#innerId] : 1;
class Transport Order
#Entity
class TransportOrder {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String url;
private String source;
private String transport;
private Date start_date;
private Date end_date;
private String start_address;
private String end_address;
private ArrayList<String> phones =new ArrayList<>()
private Integer distance;
private String content;
private String weight;
private String size;
private Boolean fromVendor;
private ArrayList<PaymentType> payment_type = new ArrayList<PaymentType>();
private Double payment;
private String payment_by;
private ArrayList<PaymentEvent> payment_event = new ArrayList<PaymentEvent>();
private Date add_date;
#NotNull
#Column(unique=true)
private Integer hashCode;
LardiSendedOrder getLardiSendedOrder() {
return lardiSendedOrder
}
void setLardiSendedOrder(LardiSendedOrder lardiSendedOrder) {
this.lardiSendedOrder = lardiSendedOrder
}
#OneToOne
#JoinColumn(
/* referencedColumnName = "innerId"*/
referencedColumnName = "innerId"
)
private LardiSendedOrder lardiSendedOrder;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
//another getters and setters omitted
}
class LardiSendedOrder
#Entity
class LardiSendedOrder {
#Id
#GeneratedValue(strategy= GenerationType.AUTO)
private Integer id;
private Integer innerId;
private Integer outerId;
private Date publish_date;
Integer getUserId() {
return userId
}
void setUserId(Integer userId) {
this.userId = userId
}
private Integer userId;
TransportOrder getTransportOrder() {
return transportOrder
}
void setTransportOrder(TransportOrder transportOrder) {
this.transportOrder = transportOrder
}
#OneToOne(
cascade = CascadeType.ALL,
fetch = FetchType.LAZY,
mappedBy = "lardiSendedOrder")
private TransportOrder transportOrder;
Integer getInnerId() {
return innerId
}
void setInnerId(Integer innerId) {
this.innerId = innerId
}
Integer getOuterId() {
return outerId
}
void setOuterId(Integer outerId) {
this.outerId = outerId
}
String getPublish_date() {
return publish_date.format("dd.MM.yyyy HH:mm:ss");;
}
void setPublish_date(Date publish_date) {
this.publish_date = publish_date
}
}
And i just call
ArrayList<LardiSendedOrder> orders = (ArrayList<LardiSendedOrder>) lardiSendedOrderRepository.findAll();

Related

How to make an entity as a primary key for the other entity

I have two entities Institute and the InstituteVerificationStatus. I want to make the Institute as a Primary key for the InstituteVerificationStatus Entity. I add #Id annotation, but it is giving me an error.
The InstituteVerficationStatus Class doesn't have ID class.
Institute:
#Entity
#Table(name="Institute")
public class institute implements Serializable {
/**
*
*/
private static final long serialVersionUID = 617543973484435246L;
#Id
#Column(name = "id")
private String id;
#Column(name = "vendorId")
private String vendorId;
#Column(name = "name")
private String name;
#Column(name = "profilePicPath")
private String profilePicPath;
#Column(name = "contactNumber")
private String contactNumber;
#Column(name = "email")
private String email;
#Column(name = "gstNo")
private String gstNo;
#Column(name = "address")
private String address;
#OneToOne(mappedBy = "institute")
private instituteVerificationStatus instituteVerificationStatus;
}
InstituteVerificationStatus:
#Entity
#Table(name="InstituteVerificationStatus")
public class instituteVerificationStatus implements Serializable {
/**
*
*/
private static final long serialVersionUID = -1603576445855941895L;
#Id
#JoinColumn(name ="instituteId")
#OneToOne
private institute institute;
#Column(name="verifiedByUserId")
private String verifiedByUserId;
#Column(name="status")
private Character status;
#Column(name="verificationTs")
private Timestamp verificationTs;
public instituteVerificationStatus(institute institute) {
this.institute = institute;
this.status='N';
}
public institute getInstitute() {
return institute;
}
public void setInstitute(institute institute) {
this.institute = institute;
}
public String getVerifiedByUserId() {
return verifiedByUserId;
}
public void setVerifiedByUserId(String verifiedByUserId) {
this.verifiedByUserId = verifiedByUserId;
}
public Character getStatus() {
return status;
}
public void setStatus(Character status) {
this.status = status;
}
public Timestamp getVerificationTs() {
return verificationTs;
}
public void setVerificationTs(Timestamp verificationTs) {
this.verificationTs = verificationTs;
}
}
Try using the MapsId like below and add id which has the same data structure like the first entity's id, it will work fine.
#Entity
#Table(name="InstituteVerificationStatus")
public class instituteVerificationStatus implements Serializable {
/**
*
*/
private static final long serialVersionUID = -1603576445855941895L;
#Id
#Column(name ="instituteId")
private String id;
#JoinColumn(name ="instituteId")
#OneToOne
#MapsId
private institute institute;
#Column(name="verifiedByUserId")
private String verifiedByUserId;
#Column(name="status")
private Character status;
#Column(name="verificationTs")
private Timestamp verificationTs;
public instituteVerificationStatus(institute institute) {
this.institute = institute;
this.status='N';
}
public institute getInstitute() {
return institute;
}
public void setInstitute(institute institute) {
this.institute = institute;
}
public String getVerifiedByUserId() {
return verifiedByUserId;
}
public void setVerifiedByUserId(String verifiedByUserId) {
this.verifiedByUserId = verifiedByUserId;
}
public Character getStatus() {
return status;
}
public void setStatus(Character status) {
this.status = status;
}
public Timestamp getVerificationTs() {
return verificationTs;
}
public void setVerificationTs(Timestamp verificationTs) {
this.verificationTs = verificationTs;
}
}

Spring REST Many To Many with an extra column

I have three Entities in my Spring-Boot Application:
Recipe N->1 RecipeIngredient 1->N Ingredient
It corresponds to a many to many relationship. However, the RecipeIngredient has an extra field "quantity".
I am trying to link together my table so that I can get a Recipe with its ingredients and their quantities.
So far I have this code:
Recipe
#Data
#Entity
#Table(name="recipe")
public class Recipe implements Serializable {
#Id
#Column(name="id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#Column(name="name")
private String name;
#Column(name="duration")
private int duration;
#Column(name="created_on")
private LocalDate created_on = LocalDate.now();
#Column(name = "preparation")
private String preparation;
#Column(name = "difficulty")
private int difficulty;
#Column(name = "serving")
private int serving;
#Column(name = "pricing")
private int pricing;
#JsonManagedReference("recipe")
#OneToMany(mappedBy = "recipe",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<RecipeIngredient> ingredients = new ArrayList<>();
public Recipe() {
}
public Recipe(String name, int duration, LocalDate created_on, String preparation, int difficulty, int serving, int pricing) {
this.name = name;
this.duration = duration;
this.created_on = created_on;
this.preparation = preparation;
this.difficulty = difficulty;
this.serving = serving;
this.pricing = pricing;
}
}
Ingredient
#Data
#Entity
#Table(name = "ingredient")
public class Ingredient implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private int id;
#Column(name = "name")
private String name;
#JsonManagedReference("ingredient")
#OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL)
private List<RecipeIngredient> recipeIngredients = new ArrayList<>();
public Ingredient() {
}
public Ingredient(int id, String name, List<RecipeIngredient> recipeIngredients) {
this.id = id;
this.name = name;
this.recipeIngredients = recipeIngredients;
}
}
RecipeIngredient
#Entity
#Table(name = "recipe_ingredient")
#Data
public class RecipeIngredient implements Serializable {
#EmbeddedId
public RecipeIngredientId recipeIngredientId = new RecipeIngredientId();
#JsonBackReference("recipe")
#ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
#MapsId("recipe_id")
private Recipe recipe;
#JsonBackReference("ingredient")
#ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
#MapsId("ingredient_id")
private Ingredient ingredient;
#Column(name="quantity")
private String quantity;
public RecipeIngredient() {
System.out.println("[RecipeIngredient] no args constructor called");
}
public RecipeIngredient(Recipe recipe, Ingredient ingredient, String quantity) {
this.recipe = recipe;
this.ingredient = ingredient;
this.quantity = quantity;
}
}
And finally RecipeIngredientId
#Embeddable
public class RecipeIngredientId implements Serializable {
private static final long serialVersionUID = 1L;
private int recipe_id;
private int ingredient_id;
public RecipeIngredientId() {
}
public RecipeIngredientId(int recipe_id, int ingredient_id) {
this.recipe_id = recipe_id;
this.ingredient_id = ingredient_id;
}
public int getRecipe_id() {
return recipe_id;
}
public void setRecipe_id(int recipe_id) {
this.recipe_id = recipe_id;
}
public int getIngredient_id() {
return ingredient_id;
}
public void setIngredient_id(int ingredient_id) {
this.ingredient_id = ingredient_id;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RecipeIngredientId that = (RecipeIngredientId) o;
return recipe_id == that.recipe_id &&
ingredient_id == that.ingredient_id;
}
#Override
public int hashCode() {
return Objects.hash(recipe_id, ingredient_id);
}
}
In my browser, can request all recipe and I obtain the following:
Ideally, I would like to obtain ingredients like:
...
"ingredients": [
{
"name": "salad",
"quantity: 2
}
]
Finally, is it possible with Spring / Hibernate to create a new Recipe, and add it new ingredients in a single request ? Or should I create the ingredients first and then pass them to the recipe ?

JPA system exception error accesing field

I'm trying to implement a unidirectional many to many relationship between entities with spring+JPA.
After a few tries changing hibernate versions I don't know whats the cause
Caused by: org.springframework.orm.jpa.JpaSystemException: Error accessing field [private java.lang.Integer com.uca.refactor2.model.Achievement.id] by reflection for persistent property [com.uca.refactor2.model.Achievement#id] : 1; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.Integer com.uca.refactor2.model.Achievement.id] by reflection for persistent property [com.uca.refactor2.model.Achievement#id] : 1
User.java
#Entity
#Table(name="USER")
public class User implements Serializable {
private static final long serialVersionUID = 4402583037980335445L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String firstName;
private String lastName;
#Column(unique = true)
private String username;
private String password;
#Enumerated(EnumType.STRING)
private UserType userType;
#OneToMany(cascade=CascadeType.ALL, mappedBy="joinedUserAchievementId.user")
private List<JoinedUserAchievement> joinedUserAchievementList = new ArrayList<JoinedUserAchievement>();
public User() {}
public User(Integer id) {
this.id = id;
}
public User(String username, String firstName, String lastName,
String password, UserType userType) {
this.username = username;
this.firstName = firstName;
this.lastName = lastName;
this.password = password;
this.userType = userType;
}
public List<JoinedUserAchievement> getAllAchievement() {
return joinedUserAchievementList;
}
public void addAchievement(Achievement achievement) {
// Notice a JoinedUserAchievement object
Date dateOfAcquisition = new Date();
JoinedUserAchievement joinedUserAchievement = new JoinedUserAchievement(new JoinedUserAchievement.JoinedUserAchievementId(this, achievement),dateOfAcquisition );
joinedUserAchievement.setAchievementId(achievement.getId());
joinedUserAchievementList.add(joinedUserAchievement);
}
//set and gets
JoinedUserAchievement.java
#Entity
#Table(name="USER_ACHIEVEMENT")
public class JoinedUserAchievement {
public JoinedUserAchievement() {}
public JoinedUserAchievement(JoinedUserAchievementId joinedUserAchievementId, Date dateOfAcquisition) {
this.joinedUserAchievementId = joinedUserAchievementId;
this.dateOfAcquisition = dateOfAcquisition;
}
#ManyToOne(targetEntity = Achievement.class)
#JoinColumn(name="id", insertable=false, updatable=false)
private Integer achievementId;
private Date dateOfAcquisition;
public String getDate() {
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = dateOfAcquisition;
return dateFormat.format(date);
}
public Integer getAchievementId() {
return achievementId;
}
public void setAchievementId(Integer achievementId) {
this.achievementId = achievementId;
}
#EmbeddedId
private JoinedUserAchievementId joinedUserAchievementId;
// required because JoinedUserAchievments contains composite id
#Embeddable
public static class JoinedUserAchievementId implements Serializable {
/**
*
*/
private static final long serialVersionUID = -9180674903145773104L;
#ManyToOne
#JoinColumn(name="USER_ID")
private User user;
#ManyToOne
#JoinColumn(name="ACHIEVEMENT_ID")
private Achievement achievement;
// required no arg constructor
public JoinedUserAchievementId() {}
public JoinedUserAchievementId(User user, Achievement achievement) {
this.user = user;
this.achievement = achievement;
}
public JoinedUserAchievementId(Integer userId, Integer achievementId) {
this(new User(userId), new Achievement(achievementId));
}
public User getUser() {
return user;
}
public Achievement getAchievement() {
return achievement;
}
public void setUser(User user) {
this.user = user;
}
public void setAchievement(Achievement achievement) {
this.achievement = achievement;
}
#Override
public boolean equals(Object instance) {
if (instance == null)
return false;
if (!(instance instanceof JoinedUserAchievementId))
return false;
final JoinedUserAchievementId other = (JoinedUserAchievementId) instance;
if (!(user.getId().equals(other.getUser().getId())))
return false;
if (!(achievement.getId().equals(other.getAchievement().getId())))
return false;
return true;
}
#Override
public int hashCode() {
int hash = 7;
hash = 47 * hash + (this.user != null ? this.user.hashCode() : 0);
hash = 47 * hash + (this.achievement != null ? this.achievement.hashCode() : 0);
return hash;
}
}
}
Achievement.java
#Entity
#Table(name="ACHIEVEMENT")
public class Achievement implements Serializable{
private static final long serialVersionUID = 7747630789725422177L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private Integer points;
public Achievement() {
}
public Achievement(String name, Integer points) {
this.name = name;
this.points = points;
}
public Achievement(Integer id) {
this.id = id;
}
//set and gets
I've also tried to make this relationship bidirectional and it didn't work, so I may be missing something
Also before this I had achievement objects instead of achievementId on joinedUserAchievement, it worked but I think its not what I need, I don't need to get every achievement object always, with only the id is fine.
From the docs:
Relationship mappings defined within an embedded id class are not supported
You should put the ids only in JoinedUserAchievementId, and put User and Achievement associations in JoinedUserAchievement directly like so:
public class JoinedUserAchievementId {
private Long userId;
private Long achievementId;
...
}
public class JoinedUserAchievement {
#EmbeddedId
private JoinedUserAchievementId joinedUserAchievementId;
#ManyToOne
#MapsId("userId")
#JoinColumn(name = "USER_ID")
private User user;
#ManyToOne(optional = false, fetch = LAZY)
#MapsId("achievementId")
#JoinColumn(name = "ACHIEVEMENT_ID")
private Achievement achievement;
//if you absolutely need to map the achievement_id column here as well
//note that it will already be mapped to joinedUserAchievementId.achievementId
#Column(name = "ACHIEVEMENT_ID", insertable=false, updatable=false)
private Long achievementId;
...
}
Remember to update the User.joinedUserAchievementList mapping to mappedBy="user".

Jackson is returning duplicate rows

I am trying to return json from these two entity classes.
Questions.java
#Entity
public class Questions {
#Id
#Column(name = "id")
private int id;
#Column(name = "question")
private String question;
#Column(name = "type")
private String type;
#Column(name = "description")
private String description;
#Column(name = "param")
private String param;
#Column(name = "maxlength")
private int maxlength;
#Column(name = "dependency")
private String dependency;
#OneToMany(mappedBy = "questions",targetEntity = Answers.class, cascade = CascadeType.ALL)
private Set<Answers> answers = new HashSet<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
public int getMaxlength() {
return maxlength;
}
public void setMaxlength(int maxlength) {
this.maxlength = maxlength;
}
public String getDependency() {
return dependency;
}
public Set<Answers> getAnswers() {
return answers;
}
public void setAnswers(Set<Answers> answers) {
this.answers = new HashSet<>(answers);
for(Answers answers1:answers){
answers1.setQuestions(this);
}
}
public void setDependency(String dependency) {
this.dependency = dependency;
}
}
Answers.java
#Entity
public class Answers {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private int id;
#Column(name = "ans_label")
private String ans_label;
#Column(name = "ans_value")
private int ans_value;
#Column(name = "ans_weightage")
private int ans_weightage;
#Column(name = "is_default")
private int is_default;
#ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
#JoinColumn(name = "ques_id", nullable = false)
private Questions questions;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAns_label() {
return ans_label;
}
public void setAns_label(String ans_label) {
this.ans_label = ans_label;
}
public int getAns_value() {
return ans_value;
}
public void setAns_value(int ans_value) {
this.ans_value = ans_value;
}
public int getAns_weightage() {
return ans_weightage;
}
public void setAns_weightage(int ans_weightage) {
this.ans_weightage = ans_weightage;
}
public int getIs_default() {
return is_default;
}
public void setIs_default(int is_default) {
this.is_default = is_default;
}
public Questions getQuestions() {
return questions;
}
public void setQuestions(Questions questions) {
this.questions = questions;
}
}
my controller looks like this.
SaveApiController
#RequestMapping("/getData")
public #ResponseBody List<Questions> getData(){
List<Questions> questionss=saveApiServices.getQuestions();
return questionss;
}
The json result i am currently getting has bunch of repeated values.
[{"id":1,"question":"१. व्यक्तिको पुरा नाम थर?", "type":"input_edittext",
"description":"","param":"smalltext","maxlength":20,"dependency":"",
"answers":
[{"id":0,"ans_label":"मुली","ans_value":1,"ans_weightage":0,"is_default":0,
"questions":{"id":1,"question":"१. व्यक्तिको पुरा नाम थर?",
"type":"input_edittext","description":"","param":"smalltext","maxlength":20
,"dependency":"","answers":[{"id":0,"ans_label":"मुली","ans_value":1,
"ans_weightage":0,"is_default":0,"questions":{"id":1,
"question":"१. व्यक्तिको पुरा नाम थर ?","type":"input_edittext",
"description":"","param":"smalltext","maxlength":20,"dependency":"",
"answers":[{"id":0,"ans_label":"मुली",
"ans_value":1,"ans_weightage":0,"is_default":0,"questions":{"id":1,
"question":"१. व्यक्तिको पुरा नाम थर ?","type":"input_edittext",
"description":"","param":"smalltext","maxlength":20,"dependency":"",
my database has only one row inserted. and on my controller there is only one list of questions found. but whenever json output is thrown it repeats a lot of same rows like in the above json sample.
what might be the problems? if you can't find the complete solution can you please suggest me the reason behind the duplication of the same values in json format?
Jackson is getting in a loop here. Your Questions class has a link to Answers and that class refers back to Questions.
Solution
Mark the questions field or the getter in the Answers class as #JsonIgnored.
You can try #JsonManagedReference and #JsonBackReference annotations
For Jackson to work well, one of the two sides of the relationship should not be serialized, in order to avoid the infite loop that causes your stackoverflow error.
#OneToMany(mappedBy = "questions",targetEntity = Answers.class, cascade = CascadeType.ALL)
#JsonManagedReference
private Set<Answers> answers = new HashSet<>();
#ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
#JoinColumn(name = "ques_id", nullable = false)
#JsonBackReference
private Questions questions;
Or
If not interested in getting some entity data just use #JsonIgnore in any one of the class

How to join Maps

How to join newMap detals in custMap.
Map<String, Customer> custMap= new HashMap<String,Customer>();
Map<String, DoCustomer> newMap= new HashMap<String,DoCustomer>();
for (Map.Entry<String, DoCustomer> cust: newMap.entrySet()) {
custMap.put(cust.getKey(),cust.getValue());
}
public class DoCustomer {
private Long id;
private String custName;
private String description;
private String status;
private List<DoCustomerBranch> doCustomerBranch=new ArrayList<DoCustomerBranch>
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
getter/setters of doCustomerBranch
}
#Entity
#Table(name = "CUSTOMER")
public class Customer implements Serializable{
private static final long serialVersionUID = 1L;
private Long id;
private String custName;
private String description;
private String createdBy;
private Date createdOn;
private String updatedBy;
private Date updatedOn;
private Set<CustomerBranch> customerBranch=new HashSet<CustomerBranch>
#Id
#GeneratedValue(generator = "CUSTOMER_SEQ")
#SequenceGenerator(name = "CUSTOMER_SEQ", sequenceName = "CUSTOMERN_SEQ", allocationSize = 1)
#Column(name = "ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#Column(name = "CUST_NAME",nullable=false)
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
#Column(name = "DESCRIPTION")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
#Column(name = "CREATED_BY", length = 50)
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "CREATED_ON")
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
#Column(name = "UPDATED_BY", length = 50)
public String getUpdatedBy() {
return updatedBy;
}
public void setUpdatedBy(String updatedBy) {
this.updatedBy = updatedBy;
}
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "UPDATED_ON")
public Date getUpdatedOn() {
return updatedOn;
}
public void setUpdatedOn(Date updatedOn) {
this.updatedOn = updatedOn;
}
#OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REMOVE }, fetch = FetchType.LAZY, mappedBy = "customer")
public Set<CustomerBranch> getCustomerBranch() {
return customerBranch;
}
public void setCustomerBranch(Set<CustomerBranch> customerBranch) {
this.customerBranch = customerBranch;
}
}
CustomerBranch
#Entity
#Table(name = "CUSTOMER_BRANCH")
public class CustomerBranch implements Serializable{
#Id
#GeneratedValue(generator = "CUSTOMER_BRANCH_SEQ")
#SequenceGenerator(name = "CUSTOMER_BRANCH_SEQ", sequenceName = "CUSTOMER_BRANCH_SEQ", allocationSize = 1)
#Column(name = "ID")
private Long id;
private String branchName;
private String branchAddress;
private Customer customer;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#Column(name = "BRANCH_NAME",nullable=false)
public String getBranchName() {
return branchName;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "MOBEE_CUSTOMER")
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
The problem with your code is that you want to put a DoCustomer in a Customer container. It only works if DoCustomer is a subclass of Customer.
Edit 1: You could use BeanUtils to convert a DoCustomer into a Customer. Here is a good tutorial.
Do you mean:
custMap.putAll(newMap)
As everyone else has pointed out, we need to know what DoCustomer is to be able to help.
But, from what you have given us, I'd suggest casting each DoCustomer to a Customer or, more correctly, making a new Customer from the fields of each DoCustomer.
Something like:
custMap.put(cust.getKey(), new Customer(cust.getValue().getId(), cust.getValue().getCustName(), and so on..));
inside your for loop.
I can see the customer class defined you have provided doesn't have a constructor, so naturally you would have to add one to it

Categories