This documentation is not maintained. Please refer to doc.castsoftware.com/technologies to find the latest updates.

Summary: This document provides information about the extension providing Spring Data support for JEE.


What's new

  • Spring Data JDBC support
  • Spring JDBC support

Extension ID

com.castsoftware.springdata

What's new?

See Spring Data - 1.1 - Release Notes for more information.

Description

In what situation should you install this extension?

This extension is specifically targeted at the Spring Data framework and should be used in conjunction with the JEE Analyzer extension. CRUD operations and Named Queries (@NamedQuery and @NamedQueries Annotations) are supported for JPA and JDBC.

In addition, this extension provides support for JdbcTemplate which is the main Spring JDBC API, which allows access to almost all functionalities of this framework. We focus here on running basic queries and running queries with named parameters.

When client code uses any of these coding mechanisms, the extension will create the links from the calling method to the database table. This helps form the complete transaction.

Features

Managing Crud method calls

Spring Data Repository abstraction is used to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores. A number of crud methods are provided to improve data access. Some of the crud methods are as follows:

  • count
  • deleteById
  • delete
  • deleteAll
  • deleteAllInBatch
  • deleteInBatch
  • exists
  • existsById
  • findAll
  • findById
  • findOne
  • flush
  • save
  • saveAll
  • saveAndFlush

Using CRUD methods with Spring Data JPA

ProductServiceImpl.java

@Component
public class ProductServiceImpl implements ProductService{

	@Autowired
	private ProductRepository productRepository;

	@Transactional
	@Override
	public void add(Product product) {
		productRepository.save(product);
	}
	
	@Transactional(readOnly=true)
	@Override
	public List<Product> findAll() {
		return productRepository.findAll();
	}
	
	@Override
	public Product findById(long id) {
		return productRepository.findOne(id);
	}

Repository: ProductRepository.java

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
	
	/** No need to define findAll() here, because 
	 * 	inherited from JpaRepository with many other basic JPA operations.**/  
	public List<Product> findAll();


	/** spring-jpa-data understands this method name,
	 *  because it supports the resolution of specific keywords inside method names. **/
	public List<Product> findByNameContainingIgnoreCase(String searchString);
	
	
    /** You can define a JPA query.**/
	@Query("select p from Product p where p.name = :name")
    public List<Product> findByNameIs(@Param("name") String name);
	
	//Page<Product> finadAll(Pageable pageable);
	
	//List<Product> findByProductSku(SKU sku);
    
	/** This method will get query from Product class @NamedQuery Annotation  **/
	public List<Product> findByName(String name);

}

The code listed above will produce the following links and objects when the Spring Data extension is installed:

Using CRUD methods with Spring Data JDBC

Spring Data JDBC uses a syntax that is similar to Spring Data JPA. We can create a Spring Data JDBC repository by extending the Repository, CrudRepository, or PagingAndSortingRepository interface. By implementing CrudRepository, we receive the implementation of the most commonly used methods like save, delete, and findById, among others, as listed for above.

Custom JDBC Repository

In Spring Data JDBC, we write queries in plain SQL. Custom methods are decorated with the @Query annotation and inside we have the SQL query.

interface LegoSetRepository extends CrudRepository<LegoSet, Integer> {

	@Query("SELECT m.name model_name, m.description, l.name set_name" +
			"  FROM model m" +
			"  JOIN lego_set l" +
			"  ON m.lego_set = l.id" +
			"  WHERE :age BETWEEN l.min_age and l.max_age")
	List<ModelReport> reportModelForAge(@Param("age") int age);

	@Modifying
	@Query("UPDATE model set name = lower(name) WHERE name <> lower(name)")
	int lowerCaseMapKeys();
}
public void customQueries() {
	List<ModelReport> report = repository.reportModelForAge(6);
}

Named Queries

Using JPA NamedQueries

The @NamedQuery annotations can be used individually or can coexist in the class definition for an entity. The annotations define the name of the query, as well as the query text. In a real application, you will probably need multiple named queries defined on an entity class. For this, you will need to place multiple @NamedQuery annotations inside a @NamedQueries annotation.

