Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes javaeekickoff/java-ee-kickoff-app#44 : use annotation NamedQuery #45

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public String generate(String email, String ipAddress, String description, Token
}

public void remove(String loginToken) {
createNamedQuery("LoginToken.remove")
createNamedQuery(LoginToken.REMOVE)
.setParameter("tokenHash", digest(loginToken, MESSAGE_DIGEST_ALGORITHM))
.executeUpdate();
}

public void removeExpired() {
createNamedQuery("LoginToken.removeExpired")
createNamedQuery(LoginToken.REMOVE_EXPIRED)
.executeUpdate();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ public void requestResetPassword(String email, String ipAddress, String callback
}

public Optional<Person> findByEmail(String email) {
return getOptionalSingleResult(createNamedTypedQuery("Person.getByEmail")
return getOptionalSingleResult(createNamedTypedQuery(Person.BY_EMAIL)
.setParameter("email", email));
}

public Optional<Person> findByLoginToken(String loginToken, TokenType type) {
return getOptionalSingleResult(createNamedTypedQuery("Person.getByLoginToken")
return getOptionalSingleResult(createNamedTypedQuery(Person.BY_LOGIN_TOKEN)
.setParameter("tokenHash", digest(loginToken, "SHA-256"))
.setParameter("tokenType", type));
}
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/org/example/kickoff/model/LoginToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,31 @@
import jakarta.persistence.Entity;
import jakarta.persistence.Enumerated;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.PrePersist;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

import org.omnifaces.persistence.model.GeneratedIdEntity;

@NamedQuery(name = LoginToken.REMOVE, query =
"DELETE"
+ " FROM"
+ " LoginToken _loginToken"
+ " WHERE"
+ " _loginToken.tokenHash = :tokenHash"
)
@NamedQuery(name = LoginToken.REMOVE_EXPIRED, query =
"DELETE"
+ " FROM"
+ " LoginToken _loginToken"
+ " WHERE"
+ " _loginToken.expiration &lt; CURRENT_TIMESTAMP"
)
@Entity
public class LoginToken extends GeneratedIdEntity<Long> {
public final static String REMOVE = "LoginToken.remove";
public final static String REMOVE_EXPIRED = "LoginToken.removeExpired";

private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -116,4 +133,4 @@ public void setTimestamps() {
}
}

}
}
27 changes: 26 additions & 1 deletion src/main/java/org/example/kickoff/model/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.Enumerated;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Transient;
Expand All @@ -25,8 +26,32 @@
import org.example.kickoff.model.validator.Email;
import org.omnifaces.persistence.model.TimestampedEntity;

@NamedQuery(name = Person.BY_EMAIL, query =
"SELECT"
+ " _person"
+ " FROM"
+ " Person _person"
+ " WHERE"
+ " _person.email = :email"
)
@NamedQuery(name = Person.BY_LOGIN_TOKEN, query =
"SELECT"
+ " _person"
+ " FROM"
+ " Person _person"
+ " JOIN"
+ " _person.loginTokens _loginToken"
+ " JOIN FETCH"
+ " _person.loginTokens"
+ " WHERE"
+ " _loginToken.tokenHash = :tokenHash AND"
+ " _loginToken.type = :tokenType AND"
+ " _loginToken.expiration &gt; CURRENT_TIMESTAMP"
)
@Entity
public class Person extends TimestampedEntity<Long> {
public final static String BY_LOGIN_TOKEN = "Person.getByLoginToken";
public final static String BY_EMAIL = "Person.getByEmail";

private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -136,4 +161,4 @@ public Set<String> getRolesAsStrings() {
return getRoles().stream().map(Role::name).collect(toSet());
}

}
}
27 changes: 0 additions & 27 deletions src/main/resources/META-INF/LoginToken.xml

This file was deleted.

35 changes: 0 additions & 35 deletions src/main/resources/META-INF/Person.xml

This file was deleted.

3 changes: 0 additions & 3 deletions src/main/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
<persistence-unit name="kickoff">
<jta-data-source>java:app/kickoff/DataSource</jta-data-source>

<mapping-file>META-INF/LoginToken.xml</mapping-file>
<mapping-file>META-INF/Person.xml</mapping-file>

<properties>
<property name="jakarta.persistence.schema-generation.database.action" value="drop-and-create" />

Expand Down