- NoArgConstructor, Getter, Setter ⇒ kotlin 기본제공
- AllArgConstructor ⇒ named argument
- JPA를 사용한다면 변경될 수 있는 프로퍼티를 var로 선언해주어야 한다.
// java
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Builder
@Entity
@Table(name = "todos")
public class Todo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "title")
private String title;
@Lob
@Column(name = "description")
private String description;
@Column(name = "done")
private Boolean done;
@Column(name = "created_at")
private LocalDateTime createdAt;
@Column(name = "updated_at")
private LocalDateTime updatedAt;
public void update(String title, String description, Boolean done) {
this.title = title;
this.description = description;
this.done = done != null && done;
this.updatedAt = LocalDateTime.now();
}
}
// kotlin
@Entity
@Table(name = "todos")
class Todo(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = 0,
@Column(name = "title")
var title: String,
@Lob
@Column(name = "description")
var description: String,
@Column(name = "done")
var done: Boolean,
@Column(name = "created_at")
var createdAt: LocalDateTime,
@Column(name = "updated_at")
var updatedAt: LocalDateTime? = null,
) {
fun update(title: String, description: String, done: Boolean) {
this.title = title
this.description = description
this.done = done
this.updatedAt = LocalDateTime.now()
}
}
TodoRepository
// java
public interface TodoRepository extends JpaRepository<Todo, Long> {
Optional<List<Todo>> findAllByDoneIsFalseOrderByIdDesc();
}
// kotlin
interface TodoRepository : JpaRepository<Todo, Long> {
fun findAllByDoneIsFalseOrderByIdDesc(): List<Todo>?
}