-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JFB-9: Add CRUD operations using Hibernate
- Loading branch information
1 parent
542a889
commit 03873f2
Showing
9 changed files
with
457 additions
and
39 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.