Example @NamedQueries code:

Post.java

@Entity
@Table(name="POST")
@NamedQueries(  
	    {  
	        @NamedQuery( name = "@findCustomer", query = "from Customer" )  
	    }  
	)
@NamedQuery(name = "Post.fetchByTitle",
query = "SELECT p.title FROM Post p")
public class Post {
	@Id

Source code using @NamedQuery: Product.java

@Entity
@Table(name= "product")
@NamedQuery(name = "Product.findByName",query = "select p from Product p where p.name = ?1")
public class Product {
	
    @Id
   /* @GeneratedValue(strategy = GenerationType.IDENTITY)*/
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "id_Sequence")
    @Column(name = "id", updatable = false, nullable = false)
    @SequenceGenerator(name = "id_Sequence", sequenceName = "ID_SEQ")
	public Long id;
    public String name;

ProductServiceImpl.java

@Component
public class ProductServiceImpl implements ProductService{

@Autowired
private ProductRepository productRepository;

@Transactional(readOnly=true)
@Override
public List<Product> findByNameIs(String name) {
    productRepository.findByName(name);
    return productRepository.findByNameIs(name);
}

The code listed above will produce the following links and objects when the Spring Data extension is installed:

Using JPA NamedQueries via XML

NamedQuery works with annotations as well as with XML files. The application's web.xml file contains the param-value which indicates the XML file that contains the named query. Using the Spring Data extension, proper links can be created from the methods which call these queries to the data base table.

web.xml

<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<display-name>Spring-data Application</display-name>
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/spring-servlet.xml,/WEB-INF/orm.xml
		</param-value>
	</context-param>

orm.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

  <!--  <persistence-unit name="myUnit" transaction-type="RESOURCE_LOCAL">
    <mapping-file>META-INF/orm.xml</mapping-file>
    <exclude-unlisted-classes/>
  </persistence-unit> -->
  
<!-- Named Query using XML Configuration -->
	<named-query name="Post.fetchByTitle">
		<query>SELECT p FROM Product p WHERE p.name = ?1</query>
	</named-query>
</persistence>

PostService.java

@Component
public class PostService {
	@Autowired
	PostRepository repository;
	
	
	@Transactional
	public void add(Post post) {
		repository.save(post);
		repository.count();
	}
	
	@Transactional
	public void namedQueryCall(){
		List<Post> ret = repository.fetchByTitle();
	}
	@Transactional
	public void check()
	{
		repository.delete();
	}
	
	@Transactional
	public void countByEmailAddress()
	{
		repository.countByEmailAddressAndLastname();
	}

PostRepository.java

public interface PostRepository extends JpaRepository<Post, Integer> {
	
	List<Post> countByEmailAddressAndLastname(String emailAddress, String lastname);
	
}

The code listed above will produce the following links and objects when the Spring Data extension is installed. A link will be created from the method namedQueryCall to the table POST:

Using JDBC NamedQueries

@Entity
@Table(name = "SCHEDULING_ORDER")
@NamedQueries({ @NamedQuery(name = "Order.findByOrderNumberAndAppointmentType",
query = "SELECT o FROM Order o LEFT JOIN FETCH o.appointments "
		+ "WHERE o.orderNumber = :orderNumber and o.appointmentType = :appointmentType") })
public class Order { 
}
@Override
public Order findByOrderNumberAndAppointmentType(final OrderNumber orderNumber,
    final AppointmentType appointmentType) throws ObjectNotFoundException
{
    Query query = this.em.createNamedQuery("Order.findByOrderNumberAndAppointmentType");
}

Using JDBC NamedQueries via XML

<named-query name="ActivityTask.queryActivityById">
	<query>SELECT activityId FROM ActivityTask</query>
</named-query>

<entity class="com.amdocs.oss.aff.omx.impl.model.ActivityTask"
		access="FIELD" metadata-complete="true">

