Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.bobocode.dao;

import com.bobocode.exception.CompanyDaoException;
import com.bobocode.model.Company;
import com.bobocode.util.ExerciseNotCompletedException;
import org.hibernate.Session;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import java.util.function.Function;

public class CompanyDaoImpl implements CompanyDao {
private EntityManagerFactory entityManagerFactory;
Expand All @@ -14,6 +18,27 @@ public CompanyDaoImpl(EntityManagerFactory entityManagerFactory) {

@Override
public Company findByIdFetchProducts(Long id) {
throw new ExerciseNotCompletedException(); // todo
return readWithinTx(entityManager ->
entityManager
.createQuery("select c from Company c join fetch c.products where c.id = :id", Company.class)
.setParameter("id", id)
.getSingleResult()
);
}

private <T> T readWithinTx(Function<EntityManager, T> entityManagerFunction) {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.unwrap(Session.class).setDefaultReadOnly(true);
entityManager.getTransaction().begin();
try {
T queryResult = entityManagerFunction.apply(entityManager);
entityManager.getTransaction().commit();
return queryResult;
} catch (Exception e) {
entityManager.getTransaction().rollback();
throw new CompanyDaoException("Error performing read operation", e);
} finally {
entityManager.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.bobocode.model;

import com.bobocode.util.ExerciseNotCompletedException;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -25,16 +24,28 @@
@NoArgsConstructor
@Getter
@Setter
@EqualsAndHashCode(of = "id")
@Entity
@Table(name = "company")
public class Company {
@Id
@GeneratedValue
private Long id;

@Column(nullable = false)
private String name;

@Setter(AccessLevel.PRIVATE)
@OneToMany(mappedBy = "company", fetch = FetchType.LAZY)
private List<Product> products = new ArrayList<>();

public void addProduct(Product product) {
throw new ExerciseNotCompletedException();
products.add(product);
product.setCompany(this);
}

public void removeProduct(Product product) {
throw new ExerciseNotCompletedException();
products.remove(product);
product.setCompany(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.bobocode.model;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;

/**
* todo:
* - configure JPA entity
Expand All @@ -17,8 +20,18 @@
@NoArgsConstructor
@Getter
@Setter
@EqualsAndHashCode(of = "id")
@Entity
@Table(name = "product")
public class Product {
@Id
@GeneratedValue
private Long id;

@Column(nullable = false)
private String name;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "company_id")
private Company company;
}