Skip to content

Instantly share code, notes, and snippets.

@garcia-jj
Last active August 29, 2015 14:00
Show Gist options
  • Save garcia-jj/11378575 to your computer and use it in GitHub Desktop.
Save garcia-jj/11378575 to your computer and use it in GitHub Desktop.
JPA with java.time
/*
* Copyright 2014 Otávio S Garcia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package br.com.otavio.jpa.converters;
import java.sql.Date;
import java.time.LocalDate;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
/**
* Convert a {@link LocalDate} into database column representation as {@link Date} and back again.
*
* @author Otávio S Garcia
*/
@Converter(autoApply = true)
public class PersistentLocalDate implements AttributeConverter<LocalDate, Date> {
@Override
public Date convertToDatabaseColumn(LocalDate value) {
return value == null ? null : Date.valueOf(value);
}
@Override
public LocalDate convertToEntityAttribute(Date value) {
return value == null ? null : value.toLocalDate();
}
}
/*
* Copyright 2014 Otávio S Garcia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package br.com.otavio.jpa.converters;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
/**
* Convert a {@link LocalDateTime} into database column representation as {@link Date} and back again.
*
* @author Otávio S Garcia
*/
@Converter(autoApply = true)
public class PersistentLocalDateTime implements AttributeConverter<LocalDateTime, Timestamp> {
@Override
public java.sql.Timestamp convertToDatabaseColumn(LocalDateTime value) {
return value == null ? null : Timestamp.valueOf(value);
}
@Override
public LocalDateTime convertToEntityAttribute(Timestamp value) {
return value == null ? null : value.toLocalDateTime();
}
}
/*
* Copyright 2014 Otávio S Garcia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package br.com.otavio.jpa.converters;
import java.sql.Date;
import java.time.YearMonth;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
/**
* Convert a {@link YearMonth} into database column representation as {@link Date} and back again.
*
* @author Otávio S Garcia
*/
@Converter(autoApply = true)
public class PersistentYearMonth implements AttributeConverter<YearMonth, Date> {
@Override
public Date convertToDatabaseColumn(YearMonth value) {
return value == null ? null : Date.valueOf(value.atDay(1));
}
@Override
public YearMonth convertToEntityAttribute(Date value) {
return value == null ? null : YearMonth.from(value.toLocalDate());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment