Skip to content

Instantly share code, notes, and snippets.

@SpringBootApplication
public class SpringDataJpaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDataJpaApplication.class, args);
}
@Bean
public ApplicationRunner configure(UserRepository userRepository) {
return env -> {
User user1 = new User("beth", LocalDate.of(2020, Month.AUGUST, 3));
@Entity
@Table(name = "USERS")
public class User {
@Id
@GeneratedValue
private Long id;
private String username;
private LocalDate registrationDate;
public User() {}
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.manning.javapersistence</groupId>
<artifactId>springdatajpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springdatajpa</name>
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {SpringDataConfiguration.class})
public class HelloWorldSpringDataJPATest {
@Autowired
private MessageRepository messageRepository;
@Test
public void storeLoadMessage() {
Message message = new Message();
message.setText("Hello World from Spring Data JPA!");
public class HelloWorldHibernateTest {
private static SessionFactory createSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure().addAnnotatedClass(Message.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
applySettings(configuration.getProperties()).build();
return configuration.buildSessionFactory(serviceRegistry);
}
@Test
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/CH02?serverTimezone=UTC</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password" />
<property name="hibernate.connection.pool_size">50</property>
public class HelloWorldJPATest {
@Test
public void storeLoadMessage() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("ch02");
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Message message = new Message();
message.setText("Hello World!");
@Entity
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String text;
public String getText() {
return text;
}
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="ch02">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/CH02?serverTimezone=UTC " />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.6.9.Final</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>