|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springsource.restbucks.core; |
| 17 | + |
| 18 | +import java.time.Year; |
| 19 | + |
| 20 | +import javax.persistence.AttributeConverter; |
| 21 | +import javax.persistence.Converter; |
| 22 | + |
| 23 | +import org.hibernate.annotations.Immutable; |
| 24 | + |
| 25 | +/** |
| 26 | + * JPA {@link AttributeConverter} implementation to map {@link Year} to an {@link Integer} to avoid Hibernates |
| 27 | + * serializable type handling kick in. |
| 28 | + * |
| 29 | + * @author Oliver Drotbohm |
| 30 | + * @see https://hibernate.atlassian.net/browse/HHH-10558 |
| 31 | + */ |
| 32 | +@Immutable |
| 33 | +@Converter(autoApply = true) |
| 34 | +public class YearAttributeConverter implements AttributeConverter<Year, Integer> { |
| 35 | + |
| 36 | + @Override |
| 37 | + public Integer convertToDatabaseColumn(Year year) { |
| 38 | + return year == null ? null : year.getValue(); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public Year convertToEntityAttribute(Integer source) { |
| 43 | + return source == null ? null : Year.of(source.intValue()); |
| 44 | + } |
| 45 | +} |
0 commit comments