Skip to content

Commit

Permalink
completed hw
Browse files Browse the repository at this point in the history
  • Loading branch information
ZinaBudilova committed Jan 15, 2023
1 parent 8ae23e9 commit 3c91fa3
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/main/java/ru/hh/school/dao/EmployerDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public EmployerDao(SessionFactory sessionFactory) {
*/
public Employer getEager(int employerId) {
return getSession()
.createQuery("from Employer employer", Employer.class)
.createQuery("FROM Employer e " +
"LEFT JOIN FETCH e.vacancies" +
"WHERE e.id = :employerId", Employer.class)
.getSingleResult();
}

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/ru/hh/school/dao/GenericDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import org.hibernate.SessionFactory;

import java.io.Serializable;
import java.util.Collection;
import java.util.Objects;

public class GenericDao {
private final SessionFactory sessionFactory;
Expand All @@ -25,7 +23,7 @@ public void save(Object object) {
if (object == null) {
return;
}
getSession().save(object);
getSession().saveOrUpdate(object);
}

protected Session getSession() {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/ru/hh/school/dao/VacancyDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ public StatisticsDto getSalaryStatistics(Area area){
// ToDo дополните запрос, чтобы возвращался ru.hh.school.employers.StatisticsDto
// https://vladmihalcea.com/the-best-way-to-map-a-projection-query-to-a-dto-with-jpa-and-hibernate/
return getSession().createQuery(
"SELECT count(v.id), min(v.compensationFrom), max(v.compensationTo) " +
"FROM Vacancy v WHERE v.area = :area", StatisticsDto.class)
"SELECT NEW ru.hh.school.employers.StatisticsDto(" +
"COUNT(v.id), MIN(v.compensationFrom), MAX(v.compensationTo)" +
") " +
"FROM Vacancy v " +
"WHERE v.area = :area", StatisticsDto.class)
.setParameter("area", area)
.getSingleResult();
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/ru/hh/school/entity/Area.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package ru.hh.school.entity;

import javax.persistence.*;

//TODO: оформите entity
@Entity
@Table(name = "area", schema = "public")
public class Area {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "area_id", nullable = false, updatable = false)
private Integer id;

@Column(name = "name")
private String name;

public String getName() {
Expand Down
28 changes: 21 additions & 7 deletions src/main/java/ru/hh/school/entity/Employer.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
package ru.hh.school.entity;

import org.hibernate.annotations.CreationTimestamp;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

//TODO: оформите entity
@Entity
@Table(name = "employer", schema = "public")
public class Employer {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "area_id", nullable = false, updatable = false)
private Integer id;

@Column(name = "company_name", nullable = false)
private String companyName;

// не используйте java.util.Date
// https://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#basic-datetime-java8
@CreationTimestamp
@Column(name = "creation_time", nullable = false, updatable = false)
private LocalDateTime creationTime;

@OneToMany(mappedBy = "employer",
cascade = {CascadeType.ALL},
orphanRemoval = true)
private List<Vacancy> vacancies = new ArrayList<>();

@Column(name = "block_time")
private LocalDateTime blockTime;

public List<Vacancy> getVacancies() {
Expand Down Expand Up @@ -49,16 +63,16 @@ public void setBlockTime(LocalDateTime blockTime) {
// https://vladmihalcea.com/hibernate-facts-equals-and-hashcode/
// https://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#mapping-model-pojo-equalshashcode
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employer employer = (Employer) o;
return Objects.equals(companyName, employer.companyName);
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
Employer otherEmployer = (Employer) other;
return id.equals(otherEmployer.getId());
}

@Override
public int hashCode() {
return Objects.hash(companyName);
return (31 * id) ^ 2 + 31 * Objects.hash(companyName);
}

}
12 changes: 8 additions & 4 deletions src/main/java/ru/hh/school/entity/Resume.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package ru.hh.school.entity;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.*;

//TODO: оформите entity
@Entity
@Table(name = "resume", schema = "public")
public class Resume {
// TODO: сделать так, чтобы id брался из sequence-а
// таким образом, мы сможем отправлять в бд запросы батчами.
Expand All @@ -15,12 +16,15 @@ public class Resume {
// https://vladmihalcea.com/from-jpa-to-hibernates-legacy-and-enhanced-identifier-generators/

@Id
@GeneratedValue(/* здесь место для вашего кода */)
@GeneratedValue(generator = "sequence", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "sequence", sequenceName = "resume_id_seq", allocationSize = 10)
@Column(name = "id", updatable = false, nullable = false)
private Integer id;

@Column(name = "description")
private String description;

Resume() {}
protected Resume() {}

public Resume(String description) {
this.description = description;
Expand Down
31 changes: 25 additions & 6 deletions src/main/java/ru/hh/school/entity/Vacancy.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.hh.school.entity;

import org.hibernate.annotations.CreationTimestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
Expand All @@ -13,26 +15,43 @@
import java.util.Objects;

//TODO: оформите entity
@Entity
@Table(name = "vacancy", schema = "public")
public class Vacancy {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "vacancy_id", nullable = false, updatable = false)
private Integer id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "employer_id", nullable = false)
private Employer employer;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "area_id")
private Area area;

@Column(name = "title", nullable = false)
private String title;

@Column(name = "description")
private String description;

@Column(name = "compensation_from")
private Integer compensationFrom;

@Column(name = "compensation_to")
private Integer compensationTo;

@Column(name = "compensation_gross")
private Boolean compensationGross;

@CreationTimestamp
@Column(name = "creation_time", nullable = false, updatable = false)
private LocalDateTime creationTime;

@Column(name = "archiving_time")
private LocalDateTime archivingTime;

public Vacancy() {
Expand Down Expand Up @@ -91,16 +110,16 @@ public void setArchivingTime(LocalDateTime archivingTime) {
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Vacancy vacancy = (Vacancy) o;
return Objects.equals(id, vacancy.id);
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
Vacancy otherVacancy = (Vacancy) other;
return id.equals(otherVacancy.getId());
}

@Override
public int hashCode() {
return 17;
return (31 * id) ^ 2 + 31 * Objects.hash(title);
}

}
1 change: 1 addition & 0 deletions src/main/java/ru/hh/school/service/EmployerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public void blockIfEmployerUseBadWords(int employerId) {
transactionHelper.inTransaction(() -> {
employer.setBlockTime(LocalDateTime.now());
employer.getVacancies().forEach(v -> v.setArchivingTime(LocalDateTime.now()));
employerDao.save(employer);
});
}

Expand Down

0 comments on commit 3c91fa3

Please sign in to comment.