	<table name="ACTIVITY_TASK_TABLE" />

	<attributes>

		<id name="activityId">
			<column name="ACTIVITY_ID" />
		</id>
		<basic name="taskId">
			<column name="TASK_ID" updatable="true" />
		</basic>
		<basic name="projectId">
			<column name="PROJECT_ID" updatable="true" />
		</basic>
	</attributes>
</entity>
private static final String QUERY_ACTIVITY_BY_ID = "ActivityTask.queryActivityById";

public  ActivityTask getActivityByActivityId(String activityId) {

		EntityManager entityManager = emp.getEntityManager();
		Query query = entityManager.createNamedQuery(QUERY_ACTIVITY_BY_ID );
		query.setParameter("activityId", activityId);
		ActivityTask activityTask = (ActivityTask) query.getSingleResult();
		return activityTask;
}

Creating query by inference in Spring Data JPA

The query builder mechanism of Spring Data is useful for building queries over entities of the repository. The mechanism is to create the query for patterns such as  find..By, read..By, query..By, count..By, and get..By. Spring Data parses this string as it may contain further expressions, such as a Distinct to set a distinct flag on the query to be created. However, the first By acts as delimiter to indicate the start of the actual criteria. If such a scenario is used and the methods declared in the repository are used in some other method then using the Spring Data extension the transaction link can be drawn from the method to the database table.
 

Postservice.java

@Component
public class PostService {
	@Autowired
	PostRepository repository;
	
	
	@Transactional
	public void add(Post post) {
		repository.save(post);
		repository.count();
	}
	
	@Transactional
	public void namedQueryCall(){
		List<Post> ret = repository.fetchByTitle();
	}
	@Transactional
	public void check()
	{
		repository.delete();
	}
	
	@Transactional
	public void countByEmailAddress()
	{
		repository.countByEmailAddressAndLastname();
	}
	
	@Transactional
	public void findUsingEmail()
	{
		repository.findByEmail();
	}
	
	@Transactional
	public void readUsingEmail()
	{
		repository.readByEmail();
	}
	
	@Transactional
	public void getUsingEmail()
	{
		repository.getByEmail();
	}

}

PostRepository.java

public interface PostRepository extends JpaRepository<Post, Integer> {
	
	List<Post> findByEmail(String email);
	List<Post> readByEmail(String email);
	List<Post> queryByEmail(String email);
	List<Post> getByEmail(String email);
}

The code listed above will produce the following links and objects when the Spring Data extension is installed:

 

Handling Query Dsl in Spring Data JPA

Querydsl is a framework which enables the construction of statically typed SQL-like queries, instead of writing queries as inline stringsQuerydsl for JPA is an alternative to both JPQL and Criteria queries. Querydsl for JPA/Hibernate is an alternative to both JPQL and JPA 2 Criteria queries. It combines the dynamic nature of Criteria queries with the expressiveness of JPQL and all that in a fully typesafe manner. Using the Spring Data extension the link between the function which uses the query dsl to the JPA enity related to the entity used in query dsl can be identified.

Example: Product.java

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="PRODUCT")
public class Product {

	@Id
	private Long id;
	
	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;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	private String name;
	
	private double price;
	
	@ManyToOne
	private Category category;
}

DemoService.java

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import static com.mysema.demo.QProduct.product;

import com.querydsl.jpa.impl.JPAQuery;

public class DemoService {
	
	public List<Product> findProductsByNameAndCategoryId(String name, Long categoryId){
		EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistence");
        EntityManager entityManager = entityManagerFactory.createEntityManager();

		QCategory cat = QCategory.category;
		JPAQuery qry = new JPAQuery(entityManager);
		
		createQuery(QProduct.product,qry);
		
		if(name != null){
			qry.where(product.name.like(name));
		}
		
		if(categoryId != null){
			qry.where(product.category.catId.eq(categoryId));
		}
		
		return qry.fetch();
	}

