Skip to content

Commit

Permalink
Store Instant as Long. Fixes javaeekickoff#46
Browse files Browse the repository at this point in the history
  • Loading branch information
JEAN-PROST Frédéric committed Oct 31, 2024
1 parent fe5c263 commit 01603bc
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void remove(String loginToken) {

public void removeExpired() {
createNamedQuery("LoginToken.removeExpired")
.setParameter("expiration", Instant.now())
.executeUpdate();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.omnifaces.persistence.JPA.getOptionalSingleResult;
import static org.omnifaces.utils.security.MessageDigests.digest;

import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -118,6 +119,7 @@ public Optional<Person> findByEmail(String email) {
public Optional<Person> findByLoginToken(String loginToken, TokenType type) {
return getOptionalSingleResult(createNamedTypedQuery("Person.getByLoginToken")
.setParameter("tokenHash", digest(loginToken, "SHA-256"))
.setParameter("expiration", Instant.now())
.setParameter("tokenType", type));
}

Expand Down Expand Up @@ -155,4 +157,4 @@ public void setPassword(Person person, String password) {
credentials.setPassword(password);
}

}
}
13 changes: 6 additions & 7 deletions src/main/java/org/example/kickoff/config/InstantConverter.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package org.example.kickoff.config;
import java.time.Instant;
import java.util.Date;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

/**
* Converts the JDK 8 / JSR 310 <code>Instant</code> type to <code>Date</code> for
* Converts the JDK 8 / JSR 310 <code>Instant</code> type to <code>Long</code> for
* usage with JPA.
*
* <p>
Expand All @@ -18,23 +17,23 @@
* @author Arjan Tijms
*/
@Converter(autoApply = true)
public class InstantConverter implements AttributeConverter<Instant, Date> {
public class InstantConverter implements AttributeConverter<Instant, Long> {

@Override
public Date convertToDatabaseColumn(Instant instant) {
public Long convertToDatabaseColumn(Instant instant) {
if (instant == null) {
return null;
}

return Date.from(instant);
return instant.toEpochMilli();
}

@Override
public Instant convertToEntityAttribute(Date date) {
public Instant convertToEntityAttribute(Long date) {
if (date == null) {
return null;
}

return date.toInstant();
return Instant.ofEpochMilli(date);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/LoginToken.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
FROM
LoginToken _loginToken
WHERE
_loginToken.expiration &lt; CURRENT_TIMESTAMP
_loginToken.expiration &lt; :expiration
</query>
</named-query>
</entity-mappings>
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/Person.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
WHERE
_loginToken.tokenHash = :tokenHash AND
_loginToken.type = :tokenType AND
_loginToken.expiration &gt; CURRENT_TIMESTAMP
_loginToken.expiration &gt; :expiration
</query>
</named-query>
</entity-mappings>
5 changes: 2 additions & 3 deletions src/main/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
<!--
Needed because org.example.kickoff.config.InstantConverter is used, and Hibernate does not seem to fully apply it.
Without this it will still think the SQL type is 3003 (org.hibernate.types.SqlTypes.TIMESTAMP_UTC) and therefor complain
when it's actually 93 (java.sql.Types.TIMESTAMP).
-->
<property name="hibernate.type.preferred_instant_jdbc_type" value="TIMESTAMP"/>
<property name="hibernate.type.preferred_instant_jdbc_type" value="BIGINT"/>

<property name="wildfly.jpa.twophasebootstrap" value="false" /> <!-- https://issues.jboss.org/browse/WFLY-2727 -->

<property name="eclipselink.deploy-on-startup" value="true" />
</properties>
</persistence-unit>
</persistence>
</persistence>
2 changes: 1 addition & 1 deletion src/test/resources/dbunit/UserServiceTest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<person id="1001" email="[email protected]" firstName="test" lastName="test" emailVerified="1" created="2018-03-10 11:11:11" lastModified="2018-03-10 11:11:11" />
<person id="1001" email="[email protected]" firstName="test" lastName="test" emailVerified="1" created="1520680271000" lastModified="1520680271000" />
<credentials person_id="1001" passwordHash="E0K/IHFmCEwlgr4R5gaBOQy2Kwh+IwkMeWWBDer6x2k=" salt="fVJkXbtOO1HAs3GOPtwB9WGCsOSvdBITJsBAMzvN1Y5rWLbdEZFu6A==" />
<person_groups person_id="1001" groups="USER" />
</dataset>
5 changes: 2 additions & 3 deletions src/test/resources/test-persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
<!--
Needed because org.example.kickoff.config.InstantConverter is used, and Hibernate does not seem to fully apply it.
Without this it will still think the SQL type is 3003 (org.hibernate.types.SqlTypes.TIMESTAMP_UTC) and therefor complain
when it's actually 93 (java.sql.Types.TIMESTAMP).
-->
<property name="hibernate.type.preferred_instant_jdbc_type" value="TIMESTAMP"/>
<property name="hibernate.type.preferred_instant_jdbc_type" value="BIGINT"/>

<property name="wildfly.jpa.twophasebootstrap" value="false" /> <!-- https://issues.jboss.org/browse/WFLY-2727 -->

Expand All @@ -47,4 +46,4 @@

</properties>
</persistence-unit>
</persistence>
</persistence>

0 comments on commit 01603bc

Please sign in to comment.