Convert PostgreSQL Query into corresponding Hibernate Query - java

I need corresponding Hibernate Query for the PostgreSQL query.
Here is query
select DATE(row_created) from DemoTable
where DATE is inbuilt function in PostgreSQL and row_created is column in DemoTable having data type of timestamp without time zone.
Here is my POJO class
import java.util.ArrayList;
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.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
#Entity
#Table(name = "candidate")
public class Candidates {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.AUTO)
private int candidateId;
#NotNull
#Column(name = "name")
private String name;
#NotNull
#Column(name = "mail_id")
private String mailId;
#NotNull
#Column(name = "mobile_number")
private String mobileNumber;
#Column(name = "experience")
private String experience;
#Column(name = "ctc")
private String ctc;
#Column(name = "company")
private String company;
#NotNull
#Column(name = "notice_period")
private String noticePeriod;
#Column(name = "prefered_job_location")
private String preferedJobLocation;
#Column(name = "resume_filepath_name")
private String resumeFilepathName;
#Column(name = "primary_skill")
private String primarySkill;
#NotNull
#Column(name = "is_active")
private Short isActive;
#NotNull
#Column(name = "row_created")
private Date rowCreated;
#Column(name = "row_altered")
private Date rowAltered;
#NotNull
#ManyToOne
#JoinColumn(name="tracker_fk_id")
private Tracker trackerFk;
}
Can anyone help to get equivalent Hibernate Query for that.
Thanks

You can create a Query like this:
//Select your object that you want
Query query = em.createNamedQuery("SELECT a FROM DemoTable a WHERE mycondition");
1-In case to get single date
//Get result list or a single result like what you want
DemoTableEntity myresult = (DemoTableEntity) query.getSingleResult();
//get the date from this result
Date mydate = myresult.getRowCreated();
2-In case to get liste of date
List<Date> listeDate = new ArrayList<>();
List<DemoTableEntity> list = query.getResultList();
for(DemoTableEntity ent : list){
listeDate.add(ent.getRowCreated());
}
3-In case to get list of result of one column
Query query = em.createNamedQuery("SELECT a.rowAltered FROM Candidates a");
List<Date> dates = query.getResultList();
Hope this can help you.

If you need to retrieve the data, then you can do as "Youcef Laidani" said, so you can trim the time in the field getter method or trim it when needed.
But if you are looking to use that in a where condition then use BETWEEN statement instead of equal
SELECT a FROM DemoTable WHERE a.row_created BETWEEN :from AND :to
where to is the target date +1 day
example values:
from = 2016-11-10 00:00:00
to = 2016-11-11 00:00:00
that means where created_date = 2016-11-10

Related

JPQL #Query for a ManyToOne relationship

Please I need some help. I've got 2 entities:
Appointment.class
#Entity
#Table(name = "appointment")
#Data
#NoArgsConstructor
#AllArgsConstructor
public class Appointment {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(name = "created_date")
private Date createdDate;
#Column(name = "modified_date")
private Date modifiedDate;
#Column(name = "appointment_date")
private LocalDate appointmentDate;
#Column(name = "start_time")
private LocalTime startTime;
private Boolean cancelled;
#ManyToOne
#JoinColumn(nullable = false, name = "client_id")
private Client clientId;
#ManyToOne
#JoinColumn(nullable = false, name = "employee_id")
private Employee employee;
#ManyToOne
#JoinColumn(nullable = false, name = "service_id")
private Service service;
}
And Employee.class
#Entity
#Table(name = "employee")
#NoArgsConstructor
#AllArgsConstructor
public class Employee {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(name = "first_name")
private String firstName;
#Column(name = "last_name")
private String lastName;
#Column(name = "created_date")
private Date createdDate;
#Column(name = " modified_date")
private Date modifiedDate;
#OneToOne
#JoinColumn(name = "service_id", referencedColumnName = "id")
private Service service;
}
I need to get all the appointments that match with the given startTime, appointmentDate and employee
I want to define an abstract method in the interface AppointmentRepo so that in my AppointmentServices.class I can call that method with 3 arguments and get the appointment entity.
AppointmentServices.class
appointmentRepo.getAppointmentByDateAndEmployee(date, employee, scheduledHour);
AppointmentRepo interface
#Repository
public interface AppointmentRepo extends JpaRepository<Appointment, Integer>{
#Query("SELECT a FROM Appointment a INNER JOIN a.employee e WHERE a.appointmentDate = :appointment_date AND e = :employee AND s.startTime = :start_time")
public List<Appointment> getAppointmentByDateAndEmployee (#Param("appointment_date") LocalDate appointmentDate,
#Param("employee_id") Employee employee, #Param("start_time") LocalTime startTime);
}
How I have to set my #Query in order to be given an appointment entity that matches with 3 given arguments (a date, and time and a reference to other entity called Employee)
Am I doing wrong matching the entire object so I need just to use the id of the Employee entity?
Please help me, and thanks for your time!!
Happy Holidays
You can use SQL instead HQL (nativeQuery=true)
DAO Layer
package com.jb.app.repos;
import com.jb.app.beans.Appointment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
#Repository
public interface AppointmentRepository extends JpaRepository<Appointment, Integer> {
#Query(value = "SELECT * FROM APPOINTMENT WHERE appointment_date = ?1 AND start_time = ?2 AND employee_id = ?3", nativeQuery = true)
List<Appointment> getAppointmentByDateAndEmployee(LocalDate appointmentDate, LocalTime startTime, int employeeId);
}
Service Layer
package com.jb.app.services;
import com.jb.app.beans.Appointment;
import com.jb.app.beans.Employee;
import com.jb.app.repos.AppointmentRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
#Service
#RequiredArgsConstructor
public class AppointmentServiceImpl implements AppointmentService{
private final AppointmentRepository appointmentRepository;
#Override
public List<Appointment> getAppointmentByDateAndEmployee(LocalDate appointmentDate, LocalTime startTime, Employee e) {
return appointmentRepository.getAppointmentByDateAndEmployee(appointmentDate,startTime,e.getId());
}
}

getting null value from #Requestbody

I am in the process of adding a DTO layer to a restful api. Before, the program used entity (Recipe and Ingredient) directly and now I added a new DTO layer in between (RecipeDTO IngredientDTO). However, the moment I made the change I started getting Null values from #RequestBody. Each recipe contains a list of Ingredients and it is the list of ingredients that are returning null values, the recipe by itself is returning fine.
The controller looks like this
package com.example.recipes.controller;
import com.example.recipes.DTO.RecipeDTO;
import com.example.recipes.Service.RecipeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/*...*/
#PostMapping(path = "/post")
public void postRecipes(#RequestBody RecipeDTO recipeDTO){
recipeService.postRecipes(recipeDTO);
}
/*...*/
Recipe Entity
package com.example.recipes.Entity;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.List;
#Data
#Entity
#Table(name = "recipe", schema = "public")
#AllArgsConstructor
#NoArgsConstructor
public class Recipe {
#Id
#GeneratedValue(
strategy = GenerationType.IDENTITY
)
#Column(name = "id", updatable = false, nullable = false)
private long id;
#Column(name = "name")
private String name;
#Column(name = "instructions")
private String instructions;
#OneToMany(mappedBy = "recipe")
private List<Ingredient> ingredients;
#JsonProperty("date_added")
private String dateAdded = String.valueOf(LocalDateTime.now());
#JsonProperty("last_edited")
private String lastEdited = String.valueOf(LocalDateTime.now());
}
RecipeDTO
package com.example.recipes.DTO;
import lombok.*;
import javax.persistence.OneToMany;
import java.time.LocalDateTime;
import java.util.List;
#AllArgsConstructor
#NoArgsConstructor
#Getter
#Setter
#ToString
public class RecipeDTO {
private long id;
private String name;
private String instructions;
private List<IngredientDTO> ingredientsDTO;
private String dateAdded = String.valueOf(LocalDateTime.now());
private String lastEdited = String.valueOf(LocalDateTime.now());
public RecipeDTO(long id, String name, String instructions, String dateAdded, String lastEdited) {
this.id = id;
this.name = name;
this.instructions = instructions;
this.dateAdded = dateAdded;
this.lastEdited = lastEdited;
}
}
Ingredient Entity
package com.example.recipes.Entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import javax.persistence.*;
#Data
#Entity
#Table(name = "Ingredient")
#NoArgsConstructor
public class Ingredient {
#Id
#GeneratedValue(
strategy = GenerationType.IDENTITY
)
#JsonProperty("ingredient_id")
private long ingredient_ID;
#JsonProperty("ingredient_name")
private String ingredientName;
#Column(name = "amount")
private int amount;
#Column(name = "unit")
private String unit;
#ManyToOne
#JoinColumn(name = "recipe_id")
#ToString.Exclude
#JsonIgnore
private Recipe recipe;
}
IngredientDTO
package com.example.recipes.DTO;
import lombok.*;
#Data
#AllArgsConstructor
#NoArgsConstructor
public class IngredientDTO {
private long ingredientID;
private String ingredientName;
private int amount;
private String unit;
}
the json i sent
{
"name":"unique2",
"ingredients":[
{
"ingredient_name":"Atlantic",
"amount":13,
"unit":"ton"
},
{
"ingredient_name":"Pacific",
"amount":15,
"unit":"boatload"
},
{
"ingredient_name":"Indian",
"amount":38,
"unit":"trucload"
}
],
"instructions":"easy on the salt"
}
and the #requestbody the ingredientsDTO is null
this is recipe: RecipeDTO(id=0, name=unique2, instructions=easy on the salt, ingredientsDTO=null, dateAdded=2022-08-08T15:04:10.678748100, lastEdited=2022-08-08T15:04:10.678748100)
Edit: I have just tried copying the code from the entity classes and pasting them in the DTO classes and it still returning null...
package com.example.recipes.DTO;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.List;
#Data
#Entity
#Table(name = "recipe", schema = "public")
#AllArgsConstructor
#NoArgsConstructor
public class RecipeDTO {
#Id
#GeneratedValue(
strategy = GenerationType.IDENTITY
)
#Column(name = "id", updatable = false, nullable = false)
private long id;
#Column(name = "name")
private String name;
#Column(name = "instructions")
private String instructions;
#OneToMany(mappedBy = "recipeDTO")
private List<IngredientDTO> ingredientDTOs;
#JsonProperty("date_added")
private String dateAdded = String.valueOf(LocalDateTime.now());
#JsonProperty("last_edited")
private String lastEdited = String.valueOf(LocalDateTime.now());
public RecipeDTO(long id, String name, String instructions, String dateAdded, String lastEdited) {
this.id = id;
this.name = name;
this.instructions = instructions;
this.dateAdded = dateAdded;
this.lastEdited = lastEdited;
}
}
package com.example.recipes.DTO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import javax.persistence.*;
#Data
#Entity
#Table(name = "Ingredient")
#NoArgsConstructor
public class IngredientDTO {
#Id
#GeneratedValue(
strategy = GenerationType.IDENTITY
)
#JsonProperty("ingredient_id")
private long ingredientID;
#JsonProperty("ingredient_name")
private String ingredientName;
#Column(name = "amount")
private int amount;
#Column(name = "unit")
private String unit;
#ManyToOne
#JoinColumn(name = "recipe_id")
#ToString.Exclude
#JsonIgnore
private RecipeDTO recipeDTO;
public IngredientDTO(long ingredientID, String ingredientName, int amount, String unit) {
this.ingredientID = ingredientID;
this.ingredientName = ingredientName;
this.amount = amount;
this.unit = unit;
}
}
#RequestBody
this is recipe: RecipeDTO(id=0, name=unique2, instructions=easy on the salt, ingredientDTOs=null, dateAdded=2022-08-08T15:24:19.325806500, lastEdited=2022-08-08T15:24:19.325806500)
these are the ingredients: null
this is ingredientDTO: null
this is ingredientDTO: null
Edit2: I tried posting only the ingredientDTO and the #RequestBody was able to pick it up just fine
//this is fine
public void testRecipePost(#RequestBody IngredientDTO ingredientDTO) {
System.out.println("ingredientDTO: " + ingredientDTO);
}
You can replace
#OneToMany(mappedBy = "recipeDTO")
private List<IngredientDTO> ingredientDTOs;
to
#OneToMany(mappedBy = "recipeDTO")
private List<IngredientDTO> ingredients;
Or adding
#JsonProperty("ingredients")
Example:
#JsonProperty("ingredients")
#OneToMany(mappedBy = "recipeDTO")
private List<IngredientDTO> ingredientDTOs;
The reason for null is because Jackson doesn't know how to deserialise your fields properly with different names.
In the json, the name is ingredients but, in the DTO, it is ingredientsDTO. Those 2 need to match.
You request
{
"name":"unique2",
"ingredients":[...]
here the name of array you are passing in Json is different in the entity you are using.
Your DTO
#OneToMany(mappedBy = "recipeDTO")
private List<IngredientDTO> ingredientDTOs;
The name of fields in JSON request and Entity must match.
Change the name of field private List<IngredientDTO> ingredientDTOs; to ingredients.

Strange HQL Query Behavior When Fetching Data

I call the method below to calculate some values. I supply the agencyID and the integer representation of the month to carry out the calculation.
Map jan = new HashMap();
String a[] = shifts.getAgencyActiveAgentsByMonth(agencyID, 1).split(",");
jan.put("label", "Jan");
jan.put("active", a[0]);
jan.put("inactive", a[1]);
activeInactiveGraphData.add(jan);
Map feb = new HashMap();
a = shifts.getAgencyActiveAgentsByMonth(agencyID, 2).split(",");
feb.put("label", "Feb");
feb.put("active", a[0]);
feb.put("inactive", a[1]);
activeInactiveGraphData.add(feb);
Etc...till I get to December (value 12). However, the code gives a run time error when I get to May (month value 5). The strange thing however is that this error is thrown
java.sql.SQLException: Column 'shift_dayid' not found.
In my query, I did not include the shift_dayId anywhere in my query, as shown below:
#Query(value = "select distinct userid from shift_days where agencyid = :agencyID and month(created_at) = :givenMonth and shift_status = 1", nativeQuery=true)
List<ShiftDaysModel> getAgencyActiveAgentsCountForMonth(#Param("agencyID") String agencyID, #Param("givenMonth") int givenMonth);
I kindly request help in helping me identify why the givenMonth throws runtime exception when the value is greater than 4.
Thank you
EDIT
Here is my ShiftDaysModel as requested...
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
/**
*
* #author Javalove
*/
#Entity
#Getter
#Setter
#NoArgsConstructor
#Table(name = "shift_days")
public class ShiftDaysModel implements Serializable {
public static enum RequestStatus {
PENDING, ACCEPTED, COMPLETED, REJECTED, TRANSFERED, IN_PROGRESS, VALIDATED;
}
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long shiftDayID;
#Column(name = "created_at", updatable=false)
#CreationTimestamp
#Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date createdAt;
#Column(name = "updated_at")
#UpdateTimestamp
#Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date updatedAt;
#Column(name = "userID")
private String userID;
#Column(name = "agencyID")
private String agencyID;
#Basic
#Column(name = "shiftDate")
private LocalDate shiftDate;
#Basic
#Column(name = "startTime")
private LocalTime startTime;
#Basic
#Column(name = "endTime")
private LocalTime endTime;
#Column(name = "shiftOptionID")
private String shiftOptionID;
#Column(name = "shiftStatus")
private RequestStatus shiftStatus;
#Column(name = "isTransferred")
private boolean isTransferred;
#Column(name = "transferredFromID")
private String transferredFromID;
#Column(name = "shiftID")
private String shiftID;
#Column(name = "shiftRate")
private Double shiftRate;
#Column(name = "siteID")
private String siteID;
#Basic
#Column(name = "actualStartDateTime")
private LocalDateTime actualStartDateTime;
#Basic
#Column(name = "actualEndDateTime")
private LocalDateTime actualEndDateTime;
#Column(name = "shiftRequestGroupID")
private String shiftGroupID = "0";
}
When you use the signature:
#Query(..)
List<ShiftDaysModel> getAgencyActiveAgentsCountForMonth(...);
The expectation is that the select clause will contain all the fields that are necessary to create the entity ShiftDaysModel.
In your case, the query only select the column userid. When Hibernate/Spring tries to covert each row into a ShiftDaysModel, it throws the exception because there is no value for the field corresponding to shift_dayid.
This might work if you change the query to select distinct * from shift_days ...
Or, if you only care about the userid:
#Query(..)
List<String> getAgencyActiveAgentsCountForMonth(...)

How to prevent JPA deleteByID() from deleting parent when deleting a child?

I've got a spring mvc application in which users book meeting rooms. When I try to delete a booking by using deleteById(), the user that belongs to the booking is also deleted. How do I prevent this?
Booking object:
package spe_booker.models;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
#Entity
public class Booking {
#Id
#GeneratedValue
private Long id;
#NotNull
private Date startDateTime;
#NotNull
private Date endDateTime;
#NotNull
#ManyToOne(cascade = CascadeType.ALL)
private Room room;
#NotNull
#ManyToOne(cascade = CascadeType.ALL)
private User user;
private Date creationDate;
public Booking() { }
...getters and setters...
User object:
package spe_booker.models;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
#Entity
#Table(name = "user")
public class User {
#Id
#GeneratedValue
private Long id;
private String username;
String name;
private String password;
private String faculty;
private String role;
private Boolean blacklisted;
#Column(name = "enabled")
public int enabled;
#OneToMany(mappedBy = "user")
private List<Booking> bookings = new ArrayList<>();
...getters and setters...
Booking repository:
package spe_booker.Repositorys;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import spe_booker.models.Booking;
import spe_booker.models.User;
import java.util.List;
#Repository
public interface BookingRepository extends CrudRepository<Booking, Long> {
List<Booking> findAll();
List<Booking> findBookingsByUser(User user);
}
The delete call:
bookingRepository.deleteById(id);
Change the cascade of
#NotNull
#ManyToOne(cascade = CascadeType.ALL)
private User user;
to the appropriate values. Do you change a user and then save a booking and a user at the same time? Do you create bookings with users at the same time? If you can answer these questions with "no", you might just remove (cascade = CascadeType.ALL) at all. And you should ask yourself why the cascades are present in the first place!
CascadeType.ALL forces JPA to cascade all operations (persisting, removing, updating....). That means when you delete/update a booking, it willl delete/update the Room and User objects too

#OneToOne or #ManyToOne on xxx references an unknown entity: java.util.Set

I am creating one feeds table which has one to many mapping with comments and many to one mapping with users table. But when i am annotating the user_table field in my feeds entity as #ManyToOne i am getting error like #OneToOne or #ManyToOne on xxx references an unknown entity: java .util.Set but when i am annotating it with #ManyToMany it is not throwing any error and the table are getting created. Can anyone explain me why
package com.incture.metrodata.entity;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
#Entity
#Getter
#Setter
#ToString
#Table(name = "FEEDS_DETAILS")
public class FeedsDo implements BaseDo {
/**
*
*/
private static final long serialVersionUID = -2035537433672391211L;
#Id
#Column(name = "FEED_ID")
private String feedId;
#Column(name = "BODY")
private String body;
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "CREATED_AT")
private Date createdAt;
#Column(name = "CREATED_BY")
private String createdBy;
#Column(name = "TITLE")
private String title;
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "UPDATED_AT")
private Date updatedAt;
#Column(name = "UPDATED_BY")
private String updatedBy;
#Column(name = "IS_DELETED")
private int isDeleted = 0;
#ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
#JoinTable(name = "FEEDS_USERS", joinColumns = { #JoinColumn(name = "FEED_ID")}, inverseJoinColumns = { #JoinColumn(name = "USER_ID") })
private Set<UserDetailsDo> user = new HashSet<UserDetailsDo>(0);
#OneToMany(targetEntity = CommentsDo.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<CommentsDo> comments;
#Override
public Object getPrimaryKey()
{
return feedId;
}
}
package com.incture.metrodata.entity;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
#Entity
#Table(name = "COMMENTS")
public class CommentsDo implements BaseDo {
/**
*
*/
private static final long serialVersionUID = 5180603287069572120L;
#Id
#Column(name = "COMMENT_ID")
#GeneratedValue(strategy = GenerationType.AUTO)
private long commentId;
#Lob
private String comment;
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "CREATED_AT")
private Date createdAt;
#Column(name = "CREATED_BY")
private String createdBy;
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "UPDATED_AT")
private Date updatedAt;
#Column(name = "IS_DELETED")
private int isDeleted=0;
#Override
public Object getPrimaryKey() {
return commentId;
}
}
package com.incture.metrodata.entity;
import java.util.Date;
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.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Where;
import lombok.Data;
import lombok.ToString;
#Entity
#Data
#ToString
#Table(name = "USER_DETAILS")
#DynamicUpdate(true)
#Where(clause = "DELETE = 0")
public class UserDetailsDo implements BaseDo {
private static final long serialVersionUID = 1L;
#Id
#Column(name = "USER_ID",length=50)
private String userId;
#Column(name = "FIRST_NAME",length=100)
private String firstName;
#Column(name = "LAST_NAME",length=100)
private String lastName;
//#Formula(value = " concat(FIRST_NAME, ' ', LAST_NAME) ")
#Column(name = "NAME",length=100)
private String name;
#ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private RoleDetailsDo role;
#Column(name = "TELEPHONE",length=50)
private String telephone;
#Column(name = "CREATED_BY",length=50)
private String createdBy;
#Column(name = "UPDATED_BY",length=50)
private String updatedBy;
#Column(name = "MOBILE_TOKEN")
#Lob
private String mobileToken;
#Column(name = "WEB_TOKEN")
#Lob
private String webToken;
#Column(name = "LONGITUDE")
private Double longitude;
#Column(name = "LATITUDE")
private Double latitude;
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "CREATED_DATE")
private Date createdDate;
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "CREATED_AT")
private Date createdAt;
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "UPDATED_AT")
private Date updateAt;
#Column(name = "EMAIL",length=100)
private String email;
#Column(name = "PARENT_ID",length=100)
private String parentId;
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "LAST_LOGIN_TIME")
private Date lastLogedIn;
#Column(name = "TRACK_FREQUENCY")
#ColumnDefault("'30'")
private Long trackFreq;
#ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
#JoinTable(name = "USERS_WAREHOUSE_MAPPING", joinColumns = { #JoinColumn(name = "USER_ID") }, inverseJoinColumns = {
#JoinColumn(name = "WARE_HOUSE_ID") })
private Set<WareHouseDetailsDo> wareHouseDetails = new HashSet<WareHouseDetailsDo>(0);
#ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
#JoinTable(name = "USERS_COURIER_MAPPING", joinColumns = { #JoinColumn(name = "USER_ID") }, inverseJoinColumns = {
#JoinColumn(name = "COURIER_ID") })
private Set<CourierDetailsDo> courierDetails = new HashSet<CourierDetailsDo>(0);
#Column(name = "DELETE")
#ColumnDefault("'0'")
private Integer deleted = 0;
public void setDeleted() {
this.deleted = 1;
}
#Override
public Object getPrimaryKey() {
return userId;
}
}
You have a one-directional relationship:
#OneToMany(targetEntity = CommentsDo.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<CommentsDo> comments;
And hibernate doesn't know, which column to use to join those entities. The best would be to add relationship definition on child side. You need to specify #JoinColumn, something like:
#JoinColumn(name = "feeds_do_id")
private FeedsDo feedsDo;
in CommentsDo class. Instead of feeds_do_id there should be a foreign key.

Categories

Resources