Java로 서버 개발을 공부하다가 얼마 전에 Kotlin을 처음 해봤는데, 헷갈리는 부분이 있었다.
분명 null 값을 허용하려고 Long 타입을 사용했는데, 오류가 발생했다.
Kotlin에서는 null 값을 허용하려면 Long이 아니라 Long? 으로 선언해야 한다고 한다.
private long count; // null X, 0으로 초기화
private Long id; // null O, JPA 기본키로 많이 사용
Kotlin에는 Java처럼 primitive 타입이 따로 없으며, 모든 숫자 타입이 객체형이다.
var count: Long = 0 // null X
var id: Long? = null // null O
Java에서는 보통 이렇게 기본키를 선언한다.
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Kotlin에서는 JPA가 id 필드를 null 상태로 초기화해야 하기 때문에,
nullable 타입인 Long?으로 선언해야 한다.
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null
I've been using eurorx24.net for a few months now, and honestly, it's one of the most reliable online pharmacies I’ve come across. The delivery is discreet, the prices are fair, and the product range is exactly what I need. Highly recommend for anyone looking for a trustworthy source in Germany.