Monday, August 24, 2015

How to override Spring Boot application.properties in tests

To override Spring Boot application.properties in tests,

use the following annotations:

@TestPropertySource(properties = {"spring.cache.type=simple"})
@DirtiesContext

References:
http://stackoverflow.com/questions/29669393/override-default-spring-boot-application-properties-settings-in-junit-test
https://github.com/spring-projects/spring-boot/issues/2198

Sunday, August 23, 2015

org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException

When you try to send an email with Gmail SMTP,

you might get the following exception:

org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbuBm
534-5.7.14 7mH3ysGBLpLeQAGDrZrkNi3uUJsTPF6P8pszrRFOKqdKWGKsDpWBkcrvwJC02xOAQsW7b-
...
534-5.7.14 zFvhhWx1SQ-yRuGO0fa8JMMszw3E> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754 z16sm15149096pbt.3 - gsmtp

at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:424)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:307)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:296)

Go to the following URL:

https://www.google.com/settings/security/lesssecureapps

Set `Access for less secure apps` to `Turn on`.

Reference:
http://stackoverflow.com/questions/20337040/gmail-smtp-debug-error-please-log-in-via-your-web-browser

java.lang.IllegalArgumentException: Unknown ordinal value [100] for enum class [com.izeye.test.event.domain.EventSource]

If you try to fetch an undefined enum ordinal value in JPA backed by Hibernate,

you will get the following exception:

java.lang.IllegalArgumentException: Unknown ordinal value [100] for enum class [com.izeye.test.event.domain.EventSource]
    at org.hibernate.type.EnumType$OrdinalEnumValueMapper.fromOrdinal(EnumType.java:391)
    at org.hibernate.type.EnumType$OrdinalEnumValueMapper.getValue(EnumType.java:381)
    at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:107)
    at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:127)
    at org.hibernate.type.AbstractType.hydrate(AbstractType.java:106)
    at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2969)
    at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1696)

If you want to avoid the above exception and just get `null` instead,

add the following code:

@Converter(autoApply = true)
public class EventSourceConverter implements AttributeConverter<EventSource, Integer> {

  @Override
  public Integer convertToDatabaseColumn(EventSource eventSource) {
    return eventSource.ordinal();
  }

  @Override
  public EventSource convertToEntityAttribute(Integer ordinal) {
    EventSource[] values = EventSource.values();
    if (ordinal >= values.length) {
      return null;
    }
    return values[ordinal];
  }

}

Reference:
http://www.javacodegeeks.com/2014/05/jpa-2-1-type-converter-the-better-way-to-persist-enums.html

Saturday, August 15, 2015

org.hibernate.AssertionFailure: possible non-threadsafe access to session

When you are using a sequence for an entity

and persist it, detach it, and merge it in a transaction,

you will get the following error:

ERROR: HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: possible non-threadsafe access to session

You should flush it before merging it.

Unfortunately I didn't understand the reason.

I'm not sure it's a bug or wrong usage.

It was just a test code and I'm not sure it could be a real scenario or not.

This inspection controls whether the Persistence QL Queries are error-checked

If you get the following error in IntelliJ:

Can't resolve symbol 'Person' less... (⌘F1)
This inspection controls whether the Persistence QL Queries are error-checked

do the following:

Inspection 'Query language checks' options -> Disable inspection

Reference:
http://devday.tistory.com/entry/Cant-resolve-symbol-Note

Friday, August 14, 2015

ERROR: Sequence "HIBERNATE_SEQUENCE" not found

If you have the following with H2:

@Id
@GeneratedValue
private Long id;

you might get the following error:

Hibernate:
    call next value for hibernate_sequence
8월 15, 2015 2:49:23 오후 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 90036, SQLState: 90036
8월 15, 2015 2:49:23 오후 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Sequence "HIBERNATE_SEQUENCE" not found; SQL statement:
call next value for hibernate_sequence [90036-187]
ex: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not prepare statement

I guess `GenerationType.AUTO` uses `GenerationType.SEQUENCE`

and H2 doesn't support it.

I tried with `GenerationType.IDENTITY` as follows:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

and it works.

UPDATED:

Actually H2 DOES support `GenerationType.SEQUENCE`

and you should do as follows:

@Entity
@SequenceGenerator(
name = "USER_SEQ_GENERATOR",
sequenceName = "USER_SEQ",
initialValue = 1, allocationSize = 1)
public class User {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_SEQ_GENERATOR")
private Long id;

...
}

Wednesday, August 12, 2015

How to change a version of dependency managed by Spring Boot in Gradle

To change the following in `spring-boot-dependencies`:

<spring-data-releasetrain.version>Gosling-RC1</spring-data-releasetrain.version>

add the following in `build.gradle`:

ext['spring-data-releasetrain.version'] = 'Gosling-M1'

Reference:
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-customize-dependency-versions