Sure, here's an example of a Java Spring JPA application that uses SQLite as a database, with an entity that has two fields: id (long) and name (String).
// pom.xml (Maven dependencies)
<dependencies>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-jpa</artifactid>
</dependency>
<dependency>
<groupid>org.xerial</groupid>
<artifactid>sqlite-jdbc</artifactid>
<version>3.45.0.0</version>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-test</artifactid>
<scope>test</scope>
</dependency>
</dependencies>
// src/main/java/com/example/demo/entity/MyEntity.java
package com.example.demo.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
// src/main/java/com/example/demo/repository/MyEntityRepository.java
package com.example.demo.repository;
import com.example.demo.entity.MyEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MyEntityRepository extends JpaRepository<myentity, long=""> {
}
// src/main/java/com/example/demo/DemoApplication.java
package com.example.demo;
import com.example.demo.entity.MyEntity;
import com.example.demo.repository.MyEntityRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public CommandLineRunner demo(MyEntityRepository repository) {
return (args) -> {
// Save a few entities
repository.save(new MyEntity("First"));
repository.save(new MyEntity("Second"));
// Fetch all entities
System.out.println("Entities found with findAll():");
System.out.println("-------------------------------");
repository.findAll().forEach(entity -> {
System.out.println(entity.toString());
});
System.out.println();
};
}
}
# src/main/resources/application.properties
spring.datasource.url=jdbc:sqlite:mydatabase.db
spring.datasource.driver-class-name=org.sqlite.JDBC
spring.jpa.database-platform=org.hibernate.dialect.SQLiteDialect
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
Key points and explanations:
- Dependencies (pom.xml): The
spring-boot-starter-data-jpa dependency provides the necessary Spring Data JPA functionality. The sqlite-jdbc dependency is the JDBC driver for SQLite.
- Entity (MyEntity.java): This class represents the database table.
@Entity marks it as a JPA entity. @Id and @GeneratedValue define the primary key id.
- Repository (MyEntityRepository.java): This interface extends
JpaRepository, providing methods for basic CRUD operations (create, read, update, delete) on the MyEntity objects. Spring Data JPA automatically generates the implementation for this interface.
- Application (DemoApplication.java): This is the main Spring Boot application class. The
CommandLineRunner bean demonstrates how to use the repository to save and retrieve data when the application starts.
- application.properties: This file configures the database connection.
spring.datasource.url specifies the JDBC URL for the SQLite database file (mydatabase.db). spring.datasource.driver-class-name sets the JDBC driver class. spring.jpa.hibernate.ddl-auto=create-drop tells Hibernate to create the database schema on startup and drop it when the application stops. spring.jpa.show-sql=true will output all SQL queries to the console.
To run this application:
- Make sure you have Java and Maven installed.
- Create the project structure as shown above.
- Place the code in the corresponding files.
- Run the application using
mvn spring-boot:run.
This will create an SQLite database file named mydatabase.db in the project directory (or the directory where you run the application from), create the MyEntity table, insert two rows, and then print the contents of the table to the console.</myentity,>