Skip to content

Commit

Permalink
JFB-9: Add CRUD operations using Hibernate
Browse files Browse the repository at this point in the history
  • Loading branch information
rtemirbulat committed Jul 6, 2024
1 parent 542a889 commit 03873f2
Show file tree
Hide file tree
Showing 9 changed files with 457 additions and 39 deletions.
35 changes: 0 additions & 35 deletions build.gradle

This file was deleted.

17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.5.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-hikaricp</artifactId>
<version>6.5.2.Final</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.3</version>
</dependency>


</dependencies>

</project>
Binary file removed screenshot/result.png
Binary file not shown.
94 changes: 94 additions & 0 deletions src/main/java/com/rtemi/dao/TicketDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.rtemi.dao;
import com.rtemi.model.Ticket;
import com.rtemi.model.enums.TicketType;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.ArrayList;
import java.util.List;

public class TicketDAO {
private SessionFactory sessionFactory;

public TicketDAO() {
sessionFactory = new Configuration().configure().buildSessionFactory();
}
public void saveTicket(Ticket ticket) {
Transaction transaction = null;
try (Session session = sessionFactory.openSession()) {
transaction = session.beginTransaction();
session.persist(ticket);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
}
}
public Ticket getTicketById(int id) {
try (Session session = sessionFactory.openSession()) {
return session.get(Ticket.class, id);
}
}

public List<Ticket> getTicketsByUserId(int userId) {
try (Session session = sessionFactory.openSession()) {
return session.createQuery("FROM Ticket WHERE user.id = :userId ", Ticket.class)
.setParameter("userId", userId)
.list();
}
}

public void updateTicketType(int id, TicketType ticketType) {
Transaction transaction = null;
try (Session session = sessionFactory.openSession()) {
transaction = session.beginTransaction();
Ticket ticket = session.get(Ticket.class, id);
if (ticket != null) {
ticket.setTicketType(ticketType);
session.merge(ticket);
}
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
}
}

public void deleteTicketsByUserId(int userId) {
Transaction transaction = null;
try (Session session = sessionFactory.openSession()) {
transaction = session.beginTransaction();
List<Ticket> tickets = session.createQuery("from Ticket where user.id = :userId", Ticket.class)
.setParameter("userId", userId)
.list();
for (Ticket ticket : tickets) {
session.remove(ticket);
}
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
}
}

public void retrieveAllTickets() {
try (Session session = sessionFactory.openSession()) {
List<Ticket> tickets = session.createQuery("from Ticket", Ticket.class).list();
System.out.println("ID | User ID | Ticket Type | Creation Date");
System.out.println("---|---------|-------------|--------------------------");
for (Ticket ticket : tickets) {
System.out.printf("%-3d| %-7d | %-11s | %-25s%n", ticket.getId(), ticket.getUser().getId(), ticket.getTicketType(), ticket.getCreationDate());
}
}
}


}
83 changes: 83 additions & 0 deletions src/main/java/com/rtemi/dao/UserDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.rtemi.dao;
import com.rtemi.model.Ticket;
import com.rtemi.model.User;
import com.rtemi.model.enums.TicketType;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.List;

public class UserDAO {
private SessionFactory sessionFactory;

public UserDAO(){
sessionFactory = new Configuration().configure().buildSessionFactory();
}

public void saveUser(User user){
Transaction transaction = null;
try (Session session = sessionFactory.openSession()){
transaction = session.beginTransaction();
session.persist(user);
transaction.commit();
} catch (Exception e){
if (transaction!=null){
transaction.rollback();
}
e.printStackTrace();
}
}
public User getUserById(int id) {
try (Session session = sessionFactory.openSession()) {
return session.get(User.class, id);
}
}
public void deleteUserById(int id) {
Transaction transaction = null;
try (Session session = sessionFactory.openSession()) {
transaction = session.beginTransaction();
User user = session.get(User.class, id);
if (user != null) {
session.remove(user);
}
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
}
}
public void retrieveAllUsers() {
try (Session session = sessionFactory.openSession()) {
List<User> users = session.createQuery("from User", User.class).list();
System.out.println("ID | Name | Creation Date");
System.out.println("---|------------|--------------------------");
for (User user : users) {
System.out.printf("%-3d| %-10s | %-25s%n", user.getId(), user.getName(), user.getCreationDate());
}
}
}
public void updateUserAndTickets(User user, TicketType ticketType) {
Transaction transaction = null;
try (Session session = sessionFactory.openSession()) {
transaction = session.beginTransaction();
session.merge(user);
TicketDAO ticketDAO = new TicketDAO();
List<Ticket> tickets = ticketDAO.getTicketsByUserId(user.getId());
for (Ticket ticket : tickets) {
ticket.setTicketType(ticketType);
session.merge(ticket);
}
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
}
}

}
74 changes: 74 additions & 0 deletions src/main/java/com/rtemi/model/Ticket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.rtemi.model;

import com.rtemi.model.enums.TicketType;
import jakarta.persistence.*;

import java.sql.Timestamp;

@Entity
@Table (name = "ticket")
public class Ticket {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private User user;

@Enumerated(EnumType.STRING)
@Column(name = "ticket_type")
private TicketType ticketType;

@Column(name = "creation_date")
private Timestamp creationDate;

public Ticket(int id, User user, TicketType ticketType, Timestamp creationDate) {
this.id = id;
this.user = user;
this.ticketType = ticketType;
this.creationDate = creationDate;
}

public Ticket(int id, TicketType ticketType) {
this.id = id;
this.ticketType = ticketType;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public Ticket() {

}

public void setId(int id) {
this.id = id;
}

public int getId() {
return id;
}

public TicketType getTicketType() {
return ticketType;
}

public void setTicketType(TicketType ticketType) {
this.ticketType = ticketType;
}


public Timestamp getCreationDate() {
return creationDate;
}

public void setCreationDate(Timestamp creationDate) {
this.creationDate = creationDate;
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/rtemi/model/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.rtemi.model;


import jakarta.persistence.*;

import java.sql.Timestamp;

@Entity
@Table (name = "\"user\"")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

private String name;

private Timestamp creationDate;

public User(int id, String name, Timestamp creationDate) {
this.id = id;
this.name = name;
this.creationDate = creationDate;
}

public User() {

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Timestamp getCreationDate() {
return creationDate;
}

public void setCreationDate(Timestamp creationDate) {
this.creationDate = creationDate;
}

public void setId(int id) {
this.id = id;
}

public int getId() {
return id;
}
}
5 changes: 1 addition & 4 deletions src/main/java/com/rtemi/model/enums/TicketType.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.rtemi.model.enums;

import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;

public enum TicketType {
BusTicket,
ConcertTicket
DAY, WEEK, MONTH, YEAR
}
Loading

0 comments on commit 03873f2

Please sign in to comment.