adding entity and crud
This commit is contained in:
59
src/main/java/ch/progetto152/crud/crudLocation.java
Normal file
59
src/main/java/ch/progetto152/crud/crudLocation.java
Normal file
@@ -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<Location> readAll() {
|
||||||
|
return session.createQuery("from Location").list();
|
||||||
|
}
|
||||||
|
}
|
||||||
57
src/main/java/ch/progetto152/crud/crudLocationVisited.java
Normal file
57
src/main/java/ch/progetto152/crud/crudLocationVisited.java
Normal file
@@ -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<LocationVisited> readAll() {
|
||||||
|
return session.createQuery("from LocationVisited").list();
|
||||||
|
}
|
||||||
|
}
|
||||||
58
src/main/java/ch/progetto152/crud/crudUser.java
Normal file
58
src/main/java/ch/progetto152/crud/crudUser.java
Normal file
@@ -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<User> readAll() {
|
||||||
|
return session.createQuery("from User").list();
|
||||||
|
}
|
||||||
|
}
|
||||||
60
src/main/java/ch/progetto152/crud/crudWaypoints.java
Normal file
60
src/main/java/ch/progetto152/crud/crudWaypoints.java
Normal file
@@ -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<Waypoints> readAll() {
|
||||||
|
return session.createQuery("from Waypoints").list();
|
||||||
|
}
|
||||||
|
}
|
||||||
82
src/main/java/ch/progetto152/entity/Location.java
Normal file
82
src/main/java/ch/progetto152/entity/Location.java
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
59
src/main/java/ch/progetto152/entity/LocationVisited.java
Normal file
59
src/main/java/ch/progetto152/entity/LocationVisited.java
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
71
src/main/java/ch/progetto152/entity/User.java
Normal file
71
src/main/java/ch/progetto152/entity/User.java
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
104
src/main/java/ch/progetto152/entity/Waypoints.java
Normal file
104
src/main/java/ch/progetto152/entity/Waypoints.java
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
23
src/main/java/ch/progetto152/utils/HibernateUtils.java
Normal file
23
src/main/java/ch/progetto152/utils/HibernateUtils.java
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user