Created
May 10, 2018 09:02
-
-
Save jandk/b1ef9f207f8adb157b1325ddd059ba96 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import lombok.Data; | |
import lombok.Getter; | |
import lombok.NoArgsConstructor; | |
import org.hibernate.annotations.CreationTimestamp; | |
import org.springframework.boot.CommandLineRunner; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.data.jpa.domain.AbstractPersistable; | |
import org.springframework.data.repository.CrudRepository; | |
import javax.persistence.Entity; | |
import javax.persistence.MappedSuperclass; | |
import java.time.Instant; | |
import java.util.stream.Stream; | |
@SpringBootApplication | |
public class DemoApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(DemoApplication.class, args); | |
} | |
@Bean | |
public CommandLineRunner commandLineRunner(PersonRepository repository, ObjectMapper objectMapper) { | |
return args -> { | |
Stream.of("Jan", "Wouter") | |
.map(Person::new) | |
.forEach(repository::save); | |
Person p = repository.findByName("Jan"); | |
objectMapper.writeValue(System.out, p); | |
}; | |
} | |
} | |
@Getter | |
@MappedSuperclass | |
abstract class Auditable extends AbstractPersistable<Long> { | |
@CreationTimestamp | |
private Instant createdDate; | |
} | |
@Data | |
@NoArgsConstructor | |
@Entity | |
class Person extends Auditable { | |
private String name; | |
public Person(String name) { | |
this.name = name; | |
} | |
} | |
interface PersonRepository extends CrudRepository<Person, Long> { | |
Person findByName(String name); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment