It is my first experience with JPA-Specification.
I tried to implement a sample project with same requirements of my real project.
Here Are my Entities: Movie and Actor
#Entity
#Table(name = "MOVIE")
public class Movie {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
private Long id;
#Column(name = "TITLE")
#Basic
private String title;
#Column(name = "GENRE")
#Basic
private String genre;
#Column(name = "RATING")
#Basic
private double rating;
#Column(name = "WATCH_TIME")
#Basic
private double watchTime;
#Column(name = "RELEASE_YEAR")
#Basic
private int releaseYear;
}
#Entity
#Table(name = "ACTOR")
public class Actor {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
private Long id;
#Column(name = "NAME")
#Basic
private String name;
#Column(name = "FAMILY")
#Basic
private String family;
#ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinColumn(name = "MOVIE_ID")
#Fetch(FetchMode.JOIN)
private Movie movie;
#Basic
#Column(name = "DATE_TIME")
private Timestamp dateTime;
}
Also I have their repositories which extends JpaSpecificationExecutor<>
And my ActorSpecification is as below:
public class ActorSpecification implements Specification<Actor> {
private List<SearchCriteria> list;
public ActorSpecification() {
this.list = new ArrayList<>();
}
public void add(SearchCriteria criteria) {
list.add(criteria);
}
#Override
public Predicate toPredicate(Root<Actor> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
//create a new predicate list
List<Predicate> predicates = new ArrayList<>();
//add criteria to predicates
for (SearchCriteria criteria : list) {
Join<Actor, Movie> join = root.join(Actor_.MOVIE);
query.orderBy(builder.desc(root.get(Actor_.DATE_TIME)));
if (criteria.getOperation().equals(SearchOperation.IN_MOVIE_ID)) {
predicates.add(join.get(Movie_.ID).in(Arrays.asList(72, 74, 76, 78)));
} else if (criteria.getOperation().equals(SearchOperation.IN_MOVIE_WATCHTIME)) {
predicates.add(join.get(Movie_.WATCH_TIME).in(Arrays.asList(135, 126)));
}
}
return builder.and(predicates.toArray(new Predicate[0]));
}
}
And this is the way I use to filter my data and fetch data:
ActorSpecification actorsInMovieIdList = new ActorSpecification();
actorsInMovieIdList.add(new SearchCriteria("MovieId", "72, 74, 76, 78", SearchOperation.IN_MOVIE_ID));
List<Actor> actorsMovieIdList = actorRepository.findAll(actorsInMovieIdList);
ActorSpecification actorsInMovieWatchTime = new ActorSpecification();
actorsInMovieWatchTime.add(new SearchCriteria("MovieWatchTime", "135 ,126", SearchOperation.IN_MOVIE_WATCHTIME));
List<Actor> actorsMoviesWatchTime = actorRepository.findAll(actorsInMovieIdList.and(actorsInMovieWatchTime));
AND NOW MY REQUIREMENT:
As we have many Actor in each Movie, so the join result will return list of Actors of each movie that matches our conditions for filtering movies.
Now I need to just return the Actor of that movie which has the greatest DATE_TIME
,is there any way for doing it with JpaSpecification or I need to implement a filter method myself.
If I want to tell you about my real project in order to make it more clear.
I have STOCK and DAILY_PRICE Tables and of course any Stock has many Daily_Price, So I just want to fetch the last Daily_price joining my Stock record.
Can anyone help me in this issue??
Any help will be appreciated!!
Related
I'm new to Spring and I'm probably making the dumbest mistake, but I can't solve this problem for more than 2 hours. According to the video tutorial, I did Pagination, I did it exactly like his, but he did not have relationships between entities. I think the error is in a one-to-one relationship between Author and Book entity. Can you please help?
I wanted to add pagination because I have more than a million records in my table, after adding pagination I got this error.
Book Entity:
#Entity
#Table(name = "books")
public class Book {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long id;
#Column(name = "title")
private String title;
#Column(name = "publishing_year")
private Integer publishingYear;
#Column(name = "sell_cost")
private BigDecimal sellCost;
#Column(name = "rent_cost")
private BigDecimal rentCost;
#Column(name = "amount")
private Integer amount;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "author_id")
private Author author;
//getter and setters
Author Entity:
#Entity
#Table(name = "author")
public class Author {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long id;
#Column(name = "fullname")
private String fullname;
#Column(name = "birthday")
private Date birthday;
#OneToOne(mappedBy = "author")
private Book book;
//getters and setters
BookServiceImpl class:
#Service
public class BookServiceImpl implements BookService
{
#Autowired
private BookRepository bookRepository;
#Override
public Page<Book> findPaginated(int pageN, int pageSize,String sortField,String sortDirection) {
Sort sort = sortDirection.equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(sortField).ascending() :
Sort.by(sortField).descending();
Pageable pageable = PageRequest.of(pageN-1,pageSize,sort);
return bookRepository.findAll(pageable);
}
}
LibrarianController class:
#GetMapping("/books/{pageN}")
public String getAllBooks(#PathVariable (value = "pageN") int pageN,Model model,
#RequestParam("sortField") String sortField,
#RequestParam("sortDir") String sortDir){
int pageSize = 25;
Page<Book> page = bookService.findPaginated(pageN,pageSize,sortField,sortDir);
List<Book> books = page.getContent();
model.addAttribute("currentPage",pageN);
model.addAttribute("totalPages",page.getTotalPages());
model.addAttribute("totalItems", page.getTotalElements());
model.addAttribute("books",books);
model.addAttribute("sortField",sortField);
model.addAttribute("sortDir",sortDir);
model.addAttribute("reverseSortDir",sortDir.equals("asc") ? "desc" : "asc");
return "librarian/show-all-books";
}
It seems that you have a Book record that refers to an Author with id 10000001 that does not exit in Author table.
Try these changes. I hope that each book has only one author.
Book.java:
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "author_id")
private Author author;
Author.java:
#OneToMany(mappedBy = "author")
private List<Book> books=new ArrayList<>();
Problem
I am trying to store an object in my Postgres database. This consists of the Order.class, (List) OrderDevice.class, and a Department.class.
The important thing is that the OrderDevices are always stored new in the DB, but a Department may already exist.
When I try to save the object to my database using save I get the following error message: (shown below)
I get the error message "detached entity passed to persist: com.niclas.model.OrderDevice" if the department does not exist yet, if the department exists the error message looks like this: "detached entity passed to persist: com.niclas.model.Department".
Solution attempts
This solution cannot be used because I do not use bidirectional mapping.
(I don't want to use a bidirectional mapping because I want to access the departments without an order.)
I also tried to change the Cascade types to MERGE like in this solution
I also tried using #Transactional on the method
I also tried to save the children in the database first and then the parent like this:
departmentRepository.save(order.getDepartment()); orderDeviceRepository.saveAll(order.getDevices()); orderRepository.save(order);
I hope I have described my good enough and I am happy about suggestions for solutions
Error.log
The log can be viewed here. (The formatting did not work here)
Order.class
#Entity
#Table(name = "orders")
public class Order extends AuditModel {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.AUTO) //TODO better config for GenerationType
private long id;
#Column(name = "order_id")
private String orderId;
#Column(name = "department_id")
private long departmentId;
#OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
#JoinColumn(name = "department", referencedColumnName = "id")
private Department department;
#JsonProperty("deviceList")
#OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
#JoinColumn(name = "order_id", referencedColumnName = "order_id")
private List<OrderDevice> devices;
#JsonProperty("forename")
#Column(name = "sender_forename")
private String senderForename;
#JsonProperty("surname")
#Column(name = "sender_surname")
private String senderSurname;
#Column(name = "notes", columnDefinition = "TEXT")
private String notes;
#Column(name = "month")
private int month;
#Column(name = "year")
private int year;
public Order() {
}
... Getter/Setters
}
OrderDevice.class
#Entity
#Table(name = "order_devices")
public class OrderDevice extends AuditModel{
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.AUTO) //TODO better config for GenerationType
private long id;
#Column( name = "order_id", insertable = false, updatable = false )
private String orderId;
#Column(name = "device_id")
private long deviceId;
#Column(name = "device_name")
private String deviceName;
#Column(name = "priceName")
private String priceName;
#Column(name = "price")
private double price;
#Column(name = "count")
private int count;
public OrderDevice() {
}
... Getters/Setters
}
Department.class
#Entity
#Table(name = "departments")
public class Department {
//TODO add Form Validation
//TODO better Naming for From Attributes on Frontend and Backend
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.AUTO) //TODO better config for GenerationType
private long id;
#Column(name = "department_name")
private String department;
#Column(name = "contact_person_forename")
private String forename;
#Column(name = "contact_person_surname")
private String surname;
#Column(name = "contact_person_mail")
private String mail;
#Column(name = "street")
private String street;
#Column(name = "house_number")
private String houseNumber;
#Column(name = "location")
private String location;
#Column(name = "postal_code")
private int postalCode;
#Column(name = "country")
private String country;
#Column(name = "auto_send_invoice")
private boolean autoSend;
#Column(name = "registered")
private boolean registered;
public Department() {
}
... Getter/Setters
}
OrderController.class
#Slf4j
#RestController
public class OrderController {
private final DepartmentRepository departmentRepository;
private final OrderRepository orderRepository;
private final OrderDeviceRepository orderDeviceRepository;
public OrderController(OrderRepository orderRepository, DepartmentRepository departmentRepository,
OrderDeviceRepository orderDeviceRepository) {
this.orderRepository = orderRepository;
this.departmentRepository = departmentRepository;
this.orderDeviceRepository = orderDeviceRepository;
}
#PostMapping("/orders/add")
public ResponseEntity<Order> addDepartment(#RequestBody Order order) throws JsonProcessingException {
order.setOrderId(order.generateOrderId());
DateTime dateTime = new DateTime();
order.setMonth(dateTime.getMonthOfYear());
order.setYear(dateTime.getYear());
order.getDevices().forEach(orderDevice -> {
orderDevice.setOrderId(order.getOrderId());
});
//departmentRepository.save(order.getDepartment());
//orderDeviceRepository.saveAll(order.getDevices());
orderRepository.save(order);
return new ResponseEntity<>(order, HttpStatus.CREATED);
}
Update
If the objects are created in this way, no error will occur and the order will be successfully saved in the database.
However, I don't understand why it works this way and not via ObjectMapper. Does anyone know why?
#PostMapping("/orders/add")
public ResponseEntity<Order> addDepartment(#RequestBody JsonNode jsonNode) throws JsonProcessingException {
Order order = new Order();
JsonNode departmentJson = jsonNode.get("department");
Department department;
if ( departmentJson.get("id").canConvertToInt() ) {
department = departmentRepository.findDepartmentById(departmentJson.get("id").asInt());
} else {
department = new Department();
department.setDepartment(departmentJson.get("department").asText());
department.setForename(departmentJson.get("forename").asText());
department.setSurname(departmentJson.get("surname").asText());
department.setMail(departmentJson.get("mail").asText());
department.setStreet(departmentJson.get("street").asText());
department.setHouseNumber(departmentJson.get("houseNumber").asText());
department.setLocation(departmentJson.get("location").asText());
department.setPostalCode(departmentJson.get("postalCode").asInt());
department.setCountry(departmentJson.get("country").asText());
department.setAutoSend(departmentJson.get("autoSend").asBoolean());
department.setRegistered(departmentJson.get("registered").asBoolean());
}
order.setDepartment(department);
order.setOrderId(order.generateOrderId());
order.setDepartmentId(department.getId());
List<OrderDevice> orderDevices = new ArrayList<>();
JsonNode devices = jsonNode.get("deviceList");
for (JsonNode node : devices) {
//TODO replace this mess with objectMapper
if (node.has("count") && node.get("count").asInt() != 0){
OrderDevice device = new OrderDevice();
device.setOrderId(order.getOrderId());
device.setDeviceId(node.get("id").asLong());
device.setDeviceName(node.get("deviceName").asText());
device.setPriceName(node.get("priceName").asText());
device.setPrice(node.get("price").asDouble());
device.setCount(node.get("count").asInt());
orderDevices.add(device);
}
}
order.setDevices(orderDevices);
order.setSenderForename(jsonNode.get("forename").asText());
order.setSenderSurname(jsonNode.get("surname").asText());
order.setNotes(jsonNode.get("notes").asText());
DateTime dateTime = new DateTime();
order.setMonth(dateTime.getMonthOfYear());
order.setYear(dateTime.getYear());
orderRepository.save(order);
return new ResponseEntity<>(order, HttpStatus.CREATED);
}
You can try to use instead of orderRepository.save(order) use orderRespostiory.saveOrUpdate(order).
I have 3 entities :
#Entity
#Table(name = "copy")
public class Copy {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column
private Long id;
#Column(name = "format")
private String format;
#Column(name = "status")
private String status;
#ManyToOne
#JoinColumn(name = "book_id")
private Book book;
#ManyToOne
#JoinColumn(name = "library_id")
private Library library;
#Entity
#Table(name = "book")
public class Book implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column
private Long id;
#Column(name = "title")
private String title;
#Column(name = "pub_date")
private Date pubDate;
#Column(name = "page")
private int page;
#Column(name = "synopsis")
private String synopsis;
//TODO Image à gérer
#Column(name = "cover")
private String cover;
#ManyToOne
#JoinColumn(name = "categorie_id")
private Categorie categorie;
#ManyToOne
#JoinColumn(name = "author_id")
private Author author;
#OneToMany(mappedBy = "book")
List<Copy> copyList = new ArrayList<>();
#Entity
#Table(name = "library")
public class Library implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column
private Long id;
#Column(name = "nom")
private String nom;
#Column(name = "adress")
private String adress;
#Column(name = "phone_num")
private String phoneNum;
#Column(name = "email")
private String email;
#OneToMany(mappedBy = "library")
private List<Copy> copyList = new ArrayList<>();
I would like to recover the number of copies of a book according to its format and its libraries. However I cannot figure out how to retrieve a list of copies and the total number depending on the format and its library. How can I do. I wrote this request but I can't get what I want.
My request :
#Query("SELECT DISTINCT c, COUNT(c.format) FROM Copy c WHERE c.book.id = :id")
List<Copy> getCopyById(#Param("id") Long id);
first you need to create class to handel query result (copy,total)
public class CopyWithTotal{
Copy c;
int total;
CopyWithTotal(Copy c, int total){
this.c = c;
this.total = total;
}
}
then you should constratc this class in the query
#Query("SELECT new packgeTo.CopyWithTotal(DISTINCT c, COUNT(c.format)) FROM Copy c WHERE c.book.id = :id group by c")
List<CopyWithTotal> getCopyById(#Param("id") Long id);
whenever you use aggregation function like count all selected column shoud apper in the group by
I have the following entities:
Person.java
#Table(name = persons)
public class Person {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "UserID", nullable = false)
private Long userId;
#Column(name = "Employeenumber", nullable = false) private String employeeNumber;
#Column(name = "Firstname", nullable = false) private String firstName;
#Column(name = "Lastname", nullable = false) private String lastName;
public User() { }
public User(String employeeNumber, String firstName, String lastName) {
super();
this.employeeNumber = employeeNumber;
this.firstName = firstName;
this.lastName = lastName;
}
/*
getter and setters
...
*/
}
Personhistory.java
#Entity
#Table(name = personhistory)
public class Personhistory {
#Id
#Column(name = "UserID", nullable = false)
private Long userId;
#Column(name = "Fromdate", nullable = false) private Date fromDate;
#Column(name = "Todate", nullable = false) private Date toDate;
#Column(name = "TeamID", nullable = false) private Integer teamId;
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "UnikId", nullable = false)
private Integer unikId;
public Userhistory() {
}
public Userhistory(Long userId, Date fromDate, Date toDate, int teamId) {
super();
this.userId = userId;
this.fromDate = fromDate;
this.toDate = toDate;
this.teamId = teamId;
}
/*
Getters and setters
...
*/
}
Team.java
#Entity
#Table(name = "team")
public class Team {
#Id
#Column(name = "TeamID")
#GeneratedValue(strategy = GenerationType.AUTO)
private int teamId;
#Column(name = "TeamNumber") private String teamNumber;
public Team() {}
public Team(String teamNumber) {
super();
this.teamNumber = teamNumber;
}
/*
Getters and setters
...
*/
}
I want to make a API call like this:
localhost:8080/users/{employee}
And get back an object containing the person (His emp-number, firstname and lastname), when he was at the team and what team that is.
If I were to write this query in MSSQL, it would look like this:
select * from persons p
join personhistory ph on ph.UserID = p.UserID
and ph.Fromdate <= cast(getdate() as date)
and ph.Todate >= cast(getdate() as date)
join team t on t.TeamID = ph.TeamID
where u.Employeenumber = '999'
I have searched around for different solutions like HQL, JPQL, Criteria and so on, but I'm unable to make it work.
Any help would be much appreciated.
AFAIK Hibernate 5.1 provides more generic joins but with prior versions you'd either have to use a cross join and add the conditions in the where-clause or provide a real relation between the entities and join on that relation (using the "with" keyword for additional join conditions).
Example (note that I left out many annotations for simplicity):
class Person {
#OneToMany( mappedBy = "user" )
Collection<Personhistory> history;
...
}
class Personhistory {
#ManyToOne
Person user;
#ManyToOne
Team team;
...
}
Then the query could become
select p, ph, t from Person p
join p.history ph with ph.fromdate <= :date and ph.toDate >= :date
join ph.team t
where p.employeeNumber = :number
I have a following error:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`spindledb`.`section`, CONSTRAINT `FK_ftoru9cp83n512p9is8x3vo53` FOREIGN KEY (`scenario_id`) REFERENCES `scenario` (`scenario_id`))
Here are my classes:
Scenario:
#Entity
#Table(name = "scenario")
public class Scenario {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "scenario_id")
private int id;
#Column(name = "title", nullable = false)
private String title;
#NotNull
#DateTimeFormat(pattern = "dd/MM/yyyy")
#Column(name = "creation_date", nullable = false)
#Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
private LocalDate creationDate;
#ManyToOne
#LazyCollection(LazyCollectionOption.FALSE)
#JoinColumn(name = "id", nullable = false)
private User user;
#OneToMany(mappedBy = "scenario", orphanRemoval = true)
#LazyCollection(LazyCollectionOption.FALSE)
private Set<Plot> plotList = new HashSet<Plot>();
#OneToMany(mappedBy = "scenario", orphanRemoval = true)
#LazyCollection(LazyCollectionOption.FALSE)
private Set<Character> characterList = new HashSet<Character>();
#OneToMany(mappedBy = "scenario", cascade=CascadeType.ALL, orphanRemoval = true)
#LazyCollection(LazyCollectionOption.FALSE)
#OrderBy("sequence ASC")
private Set<Section> sectionList = new HashSet<Section>();
Section:
#Entity
#Table(name = "section")
public class Section {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "section_id")
private int id;
#Size(min = 4, max = 50)
#NotNull
#Column(name = "name")
private String name;
#NotNull
#Column(name = "type")
private String type = SectionType.TEXT.getSectionType();
#Column(name = "visibility")
private boolean visibility;
#NotNull
#Column(name = "sequence")
private int sequence;
#ManyToOne (cascade=CascadeType.ALL)
#LazyCollection(LazyCollectionOption.FALSE)
#JoinColumn(name = "scenario_id", nullable = false)
private Scenario scenario;
Controller:
#RequestMapping(value = { "/delete-{id}-scenario" }, method = RequestMethod.GET)
public String deleteScenario(#PathVariable int id) {
scenarioService.deleteScenarioById(id);
return "redirect:/home";
}
Scenario service:
#Service("scenarioService")
#Transactional
public class ScenarioServiceImpl implements ScenarioService {
#Autowired
private ScenarioDao dao;
#Override
public Scenario findById(int id) {
return dao.findById(id);
}
#Override
public void saveScenario(Scenario scenario) {
dao.saveScenario(scenario);
}
public void updateScenario(Scenario scenario) {
Scenario entity = dao.findById(scenario.getId());
if(entity!=null){
entity.setTitle(scenario.getTitle());
entity.setCreationDate(scenario.getCreationDate());
}
}
#Override
public void deleteScenarioById(int id) {
dao.deleteScenarioById(id);
}
Dao
#Repository("scenarioDao")
public class ScenarioDaoImpl extends AbstractDao<Integer, Scenario> implements ScenarioDao {
#Override
public Scenario findById(int id) {
return getByKey(id);
}
#Override
public void saveScenario(Scenario scenario) {
persist(scenario);
}
#Override
public void deleteScenarioById(int id) {
Query query = getSession().createSQLQuery("delete from scenario where id = :id");
query.setString("id", ""+id);
query.executeUpdate();
}
I understand that the problem is that there may be a Section that can not exist without scenario. Right now however section table in database is empty and I still can't remove Scenario. Thanks for advice
Deleting an entity via Query would bypass any Cascade settings you put via annotation.
I would suggest find the entity first by id, then delete the entity object:
Object scenario = session.load(Scenario.class, id);
if (scenario != null) {
session.delete(scenario);
}
use cascade=CascadeType.ALL with all #ManyToOne relations in class Scenario because if you are going to delete any Scenario from database it must not be referenced any where in data base.
the other way to delete is.
Serializable id = new Long(1); //your id
Object persistentInstance = session.load(Scenario.class, id);
if (persistentInstance != null) {
session.delete(persistentInstance);
}