Not able to save ManyToMany self-reference relationship in hibernate - java

I have a ManyToMany relationship between person that I'm trying to describe with Hibernate annotations. I have also created a test for this but the problem is that the relationship isn't saved. Please help me find where I did wrong!
Entity:
#Entity(name = "person")
#Table(appliesTo = "person", indexes = {
#org.hibernate.annotations.Index(name = "ix_uuid", columnNames = {"uuid"}),
#org.hibernate.annotations.Index(name = "ix_facebookId", columnNames = {"facebookId"})
})
public class Person implements Serializable {
#Id
#GeneratedValue(strategy = AUTO)
private Long id;
private String uuid;
private String firstName;
private String lastName;
private String facebookId;
private String email;
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(
name = "person_friend",
joinColumns = #JoinColumn(name = "person_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(name = "friend_id", referencedColumnName = "id")
)
private Set<Person> persons = new HashSet<Person>();
#ManyToMany(mappedBy = "persons", cascade = CascadeType.ALL)
private Set<Person> friends = new HashSet<Person>();
public Person(String uuid, String firstName, String lastName, String facebookId, String email) {
this.uuid = uuid;
this.firstName = firstName;
this.lastName = lastName;
this.facebookId = facebookId;
this.email = email;
}
public Person() {
// Hibernate
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
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 getFacebookId() {
return facebookId;
}
public void setFacebookId(String facebookId) {
this.facebookId = facebookId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void addFriend(Person person){
if(!getFriends().contains(person)){
getFriends().add(person);
person.getPersons().add(this);
}
}
public void becomeFriendOf(Person person) {
if(!getPersons().contains(person)){
getPersons().add(person);
person.getFriends().add(this);
}
}
public Set<Person> getFriends() {
return friends;
}
public void setFriends(Set<Person> friends) {
this.friends = friends;
}
public Set<Person> getPersons() {
return persons;
}
public void setPersons(Set<Person> persons) {
this.persons = persons;
}
#Override
public String toString() {
return "Person{" +
"id=" + id +
", uuid='" + uuid + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", facebookId='" + facebookId + '\'' +
", email='" + email + '\'' +
'}';
}
}
PersonDao:
#Repository("personDao")
public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {
#Autowired
public PersonDaoImpl(SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
}
#Override
public void save(Person person) {
getSession().save(person);
}
#Override
public Person getPerson(Long id) {
return (Person) getSession().get(Person.class, id);
}
#Override
public void saveFriendship(Friendship friendship) {
getSession().save(friendship);
}
}
Test:
#Test(groups = {"integration"})
#ContextConfiguration(locations = {"classpath:applicationContext-test.xml" })
public class PersonDaoImplTest extends AbstractTestNGSpringContextTests {
#Autowired
private PersonDao target;
#Test
public void loadDatabaseWithSomeInitialValues(){
System.out.println(applicationContext.toString());
Person person = new Person("12345abcde","test","test2","test.test", "test.test#gmail.com");
Person person2 = new Person("4567abcde","fest","fest","fest.fest", "fest.fest#gmail.com");
Person person3 = new Person("89105abcde","best","best","best.best", "best.best#gmail.com");
person.addFriend(person2);
person.addFriend(person3);
person2.becomeFriendOf(person);
person3.becomeFriendOf(person);
target.save(person);
}
}
}
As stated the persons are saved in the person table but not the relationship in the person_friend table. Why?

Perhaps it has something to do with transaction boundaries. Try to make your test method #Transactional.

Related

Even after using #AttributeOverrides to change attribute names, I am still getting Repeated Column error

package com.hashedin.employeemanagementsystem.entities;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.util.Date;
import java.util.List;
#Data
#Table(name="employee")
#Entity(name="User")
public class User{
public User(){
this.individual = new Individual();
this.workExperience = null;
this.experienceInMonths = null;
this.jobRole = null;
this.primaryContact = null;
this.secondaryContact = null;
this.employeeID = null;
this.joiningDate = null;
this.employeeCategory = null;
this.exitDate = null;
this.educationDetails = null;
}
#Id
//Employee 4 digit ID
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="employee_id")
private Long empID;
//employee ID Code
#Column(name = "employee_code_id")
private String employeeID;
#Embedded
#AttributeOverrides({
#AttributeOverride(name="first_name", column = #Column(name="employee_first_name")),
#AttributeOverride(name="last_name", column = #Column(name="employee_last_name")),
#AttributeOverride(name="email", column = #Column(name="employee_email")),
#AttributeOverride(name="phone", column = #Column(name="employee_phone")),
})
private Individual individual;
//Primary and Secondary Contacts
#Embedded
#AttributeOverrides({
#AttributeOverride(name = "email", column = #Column(name = "primary_contact_email")),
#AttributeOverride(name = "Individual.first_name", column = #Column(name = "primary_contact_first_name")),
#AttributeOverride(name = "last_name", column = #Column(name = "primary_contact_last_name")),
#AttributeOverride(name = "phone", column = #Column(name = "primary_contact_phone"))
})
private Individual primaryContact;
#Embedded
#AttributeOverrides({
#AttributeOverride(name="email", column = #Column(name="secondary_contact_email")),
#AttributeOverride(name="first_name", column=#Column(name="secondary_contact_first_name")),
#AttributeOverride(name="last_name", column=#Column(name="secondary_contact_last_name")),
#AttributeOverride(name="phone", column=#Column(name="secondary_contact_phone"))
})
private Individual secondaryContact;
//jobRole
#Embedded
private JobRole jobRole;
//joining date and exit date (mentioned for Fixed Employees / probation period)
#Column(name="joining_date")
private Date joiningDate;
#Column(name="exit_date")
private Date exitDate;
//category, experience and highest degree
#OneToOne(cascade = CascadeType.ALL)
private Education educationDetails;
#Column(name="employee_category")
private String employeeCategory;
#Column(name="total_experience")
private Integer experienceInMonths;
#OneToMany(cascade = CascadeType.ALL)
#JoinColumn(name="employee_id")
private List<Employment> workExperience;
public User(String firstName, String lastName, Location location, String email, String phone, String employeeID, Individual primaryContact, Individual secondaryContact, Date joiningDate, String employeeCategory, Date exitDate, Integer experience, Education educationDetails, JobRole jobRole, List<Employment> exp) {
this.individual = new Individual(firstName, lastName, location, email, phone);
this.workExperience = exp;
this.experienceInMonths = experience;
this.jobRole = jobRole;
this.primaryContact = primaryContact;
this.secondaryContact = secondaryContact;
this.employeeID = employeeID;
this.joiningDate = joiningDate;
this.employeeCategory = employeeCategory;
this.exitDate = exitDate;
this.educationDetails = educationDetails;
}
public Individual getIndividual() {
return individual;
}
public void setIndividual(Individual individual) {
this.individual = individual;
}
public Long getEmpID() {
return empID;
}
public void setEmpID(Long empID) {
this.empID = empID;
}
public Integer getExperienceInMonths() {
return experienceInMonths;
}
public void setExperienceInMonths(Integer experienceInMonths) {
this.experienceInMonths = experienceInMonths;
}
public List<Employment> getWorkExperience() {
return workExperience;
}
public void setWorkExperience(List<Employment> workExperience) {
this.workExperience = workExperience;
}
public JobRole getJobRole() {
return jobRole;
}
public void setJobRole(JobRole jobRole) {
this.jobRole = jobRole;
}
public Education getEducationDetails() {
return educationDetails;
}
public void setEducationDetails(Education educationDetails) {
this.educationDetails = educationDetails;
}
public Integer getExperience() {
return experienceInMonths;
}
public void setExperience(Integer experience) {
this.experienceInMonths = experience;
}
public Date getExitDate() {
return exitDate;
}
public void setExitDate(Date exitDate) {
this.exitDate = exitDate;
}
public String getEmployeeCategory() {
return employeeCategory;
}
public void setEmployeeCategory(String employeeCategory) {
this.employeeCategory = employeeCategory;
}
public String getEmployeeID() {
return employeeID;
}
public void setEmployeeID(String employeeID) {
this.employeeID = employeeID;
}
public Date getJoiningDate() {
return joiningDate;
}
public void setJoiningDate(Date joiningDate) {
this.joiningDate = joiningDate;
}
public Individual getPrimaryContact() {
return primaryContact;
}
public void setPrimaryContact(Individual primaryContact) {
this.primaryContact = primaryContact;
}
public Individual getSecondaryContact() {
return secondaryContact;
}
public void setSecondaryContact(Individual secondaryContact) {
this.secondaryContact = secondaryContact;
}
#Override
public String toString() {
return "User{" +
"empID=" + empID +
", employeeID='" + employeeID + '\'' +
", individual=" + individual.toString() +
", primaryContact=" + primaryContact +
", secondaryContact=" + secondaryContact +
", jobRole=" + jobRole +
", joiningDate=" + joiningDate +
", exitDate=" + exitDate +
", educationDetails=" + educationDetails +
", employeeCategory='" + employeeCategory + '\'' +
", experienceInMonths=" + experienceInMonths +
", workExperience=" + workExperience +
'}';
}
}
Still getting this error, please help:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boo
t/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceU
nit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.hashedin.emplo
yeemanagementsystem.entities.User column: email (should be mapped with insert="false" update="false")
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) ~[spring-beans-5
.3.5.jar:5.3.5]
This is what my Individual class looks like:
package com.hashedin.employeemanagementsystem.entities;
import javax.persistence.*;
import java.lang.annotation.Target;
#Embeddable
public class Individual {
#Column(name="first_name")
private String firstName;
#Column(name="last_name")
private String lastName;
#Embedded
private Location location;
#Column(name="email")
private String email;
#Column(name="phone")
private String phone;
public Individual(){
this.firstName="";
this.lastName="";
this.location=null;
this.email=null;
this.phone=null;
}
public Individual(String firstName, String lastName, Location location, String email, String phone) {
this.firstName = firstName;
this.lastName = lastName;
this.location = location;
this.email = email;
this.phone = phone;
}
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 Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
#Override
public String toString() {
return "Individual{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", location=" + location +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
'}';
}
}

