jodatime
YearMonth
개체를 SQL 데이터베이스에 유지하는 방법은 무엇입니까?Joda Date를 SQL 데이터베이스에 유지하는 방법은 무엇입니까?
@Entity
public class MyEntity {
private YearMonth date; //how to persist?
//some more properties
}
나중에 특정 달까지 모든 항목을 선택할 수 있기를 바랍니다.
jodatime
YearMonth
개체를 SQL 데이터베이스에 유지하는 방법은 무엇입니까?Joda Date를 SQL 데이터베이스에 유지하는 방법은 무엇입니까?
@Entity
public class MyEntity {
private YearMonth date; //how to persist?
//some more properties
}
나중에 특정 달까지 모든 항목을 선택할 수 있기를 바랍니다.
당신은 (How to convert java.util.Date to Java8 java.time.YearMonth 참조) 이것에 대한 javax.persistence.Converter이 필요합니다
@Entity
public class MyEntity {
@Convert(converter = YearMonthConverter.class)
private YearMonth date; //how to persist?
//some more properties
}
다음은 java.utilDate에 Java8 java.time.YearMonth에 대한 예를 변환기입니다. 당신이 원하는 유형에서 /로 변환 org.joda.time.YearMonth에 java.time.YearMonth을 변경하고 적절한 방법을 사용해야합니다 (생략 수입) :
@Converter(autoApply = true)
public class YearMonthConverter implements AttributeConverter<YearMonth, Date> {
@Override
public Date convertToDatabaseColumn(YearMonth attribute) {
// uses default zone since in the end only dates are needed
return attribute == null ? null : Date.from(attribute.atDay(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
}
@Override
public YearMonth convertToEntityAttribute(Date dbData) {
// TODO: check if Date -> YearMonth can't be done in a better way
if (dbData == null) return null;
Calendar calendar = Calendar.getInstance();
calendar.setTime(dbData);
return YearMonth.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1);
}
}
이 더 나은 질문은 어떻게되어 당신은 데이터베이스에있는 데이터를 사용하려고합니까? 일반적으로 문자열이나 복합 정수가 아닌 대상 데이터베이스가 지원하는 데이터 유형을 사용하여 데이터베이스에 날짜를 저장하려고합니다. 이렇게하면 응용 프로그램에서 해당 논리를 구현해야하는 것과 달리 내장 된 데이터베이스 날짜/시간 함수와 유효성 검사를 사용할 수 있습니다.
엔티티에 엔티티를 추가하여 jodatime 값을 필요한 엔티티의 엔티티로 변환 할 수 있습니다.
최대 절전 모드를 JPA 공급자로 사용하고 있습니까? – sol4me
나는 년월을 int 필드로 저장하므로 내 엔티티는 조다 타임에 의존하지 않는다 – isalgueiro
예 최대 절전 모드를 사용합니다. – membersound