parent
3315249c70
commit
3e8775e93f
@ -0,0 +1,63 @@ |
||||
package mivan.model; |
||||
|
||||
import javax.persistence.Column; |
||||
import javax.persistence.Id; |
||||
|
||||
public class Loan { |
||||
|
||||
private static final long serialVersionUID = -5124436115031696628L; |
||||
|
||||
@Id |
||||
@Column(name = "id", nullable=false, unique=true) |
||||
private long id; |
||||
|
||||
@Column(name = "date_start", nullable = false) |
||||
private String date_start; |
||||
|
||||
@Column(name = "date_end", nullable = false) |
||||
private String date_end; |
||||
|
||||
@Column(name = "state", nullable = false) |
||||
private String state; |
||||
|
||||
public Loan(long id, String date_start, String date_end, String state) { |
||||
this.id = id; |
||||
this.date_start = date_start; |
||||
this.date_end = date_end; |
||||
this.state = state; |
||||
} |
||||
|
||||
public long getId() { |
||||
return id; |
||||
} |
||||
|
||||
public String getDate_start() { |
||||
return date_start; |
||||
} |
||||
|
||||
public String getDate_end() { |
||||
return date_end; |
||||
} |
||||
|
||||
public String getState() { |
||||
return state; |
||||
} |
||||
|
||||
public void setId(long id) { |
||||
this.id = id; |
||||
} |
||||
|
||||
public void setDate_start(String date_start) { |
||||
this.date_start = date_start; |
||||
} |
||||
|
||||
public void setDate_end(String date_end) { |
||||
this.date_end = date_end; |
||||
} |
||||
|
||||
public void setState(String state) { |
||||
this.state = state; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,16 @@ |
||||
package mivan.repository; |
||||
|
||||
|
||||
import mivan.model.Loan; |
||||
|
||||
public interface LoanRepository extends Repository<Loan, Long> { |
||||
|
||||
public void deleteLoanById(Long id); |
||||
|
||||
public void addLoan(Loan loan); |
||||
|
||||
public void updateLoan(Long id, String stato); |
||||
|
||||
public int getSize(); |
||||
|
||||
} |
@ -0,0 +1,98 @@ |
||||
package mivan.repository; |
||||
|
||||
import java.util.List; |
||||
import java.util.Optional; |
||||
|
||||
import javax.persistence.EntityManager; |
||||
import javax.persistence.EntityManagerFactory; |
||||
import javax.persistence.Persistence; |
||||
|
||||
import mivan.model.Loan; |
||||
|
||||
public class LoanRepositoryImpl implements LoanRepository{ |
||||
|
||||
private EntityManagerFactory entityManagerFactory; |
||||
|
||||
public LoanRepositoryImpl() { |
||||
this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan"); |
||||
} |
||||
|
||||
public Optional<Loan> findById(Long id) { |
||||
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); |
||||
Loan loan = entityManager.find(Loan.class, id); |
||||
entityManager.close(); |
||||
return Optional.ofNullable(loan); |
||||
} |
||||
|
||||
@Override |
||||
public Iterable<Loan> findAll() { |
||||
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); |
||||
List<Loan> loan = entityManager.createQuery("FROM Loan", Loan.class).getResultList(); |
||||
entityManager.close(); |
||||
return loan; |
||||
} |
||||
|
||||
@Override |
||||
public void deleteLoanById(Long id) { |
||||
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); |
||||
try { |
||||
if (!entityManager.getTransaction().isActive()) { |
||||
entityManager.getTransaction().begin(); |
||||
} |
||||
Loan loan = entityManager.find(Loan.class, id); |
||||
entityManager.remove(loan); |
||||
entityManager.getTransaction().commit(); |
||||
entityManager.close(); |
||||
} catch (Exception ex) { |
||||
entityManager.getTransaction().rollback(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void addLoan(Loan loan) { |
||||
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); |
||||
try { |
||||
if (!entityManager.getTransaction().isActive()) { |
||||
entityManager.getTransaction().begin(); |
||||
} |
||||
entityManager.persist(loan); |
||||
entityManager.getTransaction().commit(); |
||||
entityManager.close(); |
||||
|
||||
} catch (Exception ex) { |
||||
entityManager.getTransaction().rollback(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void updateLoan(Long id, String state) { |
||||
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); |
||||
try { |
||||
if (!entityManager.getTransaction().isActive()) { |
||||
entityManager.getTransaction().begin(); |
||||
} |
||||
Loan loan = entityManager.find(Loan.class, id); |
||||
loan.setState(state); |
||||
entityManager.persist(loan); |
||||
entityManager.getTransaction().commit(); |
||||
entityManager.close(); |
||||
} catch (Exception ex) { |
||||
entityManager.getTransaction().rollback(); |
||||
} |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public int getSize() { |
||||
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); |
||||
int size = 0; |
||||
try { |
||||
size = entityManager.createQuery("FROM Loan").getResultList().size(); |
||||
|
||||
entityManager.close(); |
||||
} catch (Exception ex) { |
||||
size = 0; |
||||
} |
||||
return size; |
||||
} |
||||
} |
Loading…
Reference in new issue