	private JPAQuery createQuery(QProduct product,JPAQuery qr) {
		return (JPAQuery) qr.from(product);
	}

}

The following links are created with above code when the Spring Data extension is used:

Support for Spring Boot Starter

Spring-boot-starter-data-jpa POM provides a quick way to get started. It provides the following key dependencies

  • Hibernate: One of the most popular JPA implementations.
  • Spring Data JPA: Makes it easy to implement JPA-based repositories.

The Spring Boot application invokes the application which uses the Spring Data JPA. For example, the source code below shows how the Spring Boot Starter invokes the Spring Data JPA application:

Application.java

import org.springframework.beans.factory.annotation.Autowired;
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.web.config.EnableSpringDataWebSupport;

import com.onlinetutorialspoint.entity.Person;
import com.onlinetutorialspoint.repository.PersonRepository;
import com.onlinetutorialspoint.service.PersonService;

@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class);
	}
	@Autowired
	PersonService personService;
	@Bean
	public CommandLineRunner run(PersonRepository repository) {
		return (args) -> {
			Person person = new Person();
			person.setName("Chandra Shekhar Goka");
			person.setCity("Hyderabad");
			Person p = savePersonDetails(person);
			
			System.out.println("Person Id : "+p.getId() +" Person Name : "+p.getName());
		};
	}
	
	public Person savePersonDetails(Person p){
		return personService.savePerson(p);
	}
	
	public Person getPerson(Person person){
		return personService.getPerson(person.getId());
	}
}

PersonService.java

@Service
@Transactional
public class PersonService {
	@Autowired
	PersonRepository personRepo;

	public void savePersonDetails(PersonDTO personDto) {
		try {
			Person person = new Person();
			person.setCity(personDto.getpCity());
			person.setName(personDto.getpName());
			person.setId(personDto.getPid());
			personRepo.save(person);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Person savePerson(Person person) {
		return personRepo.save(person);
	}
}

Person.java

@Entity
@Table(name = "person")
public class Person {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	private String name;
	@Column(name="pcity")
	private String city;

	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}

	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;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	@Override
	public String toString() {
		return "Person [pid=" + id + ", pName=" + name + ", pCity=" + city
				+ "]";
	}

}

The code above will produce the following objects and links:

Support for Spring JDBC

To populate the databases, Spring JDBC JdbcTemplates and NamedParameterJdbcTemplate APIs are used. This extension supports them also.

Basic queries with JdbcTemplate

public class PersonDAOImpl implements PersonDAO {

	JdbcTemplate jdbcTemplate;
    
	private static final String SQL_FIND_PERSON = "select * from people where id = ?";
	private static final String SQL_DELETE_PERSON = "delete from people where id = ?";
	private static final String SQL_UPDATE_PERSON = "update people set first_name = ?, last_name = ?, age  = ? where id = ?";
	private static final String SQL_GET_ALL = "select * from people";
	private static final String SQL_INSERT_PERSON = "insert into people(id, first_name, last_name, age) values(?,?,?,?)";

	@Autowired
	public PersonDAOImpl(DataSource dataSource) {
		jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public Person getPersonById(Long id) {
		return jdbcTemplate.queryForObject(SQL_FIND_PERSON, new Object[]{id}, new PersonMapper());
	}

	public List<Person> getAllPersons() {
		return jdbcTemplate.query(SQL_GET_ALL, new PersonMapper());
	}

	public boolean deletePerson(Person person) {
		return jdbcTemplate.update(SQL_DELETE_PERSON, person.getId()) > 0;
	}

	public boolean updatePerson(Person person) {
		return jdbcTemplate.update(SQL_UPDATE_PERSON, person.getFirstName(), person.getLastName(), person.getAge(),
				person.getId()) > 0;
	}

	public boolean createPerson(Person person) {
		return jdbcTemplate.update(SQL_INSERT_PERSON, person.getId(), person.getFirstName(), person.getLastName(),
				person.getAge()) > 0;
	}
}

Queries with NamedParameterJdbcTemplate

public Map<Long, UserDetail> getUsers(String subQuery, Map<String, Object> parameterValues) {
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    MapSqlParameterSource parameters = new MapSqlParameterSource(parameterValues);
	String sqlQuery = "select usr as assigneeUser from Assigned_Users" 
					+ " where usr_id in ( " + subQuery + " )";
	userDetails = namedParameterJdbcTemplate.query(sqlQuery, parameters);
}

SimpleJdbcInsert

This API provides insert capabilities into a table. The methods calling execute(), executeBatch() or executeAndReturnKey() will be linked to tables. The withTableName() call helps identify the table.

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;

@Repository
public class Main
{
   private JdbcTemplate jdbcTemplate;
   private SimpleJdbcInsert simpleJdbcInsert;
   private SimpleJdbcCall simpleJdbcCall;

