My linux world » Java Spring – Data JPA

Java Spring - Data JPA


Contents

Prerequistes

Please read : Java spring – quickstart

Dependencies

Add this to your pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
 
<!-- datasource -->
<dependency>
   <groupId>org.hsqldb</groupId>
   <artifactId>hsqldb</artifactId>
</dependency>

Configuration

MainConfig.java

Enable JPA Repositories like this:

@Configuration
@ComponentScan(basePackages="net.rabahi.java.jee.spring")
@EnableJpaRepositories(basePackages="net.rabahi.java.jee.spring.repository")
@EntityScan(basePackages="net.rabahi.java.jee.spring.model")
public class MainConfig {
 
}

application.properties

In src/main/resources/application.properties, you can configure hibernate :

hibernate.dialect = org.hibernate.dialect.HSQLDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.generate_statistics=true
hibernate.hbm2ddl.auto=create

The model file

package net.rabahi.java.jee.spring.model;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
 
@Entity
@Table(name="CONTACTS")
public class Contact {
 
	@Id
	@Column(name="ID")
	@GeneratedValue
	private Integer id;
 
	@Column(name="FIRSTNAME")
	private String firstName;
 
	@Column(name="LASTNAME")
	private String lastName;
 
	@Column(name="EMAIL")
	private String email;
 
	@Column(name="TELEPHONE")
	private String telephone;
 
// getters and setters
}

The repository

package net.rabahi.java.jee.spring.repository;
 
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import net.rabahi.java.jee.spring.model.Contact;
 
public interface ContactRepository extends JpaRepository<Contact,Integer>{
       Contact findByFirstName(String firstName);
 
       List<Contact> findByLastName(String lastName);
 
       @Query("SELECT f FROM Foo f WHERE LOWER(f.email) = LOWER(:email)")
       Foo retrieveByEmail(@Param("email") String email);
}

Use Repository

To use the ContactRepository, you have to inject it :

@Controller
public class ContactController {
 
	@Autowired
	private ContactRepository contactRepository;
 
// mapping
}

Then :

@RequestMapping("/contact/{contactId}", method=RequestMethod.DELETE)
public String delete(@PathVariable("contactId") Integer contactId) {
	contactRepository.delete(contactId);
        // your code here
}

Use JNDI DataSource (example for Apache Tomcat)

In your application.properties you can add this:

spring.datasource.jndi-name=java:comp/env/jdbc/mydatasource

And use it in the context file:

<Context>
<Resource name="jdbc/mydatasource" 
          auth="Container"
          type="javax.sql.DataSource"
          username="sa"
          password=""          
          url="jdbc:hsqldb:mem:database"
          driverClassName="org.hsqldb.jdbcDriver"
          initialSize="5"
          maxWait="5000"
          maxActive="120"
          maxIdle="5"              
          poolPreparedStatements="true"/>
</Context>

Copyright © 2023 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.

Fork me on GitHub