Skip to content

Commit

Permalink
Downgrade Hibernate to finally fix dependency incompatibilities!
Browse files Browse the repository at this point in the history
Add some named queries to entities and fix access by primary key
  • Loading branch information
massijay committed Sep 5, 2022
1 parent 5a66d00 commit 8bffd0e
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 66 deletions.
61 changes: 30 additions & 31 deletions .idea/artifacts/TripRecorder_war_exploded.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 24 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,42 @@
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.36</version>
<version>2.34</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.36</version>
<version>2.34</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-cdi2-se</artifactId>
<version>2.36</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
<version>2.34</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>javax.ws.rs</groupId>-->
<!-- <artifactId>javax.ws.rs-api</artifactId>-->
<!-- <version>2.1.1</version>-->
<!-- </dependency>-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.el</artifactId>
<version>4.0.1</version>
<version>8.0.30</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.glassfish</groupId>-->
<!-- <artifactId>jakarta.el</artifactId>-->
<!-- <version>4.0.1</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.0.Final</version>
<version>5.6.11.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>7.0.4.Final</version>
<version>6.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
Expand Down Expand Up @@ -90,6 +90,14 @@
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
9 changes: 6 additions & 3 deletions src/main/java/com/mcris/triprecorder/HelloServlet.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.mcris.triprecorder;

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package com.mcris.triprecorder.models.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;

import javax.persistence.*;
import java.sql.Timestamp;

@Entity
@Table(name = "geopoints", schema = "trip_recorder")
@NamedQuery(name = "Geopoint.getListByTripIdIfUser",
query = "select t.geopoints from Trip t where t.id = :tripId and t.userId = :userId")
public class Geopoint {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "id")
private int id;
@Basic
@Column(name = "trip_id", insertable = false, updatable = false)
@Column(name = "trip_id", nullable = false)
private int tripId;
@Basic
@Column(name = "latitude")
Expand All @@ -28,7 +30,7 @@ public class Geopoint {
@Column(name = "label")
private String label;
@ManyToOne
@JoinColumn(name = "trip_id", referencedColumnName = "id", nullable = false)
@JoinColumn(name = "trip_id", referencedColumnName = "id", insertable = false, updatable = false)
@JsonIgnore
private Trip trip;

Expand Down
18 changes: 12 additions & 6 deletions src/main/java/com/mcris/triprecorder/models/entities/Session.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
package com.mcris.triprecorder.models.entities;

import jakarta.persistence.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.annotations.Type;

import javax.persistence.*;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.UUID;

@Entity
@Table(name = "sessions", schema = "trip_recorder")
@NamedQuery(name = "Session.byToken", query = "select s from Session s where s.token = :tokenString")
@NamedQueries({
@NamedQuery(name = "Session.byToken",
query = "select s from Session s where s.token = :tokenString"),
@NamedQuery(name = "Session.deleteByToken",
query = "delete from Session s where s.token = :tokenString")
})
public class Session {
// https://www.codementor.io/@petrepopescu/how-to-use-string-uuid-in-hibernate-with-mysql-1jrhjh6ef5
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Id
@Column(name = "token", updatable = false, nullable = false, columnDefinition = "VARCHAR(255)")
@JdbcTypeCode(Types.VARCHAR)
@Type(type = "uuid-char")
private UUID token;
@JsonIgnore
@Basic
@Column(name = "user_id")
@Column(name = "user_id", nullable = false)
private int userId;
@Basic
@Column(name = "expire_at")
private Timestamp expireAt;
@JsonIgnore
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false, insertable = false, updatable = false)
private User user;
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/com/mcris/triprecorder/models/entities/Trip.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package com.mcris.triprecorder.models.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;

import javax.persistence.*;
import java.sql.Timestamp;
import java.util.Collection;

@Entity
@Table(name = "trips", schema = "trip_recorder")
@NamedQueries({
@NamedQuery(name = "Trip.getByIdIfCorrectUser",
query = "select t from Trip t where t.id = :tripId and t.userId = :userId"),
@NamedQuery(name = "Trip.removeByIdIfCorrectUser",
query = "delete from Trip t where t.id = :tripId and t.userId = :userId")
})

public class Trip {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "id")
private int id;
@JsonIgnore
@Basic
@Column(name = "user_id", insertable = false, updatable = false)
@Column(name = "user_id", nullable = false)
private int userId;
@Basic
@Column(name = "trip_name")
Expand All @@ -28,11 +36,12 @@ public class Trip {
@Basic
@Column(name = "notes")
private String notes;
@OneToMany(mappedBy = "trip")
@JsonIgnore
@OneToMany(mappedBy = "trip", fetch = FetchType.LAZY)
private Collection<Geopoint> geopoints;
@JsonIgnore
@ManyToOne
@JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false)
@JoinColumn(name = "user_id", referencedColumnName = "id", insertable = false, updatable = false)
private User user;

public int getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import jakarta.persistence.*;
import com.fasterxml.jackson.annotation.JsonProperty;

import javax.persistence.*;
import java.security.Principal;
import java.sql.Timestamp;
import java.util.Collection;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Entity
@Table(name = "users", schema = "trip_recorder")
@NamedQuery(name = "User.byId", query = "select u from User u where u.id = :userId")
@NamedQuery(name = "User.byUsernameAndPassword", query = "select u from User u where u.username = :username and u.password = :password")
public class User implements Principal {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
Expand All @@ -20,7 +21,7 @@ public class User implements Principal {
@Basic
@Column(name = "username")
private String username;
@JsonIgnore
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@Basic
@Column(name = "password")
private String password;
Expand All @@ -30,6 +31,7 @@ public class User implements Principal {
@JsonIgnore
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private Collection<Session> sessions;
@JsonIgnore
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private Collection<Trip> trips;
@Basic
Expand Down

0 comments on commit 8bffd0e

Please sign in to comment.