   @Autowired
   public void setDataSource(DataSource dataSource)
   {
      this.jdbcTemplate = new JdbcTemplate(dataSource);
      simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate).withTableName(
            "Persons").usingGeneratedKeyColumns("id");
      this.simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate);
      Map<String, Object> args = new HashMap<String, Object>(2);
      args.put("personid", task.getPersonId());
      simpleJdbcInsert.execute(args);
   }
}

SimpleJdbcCall

This API represents a call to a stored procedure or a stored function. In this extension we handle the stored procedures. The methods calling execute() will be linked to procedures. The withProcedureName() call helps us identify the procedure.

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;

class Main
{
	private JdbcTemplate jdbcTemplateObject;
	public void setDataSource(DataSource dataSource) {
      this.dataSource = dataSource;
      this.jdbcTemplateObject = new JdbcTemplate(dataSource);
    }
    public Student getStudent(Integer id) {
      SimpleJdbcCall jdbcCall = new SimpleJdbcCall(dataSource).withProcedureName("getRecord");
      SqlParameterSource in = new MapSqlParameterSource().addValue("in_id", id);
      Map<String, Object> out = jdbcCall.execute(in);
    }
}

Table declaration:

CREATE TABLE Student(
   ID   INT NOT NULL AUTO_INCREMENT,
   NAME VARCHAR(20) NOT NULL,
   AGE  INT NOT NULL,
   PRIMARY KEY (ID)
);

Procedure declaration:

DELIMITER $$

DROP PROCEDURE IF EXISTS `TEST`.`getRecord` $$
CREATE PROCEDURE `TEST`.`getRecord` (
IN in_id INTEGER,
OUT out_name VARCHAR(20),
OUT out_age  INTEGER)
BEGIN
   SELECT name, age
   INTO out_name, out_age
   FROM Student where id = in_id;
END $$

DELIMITER ;

Function Point, Quality and Sizing support

This extension provides the following support:

Function Points
(transactions)
Quality and Sizing
(tick)(error)

CAST AIP compatibility

This extension is compatible with:

CAST AIP release

Supported

8.3.x(tick)
8.2.x(tick)

Supported DBMS servers

This extension is compatible with the following DBMS servers:

CAST AIP releaseCSSOracleMicrosoft
All supported releases (see above)(tick)(tick)(error)

Prerequisites

(tick)

An installation of any compatible release of CAST AIP (see table above)

Dependencies with other extensions

Some CAST extensions require the presence of other CAST extensions in order to function correctly. The SPRING-DATA extension requires that the following other CAST extensions are also installed:

  • com.castsoftware.internal.platform (internal technical extension).

Note that when using the CAST Extension Downloader to download the extension and the Manage Extensions interface in CAST Server Manager to install the extension, any dependent extensions are automatically downloaded and installed for you. You do not need to do anything.

Download and installation instructions

Please see:

The latest release status of this extension can be seen when downloading it from the CAST Extend server.

Packaging, delivering and analyzing your source code

Once the extension is installed, no further configuration changes are required before you can package your source code and run an analysis.

What results can you expect?

This extension will create the links between objects that are created by the JEE analyzer, the JAVA Methods, and the TABLES created by the SQL Analyzer.