From e5e9ac36984fb0381427642bbc747d2f17a7ac15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Tue, 25 Apr 2023 15:13:53 +0200 Subject: [PATCH] adding entity and crud --- .../ch/progetto152/crud/crudLocation.java | 59 ++++++++++ .../progetto152/crud/crudLocationVisited.java | 57 ++++++++++ .../java/ch/progetto152/crud/crudUser.java | 58 ++++++++++ .../ch/progetto152/crud/crudWaypoints.java | 60 ++++++++++ .../java/ch/progetto152/entity/Location.java | 82 ++++++++++++++ .../progetto152/entity/LocationVisited.java | 59 ++++++++++ src/main/java/ch/progetto152/entity/User.java | 71 ++++++++++++ .../java/ch/progetto152/entity/Waypoints.java | 104 ++++++++++++++++++ .../ch/progetto152/utils/HibernateUtils.java | 23 ++++ 9 files changed, 573 insertions(+) create mode 100644 src/main/java/ch/progetto152/crud/crudLocation.java create mode 100644 src/main/java/ch/progetto152/crud/crudLocationVisited.java create mode 100644 src/main/java/ch/progetto152/crud/crudUser.java create mode 100644 src/main/java/ch/progetto152/crud/crudWaypoints.java create mode 100644 src/main/java/ch/progetto152/entity/Location.java create mode 100644 src/main/java/ch/progetto152/entity/LocationVisited.java create mode 100644 src/main/java/ch/progetto152/entity/User.java create mode 100644 src/main/java/ch/progetto152/entity/Waypoints.java create mode 100644 src/main/java/ch/progetto152/utils/HibernateUtils.java diff --git a/src/main/java/ch/progetto152/crud/crudLocation.java b/src/main/java/ch/progetto152/crud/crudLocation.java new file mode 100644 index 0000000..eb9a775 --- /dev/null +++ b/src/main/java/ch/progetto152/crud/crudLocation.java @@ -0,0 +1,59 @@ +package ch.progetto152.crud; + +import ch.progetto152.entity.Location; +import ch.progetto152.utils.HibernateUtils; +import org.hibernate.Session; +import org.hibernate.Transaction; + +import java.util.List; + +public class crudLocation { + + private Session session; + + public crudLocation(Session session) { + this.session = session; + } + + public void create(Location Location) { + Transaction tx = session.beginTransaction(); + session.save(Location); + tx.commit(); + } + + public Location read(int index) { + Session asd = HibernateUtils.getSessionFactory().openSession(); + Location Location = asd.get(Location.class, index); + return Location; + } + + public Location readLocationByName(String name) { + Session asd = HibernateUtils.getSessionFactory().openSession(); + Location Location = (Location) asd.createQuery("from Location where name = :name").setParameter("name", name).uniqueResult(); + return Location; + } + + public void update(int id, Location location) { + Transaction tx = session.beginTransaction(); + Location Location = session.get(Location.class, id); + session.evict(Location); + Location.setId(location.getId()); + Location.setLocation(location.getLocation()); + Location.setRegion(location.getRegion()); + Location.setLat(location.getLat()); + Location.setLon(location.getLon()); + session.update(Location); + tx.commit(); + } + + public void delete(int id) { + Transaction tx = session.beginTransaction(); + Location Location = session.get(Location.class, id); + session.delete(Location); + tx.commit(); + } + + public List readAll() { + return session.createQuery("from Location").list(); + } +} \ No newline at end of file diff --git a/src/main/java/ch/progetto152/crud/crudLocationVisited.java b/src/main/java/ch/progetto152/crud/crudLocationVisited.java new file mode 100644 index 0000000..3e60c1b --- /dev/null +++ b/src/main/java/ch/progetto152/crud/crudLocationVisited.java @@ -0,0 +1,57 @@ +package ch.progetto152.crud; + +import ch.progetto152.entity.LocationVisited; +import ch.progetto152.utils.HibernateUtils; +import org.hibernate.Session; +import org.hibernate.Transaction; + +import java.util.List; + +public class crudLocationVisited { + + private Session session; + + public crudLocationVisited(Session session) { + this.session = session; + } + + public void create(LocationVisited LocationVisited) { + Transaction tx = session.beginTransaction(); + session.save(LocationVisited); + tx.commit(); + } + + public LocationVisited read(int index) { + Session asd = HibernateUtils.getSessionFactory().openSession(); + LocationVisited LocationVisited = asd.get(LocationVisited.class, index); + return LocationVisited; + } + + public LocationVisited readLocationVisitedByName(String name) { + Session asd = HibernateUtils.getSessionFactory().openSession(); + LocationVisited LocationVisited = (LocationVisited) asd.createQuery("from LocationVisited where name = :name").setParameter("name", name).uniqueResult(); + return LocationVisited; + } + + public void update(int id, LocationVisited locationVisited) { + Transaction tx = session.beginTransaction(); + LocationVisited LocationVisited = session.get(LocationVisited.class, id); + session.evict(LocationVisited); + LocationVisited.setId(locationVisited.getId()); + LocationVisited.setLocationid(locationVisited.getLocationid()); + LocationVisited.setUserId(locationVisited.getUserId()); + session.update(LocationVisited); + tx.commit(); + } + + public void delete(int id) { + Transaction tx = session.beginTransaction(); + LocationVisited LocationVisited = session.get(LocationVisited.class, id); + session.delete(LocationVisited); + tx.commit(); + } + + public List readAll() { + return session.createQuery("from LocationVisited").list(); + } +} \ No newline at end of file diff --git a/src/main/java/ch/progetto152/crud/crudUser.java b/src/main/java/ch/progetto152/crud/crudUser.java new file mode 100644 index 0000000..1ee9a5f --- /dev/null +++ b/src/main/java/ch/progetto152/crud/crudUser.java @@ -0,0 +1,58 @@ +package ch.progetto152.crud; + +import ch.progetto152.entity.User; +import ch.progetto152.utils.HibernateUtils; +import org.hibernate.Session; +import org.hibernate.Transaction; + +import java.util.List; + +public class crudUser { + + private Session session; + + public crudUser(Session session) { + this.session = session; + } + + public void create(User User) { + Transaction tx = session.beginTransaction(); + session.save(User); + tx.commit(); + } + + public User read(int index) { + Session asd = HibernateUtils.getSessionFactory().openSession(); + User User = asd.get(User.class, index); + return User; + } + + public User readUserByName(String name) { + Session asd = HibernateUtils.getSessionFactory().openSession(); + User User = (User) asd.createQuery("from User where name = :name").setParameter("name", name).uniqueResult(); + return User; + } + + public void update(int id, User user) { + Transaction tx = session.beginTransaction(); + User User = session.get(User.class, id); + session.evict(User); + User.setId(user.getId()); + User.setName(user.getName()); + User.setUsername(user.getUsername()); + User.setPassword(user.getPassword()); + session.update(User); + tx.commit(); + } + + public void delete(int id) { + Transaction tx = session.beginTransaction(); + User User = session.get(User.class, id); + session.delete(User); + tx.commit(); + } + + public List readAll() { + return session.createQuery("from User").list(); + } +} \ No newline at end of file diff --git a/src/main/java/ch/progetto152/crud/crudWaypoints.java b/src/main/java/ch/progetto152/crud/crudWaypoints.java new file mode 100644 index 0000000..b41907a --- /dev/null +++ b/src/main/java/ch/progetto152/crud/crudWaypoints.java @@ -0,0 +1,60 @@ +package ch.progetto152.crud; + +import ch.progetto152.entity.Waypoints; +import ch.progetto152.utils.HibernateUtils; +import org.hibernate.Session; +import org.hibernate.Transaction; + +import java.util.List; + +public class crudWaypoints { + + private Session session; + + public crudWaypoints(Session session) { + this.session = session; + } + + public void create(Waypoints Waypoints) { + Transaction tx = session.beginTransaction(); + session.save(Waypoints); + tx.commit(); + } + + public Waypoints read(int index) { + Session asd = HibernateUtils.getSessionFactory().openSession(); + Waypoints Waypoints = asd.get(Waypoints.class, index); + return Waypoints; + } + + public Waypoints readWaypointsByName(String name) { + Session asd = HibernateUtils.getSessionFactory().openSession(); + Waypoints Waypoints = (Waypoints) asd.createQuery("from Waypoints where name = :name").setParameter("name", name).uniqueResult(); + return Waypoints; + } + + public void update(int id, Waypoints waypoints) { + Transaction tx = session.beginTransaction(); + Waypoints Waypoints = session.get(Waypoints.class, id); + session.evict(Waypoints); + Waypoints.setId(waypoints.getId()); + Waypoints.setName(waypoints.getName()); + Waypoints.setLat(waypoints.getLat()); + Waypoints.setLon(waypoints.getLon()); + Waypoints.setDescription(waypoints.getDescription()); + Waypoints.setImg(waypoints.getImg()); + session.update(Waypoints); + tx.commit(); + } + + public void delete(int id) { + Transaction tx = session.beginTransaction(); + Waypoints Waypoints = session.get(Waypoints.class, id); + session.delete(Waypoints); + tx.commit(); + } + + public List readAll() { + return session.createQuery("from Waypoints").list(); + } +} \ No newline at end of file diff --git a/src/main/java/ch/progetto152/entity/Location.java b/src/main/java/ch/progetto152/entity/Location.java new file mode 100644 index 0000000..69c5291 --- /dev/null +++ b/src/main/java/ch/progetto152/entity/Location.java @@ -0,0 +1,82 @@ +package ch.progetto152.entity; + +import javax.persistence.*; +import java.util.Objects; + +@Entity +@Table(name = "Location", schema = "Progetto152", catalog = "") +public class Location { + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Id + @Column(name = "id") + private int id; + @Basic + @Column(name = "location") + private String location; + @Basic + @Column(name = "region") + private String region; + @Basic + @Column(name = "lat") + private double lat; + @Basic + @Column(name = "lon") + private double lon; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } + + public String getRegion() { + return region; + } + + public void setRegion(String region) { + this.region = region; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } + + public double getLon() { + return lon; + } + + public void setLon(double lon) { + this.lon = lon; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Location that = (Location) o; + return id == that.id && Double.compare(that.lat, lat) == 0 && Double.compare(that.lon, lon) == 0 && Objects.equals(location, that.location) && Objects.equals(region, that.region); + } + + @Override + public int hashCode() { + return Objects.hash(id, location, region, lat, lon); + } +} diff --git a/src/main/java/ch/progetto152/entity/LocationVisited.java b/src/main/java/ch/progetto152/entity/LocationVisited.java new file mode 100644 index 0000000..80ee32f --- /dev/null +++ b/src/main/java/ch/progetto152/entity/LocationVisited.java @@ -0,0 +1,59 @@ +package ch.progetto152.entity; + +import javax.persistence.*; +import java.util.Objects; + +@Entity +@Table(name = "LocationVisited", schema = "Progetto152", catalog = "") +public class LocationVisited{ + @Basic + @Column(name = "userId") + private int userId; + @Basic + @Column(name = "locationid") + private int locationid; + @Id + private Long id; + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public int getLocationid() { + return locationid; + } + + public void setLocationid(int locationid) { + this.locationid = locationid; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + LocationVisited that = (LocationVisited) o; + return userId == that.userId && locationid == that.locationid; + } + + @Override + public int hashCode() { + return Objects.hash(userId, locationid); + } + + public void setId(Long id) { + this.id = id; + } + + public Long getId() { + return id; + } + +} diff --git a/src/main/java/ch/progetto152/entity/User.java b/src/main/java/ch/progetto152/entity/User.java new file mode 100644 index 0000000..d55c65e --- /dev/null +++ b/src/main/java/ch/progetto152/entity/User.java @@ -0,0 +1,71 @@ +package ch.progetto152.entity; + +import javax.persistence.*; +import java.util.Objects; + +@Entity +@Table(name = "User", schema = "Progetto152", catalog = "") +public class User { + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Id + @Column(name = "id") + private int id; + @Basic + @Column(name = "name") + private String name; + @Basic + @Column(name = "username") + private String username; + @Basic + @Column(name = "password") + private String password; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + User that = (User) o; + return id == that.id && Objects.equals(name, that.name) && Objects.equals(username, that.username) && Objects.equals(password, that.password); + } + + @Override + public int hashCode() { + return Objects.hash(id, name, username, password); + } +} diff --git a/src/main/java/ch/progetto152/entity/Waypoints.java b/src/main/java/ch/progetto152/entity/Waypoints.java new file mode 100644 index 0000000..02802bb --- /dev/null +++ b/src/main/java/ch/progetto152/entity/Waypoints.java @@ -0,0 +1,104 @@ +package ch.progetto152.entity; + +import javax.persistence.*; +import java.util.Objects; + +@Entity +@Table(name = "Waypoints", schema = "Progetto152", catalog = "") +public class Waypoints { + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Id + @Column(name = "id") + private int id; + @Basic + @Column(name = "name") + private String name; + @Basic + @Column(name = "lat") + private double lat; + @Basic + @Column(name = "lon") + private double lon; + @Basic + @Column(name = "description") + private String description; + @Basic + @Column(name = "img") + private String img; + @Basic + @Column(name = "LocationId") + private int locationId; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } + + public double getLon() { + return lon; + } + + public void setLon(double lon) { + this.lon = lon; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getImg() { + return img; + } + + public void setImg(String img) { + this.img = img; + } + + public int getLocationId() { + return locationId; + } + + public void setLocationId(int locationId) { + this.locationId = locationId; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Waypoints that = (Waypoints) o; + return id == that.id && Double.compare(that.lat, lat) == 0 && Double.compare(that.lon, lon) == 0 && locationId == that.locationId && Objects.equals(name, that.name) && Objects.equals(description, that.description) && Objects.equals(img, that.img); + } + + @Override + public int hashCode() { + return Objects.hash(id, name, lat, lon, description, img, locationId); + } +} diff --git a/src/main/java/ch/progetto152/utils/HibernateUtils.java b/src/main/java/ch/progetto152/utils/HibernateUtils.java new file mode 100644 index 0000000..de3fd7d --- /dev/null +++ b/src/main/java/ch/progetto152/utils/HibernateUtils.java @@ -0,0 +1,23 @@ +package ch.progetto152.utils; + +import ch.progetto152.entity.*; +import org.hibernate.cfg.Configuration; +import org.hibernate.SessionFactory; + +public class HibernateUtils { + + private static SessionFactory sessionFactory = null; + + public static SessionFactory getSessionFactory() { + if (sessionFactory == null) { + Configuration configuration = new Configuration().configure("hibernate.cfg.xml"); + configuration.addAnnotatedClass(Location.class); + configuration.addAnnotatedClass(LocationVisited.class); + configuration.addAnnotatedClass(User.class); + configuration.addAnnotatedClass(Waypoints.class); + sessionFactory = configuration.buildSessionFactory(); + } + return sessionFactory; + } + +}