Hibernate doesn't show the values of arraylist correctly [duplicate]

This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 2 years ago.
I am studying onetomany in hibernate I have an Instructor that has more than one course.I am using Instructor and Course class for Annotating my tables with the same names in the database.in the GetInstructorCourseDemo am trying to print the Instructor information with an associated course but inSystem.out.println("Course "+tempinstructor.getCourse()); what I see in the console is Course [MyHibernate.Course#56ccd751, MyHibernate.Course#458544e0] . it seems to me hibernate can read courses from database but it can't show it correctly otherwise have i no idea what can cause this problem
#Entity
#Table(name = "instructor")
public class Instructor {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private int id;
#Column(name ="first_name" )
private String first_name;
#Column(name = "last_name")
private String last_name;
#Column(name = "email")
private String email;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "instructor_detail_id")
private instructor_detail Instructor_detail;
Instructor(){}
#OneToMany(mappedBy = "instructor",cascade = {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,
CascadeType.REFRESH})
private List<Course> course;
public Instructor(String first_name, String last_name, String email) {
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public instructor_detail getInstructor_detail() {
return Instructor_detail;
}
public void setInstructor_detail(instructor_detail instructor_detail) {
Instructor_detail = instructor_detail;
}
public List<Course> getCourse() {
return course ;
}
public void setCourse(List<Course> course) {
this.course = course;
}
public void add(Course tempciurse){
if(course==null){
course=new ArrayList<>();
}
course.add(tempciurse);
tempciurse.setInstructor(this);
}
public class GetInstructorCourseDemo {
public static void main(String[]args) {
// create session factory
SessionFactory factory = new Configuration()
.configure("hibername.cfg.xml")
.addAnnotatedClass(Instructor.class)
.addAnnotatedClass(instructor_detail.class)
.addAnnotatedClass(Course.class)
.buildSessionFactory();
Session session = factory.getCurrentSession();
try {
session.beginTransaction();
int id=2;
Instructor tempinstructor=session.get(Instructor.class,id);
System.out.println("instructor : "+tempinstructor);
System.out.println("Course "+tempinstructor.getCourse());
session.getTransaction().commit();
System.out.println("Done!");
}
finally {
factory.close();
}
}
}
#Entity
#Table(name = "course")
public class Course {
#Id#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private int id;
#Column(name = "title")
private String title;
#ManyToOne(cascade = {CascadeType.DETACH,CascadeType.REFRESH,CascadeType.MERGE,CascadeType.PERSIST})
#JoinColumn(name = "instructor_id")
private Instructor instructor;
Course(){}
public Course(String title) {
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Instructor getInstructor() {
return instructor;
}
public void setInstructor(Instructor instructor) {
this.instructor = instructor;
}
}
It missed toString() implementation in Instructor and Course types.
As you don't override it the default one is used -->
public String toString() {
return getClass().getName() + "#" + Integer.toHexString(hashCode());
}
NB : take care to not use course and instructor references directly in toString implementation causing a circular reference and so a StackOverflow error.
for solving this issue i have to add toString method in Course, Instructor as following
for Instructor
#Override
public String toString() {
return "Instructor{" +
"id=" + id +
", first_name='" + first_name + '\'' +
", last_name='" + last_name + '\'' +
", email='" + email + '\'' +
", Instructor_detail=" + Instructor_detail +
", course=" + course +
'}';
}
for Course
#Override
public String toString() {
return "Course{" +
"id=" + id +
", title='" + title + '\'' +
'}';
}
what was causing this confusion was that I didn't have toString method and I shouldn't have Instructor filed in my toString method

how map join query columns to a class using hibernate?

I tried to create a query that returns different columns from two tables, and I want the query columns to be mapped to the user definition class.
my Student Model :
package com.example.demo.models;
import javax.persistence.*;
import java.util.List;
#Entity
#Table
public class Students {
public Students() {
}
public Students(String firstName, String lastName, String age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
#Column
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#Column
private String firstName;
#Column
private String lastName;
#Column
private String age;
#ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
#JoinTable(name = "Student_Course",
joinColumns = #JoinColumn(name="studentID"),
inverseJoinColumns = #JoinColumn(name="courseID"))
private List<Course> courses;
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 getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
}
my Course Model :
package com.example.demo.models;
import javax.persistence.*;
import java.util.List;
#Entity
#Table
public class Course {
public Course() {
}
public Course(String courseName, String unitCount) {
this.courseName = courseName;
this.unitCount = unitCount;
}
#Column
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#Column(name = "CourseName")
private String courseName;
#Column
private String unitCount;
#ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
#JoinTable(name = "Student_Course",
joinColumns = #JoinColumn(name="courseID"),
inverseJoinColumns = #JoinColumn(name="studentID"))
private List<Students> students;
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "teacherID")
private Teachers teachers;
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getUnitCount() {
return unitCount;
}
public void setUnitCount(String unitCount) {
this.unitCount = unitCount;
}
public List<Students> getStudents() {
return students;
}
public void setStudents(List<Students> students) {
this.students = students;
}
public Teachers getTeachers() {
return teachers;
}
public void setTeachers(Teachers teachers) {
this.teachers = teachers;
}
}
my Query in Service Layer:
#Transactional
public List<StudentInfo> getStudentInfo(){
Session session = sf.openSession();
Query hql = session.createQuery("select std.firstName, std.lastName, c.courseName from Students std join std.courses c");
var data = hql.list();
session.close();
return data;
}
and i want map query columns to this simple class :
package com.example.demo.ViewModels;
public class StudentInfo {
private String firstName;
private String lastName;
private String courseName;
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 getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
}
and in finally..
my controller Class :
#RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView Index() {
List<StudentInfo> data = studentRepository.getAll();
return new ModelAndView("indexView", "data", data);
}
notice : i`m using thymeleaf in this project.
please help me.
thanks.:D
If you use Spring Data JPA you should be able to do it in the repository using the #Query annotation:
#Query(value = "SELECT new com.path.to.StudentInfo(std.firstName, " +
"std.lastName, c.courseName) " +
"FROM Students std join std.courses c"
List<StudentInfo> getAllStudentInfo();
Make sure you have an all-args constructor in StudentInfo though.
If you use Hibernate, it's almost the same:
entityManager.createQuery("SELECT new com.path.to.StudentInfo(std.firstName, " +
"std.lastName, c.courseName) " +
"FROM Students std join std.courses c",
StudentInfo.class)
Edit: I have concerns about whether it's supposed to work when using join, but give it a try regardless.

Get Column name along with JSON response

I have three entity classes, I have written the query which includes join of two tables.
Table: ExpensesCategories
#Entity
#Table(name = "ExpensesCategories")
public class ExpensesCategories {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "category_id", unique = true)
private int categoryId;
#NotNull
private String categoryName;
#NotNull
private String categoryCodeInBankStats;
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public String getCategoryCodeInBankStats() {
return categoryCodeInBankStats;
}
public void setCategoryCodeInBankStats(String categoryCodeInBankStats) {
this.categoryCodeInBankStats = categoryCodeInBankStats;
}
}
Table: Transactions
#Entity
#Table(name = "TransactionHistory")
public class TransactionHistory {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
#Temporal(TemporalType.DATE)
private Date dateOfTransaction;
private String transactionType;
private String refNo;
private Date valueDate;
private double withdrawalAmount;
private double depositAmount;
private double closingBalance;
#ManyToOne
#JoinColumn(name="userDetailsId", referencedColumnName="user_id")
private UserDetails userDetails;
#ManyToOne
#JoinColumn(name="expenseCategoriesId", referencedColumnName="category_id")
private ExpensesCategories expenseCategories;
public TransactionHistory(int userId, Date dateOfTransaction, String transactionType, String refNo, Date valueDate,
double withdrawalAmount, double depositAmount, double closingBalance) {
this.dateOfTransaction = dateOfTransaction;
this.transactionType = transactionType;
this.refNo = refNo;
this.valueDate = valueDate;
this.withdrawalAmount = withdrawalAmount;
this.depositAmount = depositAmount;
this.closingBalance = closingBalance;
}
public TransactionHistory() {
}
public Date getDateOfTransaction() {
return dateOfTransaction;
}
public void setDateOfTransaction(Date date) {
this.dateOfTransaction = date;
}
public String getTransactionType() {
return transactionType;
}
public void setTransactionType(String transactionType) {
this.transactionType = transactionType;
}
public String getRefNo() {
return refNo;
}
public void setRefNo(String refNo) {
this.refNo = refNo;
}
public Date getValueDate() {
return valueDate;
}
public void setValueDate(Date valueDate) {
this.valueDate = valueDate;
}
public double getWithdrawalAmount() {
return withdrawalAmount;
}
public void setWithdrawalAmount(double withdrawalAmount) {
this.withdrawalAmount = withdrawalAmount;
}
public double getDepositAmount() {
return depositAmount;
}
public void setDepositAmount(double depositAmount) {
this.depositAmount = depositAmount;
}
public double getClosingBalance() {
return closingBalance;
}
public void setClosingBalance(double closingBalance) {
this.closingBalance = closingBalance;
}
public UserDetails getUserDetails() {
return userDetails;
}
public void setUserDetails(UserDetails userDetails) {
this.userDetails = userDetails;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public ExpensesCategories getExpenseCategories() {
return expenseCategories;
}
public void setExpenseCategories(ExpensesCategories expenseCategories) {
this.expenseCategories = expenseCategories;
}
}
Table: User Details
#Entity
#Table(name = "Employee")
public class UserDetails {
#Id
#Column(name = "user_id", unique = true)
private int id;
#NotNull
private String firstname;
#NotNull
private String lastname;
#Column(unique = true)
#NotNull
private String emailaddress;
#NotNull
private String role;
public UserDetails(String firstname, String lastname, String emailaddress, String role) {
this.firstname = firstname;
this.lastname = lastname;
this.emailaddress = emailaddress;
this.role = role;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public UserDetails() {
}
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 getEmailaddress() {
return emailaddress;
}
public void setEmailaddress(String emailaddress) {
this.emailaddress = emailaddress;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
#Override
public String toString() {
return "Employee [id=" + id + ", firstname=" + firstname + ", lastname=" + lastname + ", emailaddress="
+ emailaddress + ", role=" + role + "]";
}
I have written query like this in transaction entity.
#Query( nativeQuery=true, value="SELECT a.expense_categories_id, a.Total_withdrawal_Amount, b.category_code_in_bank_stats, b.category_name FROM (SELECT expense_categories_id , SUM(withdrawal_amount) AS Total_withdrawal_Amount FROM transaction_history GROUP BY expense_categories_id) a join expenses_categories b on a.expense_categories_id = b.category_id
")
List<Object[]> getCategorizedExpenses();
My Json Response is like:
[
[
1,
21,
"UPI",
"UPI Payments"
],
[
2,
3733.59,
"POS",
"Shopping"
]
]
But i want json response with column names as well:
[
[
expense_categories_id: 1,
Total_withdrawal_Amount: 21,
category_code_in_bank_stats: "UPI",
category_name: "UPI Payments"
],
[
expense_categories_id: 2,
Total_withdrawal_Amount: 3733.59,
category_code_in_bank_stats: "POS",
category_name: "Shopping"
]
]
Please help me out..
You would need to map the results directly to a POJO class and ad some json config:
1) Define the pojo
public ResultClass implements Serializable{
#JsonProperty("expense_categories_id")
private Integer expenseCategoriesId;
...
public ResultClass(Integer expenseCategoriesId ... // rest params){
this.expenseCategoriesId = expenseCategoriesId;
...
}
}
2) Define the mapping:
#SqlResultSetMapping(
name="myMapping",
classes={
#ConstructorResult(
targetClass=ResultClass.class,
columns={
#ColumnResult(name="expenseCategoriesId"),
#ColumnResult(name="totalWithdrawalAmount")
// further mappings ...
}
)
}
)
3) Define a native query
#NamedNativeQuery(name="TransactionHistory.myQuery"
, query="SELECT new mypackage.ResultClass(a.expense_categories_id as expeneCategoriesId ... ) from ...")
4) Define this method in the CrudRepository without the #Query annotation:
public List<ResultClass> myQuery();
Teh #SqlResultSetMapping and #NamedNativeQuery would need to be defined on one of your mapped entities.
Your native query will give you an object[][] as an result. So, it actually a mxn rows.
So,
I think you should create a class names Response
public class Response{
private Long expense_categories_id;
private Double Total_withdrawal_Amount;
private String category_code_in_bank_stats;
private String category_name;
//getters and setters for all attributes
}
List<Response> fillCategorizedExpenses(){
List<Response> response_List = new ArrayList<>();
Response response = null;
Object[][] // fill each object with by accessing their index from
//this array.
for() //iterate the object array. {
response = new Response();
response.setExpense_categories_id(value); // set all attributes.
....
....
....
response_List.add(response);
}
return response_List; //this will print as you need in your project.
}
Thank You :) Hope this might help you out.

HQL compare existing Objects tables

I just want to explain following scenario.
I have Registration table for employee
It has one field like BranchAddress, and I have using table Branch for that with ManyToOne mapping.
#Entity
#Table(name = "temp_reg")
public class TemporaryRegistrationDTO {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private int ID;
#Column(name = "first_name")
private String firstName;
#Column(name = "last_name")
private String lastName;
#ManyToOne(cascade=CascadeType.ALL)
private BranchDTO companyBranch;
public BranchDTO getCompanyBranch() {
return companyBranch;
}
public void setCompanyBranch(BranchDTO companyBranch) {
this.companyBranch = companyBranch;
}
public int getID() {
return ID;
}
public void setID(int iD) {
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;
}
}
#Entity
#Table(name = "company_branch")
public class BranchDTO {
#Id
#GeneratedValue ( strategy = GenerationType.AUTO)
private int branchID;
public int getBranchID() {
return branchID;
}
public void setBranchID(int branchID) {
this.branchID = branchID;
}
#ManyToOne(cascade=CascadeType.ALL)
private CountriesDTO country;
#ManyToOne(cascade=CascadeType.ALL)
private StatesDTO state;
public CountriesDTO getCountry() {
return country;
}
public void setCountry(CountriesDTO country) {
this.country = country;
}
public StatesDTO getState() {
return state;
}
public void setState(StatesDTO state) {
this.state = state;
}
}
#Entity
#Table(name = "company_countries")
public class CountriesDTO {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private String countryCode;
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
private String countryName;
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
}
#Entity
#Table(name = "company_states")
public class StatesDTO {
#Id
#GeneratedValue ( strategy = GenerationType.AUTO)
private int state_id;
private String state;
private String stateCode;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getStateCode() {
return stateCode;
}
public void setStateCode(String stateCode) {
this.stateCode = stateCode;
}
#Override
public String toString() {
return "StatesDTO [statesID=" + state_id + ", state=" + state + ", stateCode=" + stateCode + "]";
}
public int getState_id() {
return state_id;
}
public void setState_id(int state_id) {
this.state_id = state_id;
}
}
Now, What I want is that, whenever their is request for registration, Firstly I am checking if Branch Address is available in the Branch table. If it contains an entry already, then it will retrieve Branch row and stooping from same data to Branch Table
Now, to check for BranchDTO, I have created method in Branch Repository class.
#Query("from BranchDTO where country = :country and state = :state")
public BranchDTO existsEntry(#org.springframework.data.repository.query.Param("country") CountriesDTO country,#org.springframework.data.repository.query.Param("state") StatesDTO state);
But It reflects me following error,
object references an unsaved transient instance - save the transient instance before flushing: com.example.demo.pojo.CountriesDTO
Thank you guys

Categories

Resources