Jack has posted 1 posts at DZone. View Full User Profile

Generating Enterprise Class GWT Applications for Spring

08.06.2010
| 15635 views |
  • submit to reddit

The Code

The best way to get a feel for the code is to download MyEclipse for Spring and walk through the GWT Tutorial. Let’s take a quick look at some of the generated code to get a feel for it. We are absolutely welcome to feedback from the community as to how we may continue to improve the product and so if you have thoughts please post them on our forums.

Server Side Classes

Customer

The Customer class is the JPA Entity that was generated in this case from our RDBMS table definition. You can see that we automatically generate @NamedQuery annotations for the common named queries you may need to search for Customers. Each field has been annotated based on information derived from the database, generally pretty standard stuff.

package com.showme.domain;

import java.io.Serializable;

import java.lang.StringBuilder;

import java.math.BigDecimal;

import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

import javax.xml.bind.annotation.*;

import javax.persistence.*;

/**
 */

@Entity
@NamedQueries( {
		@NamedQuery(name = "findAllCustomers", query = "select myCustomer from Customer myCustomer"),
		@NamedQuery(name = "findCustomerByAddressline1", query = "select myCustomer from Customer myCustomer where myCustomer.addressline1 = ?1"),
		@NamedQuery(name = "findCustomerByAddressline1Containing", query = "select myCustomer from Customer myCustomer where myCustomer.addressline1 like ?1"),
		@NamedQuery(name = "findCustomerByAddressline2", query = "select myCustomer from Customer myCustomer where myCustomer.addressline2 = ?1"),
		@NamedQuery(name = "findCustomerByAddressline2Containing", query = "select myCustomer from Customer myCustomer where myCustomer.addressline2 like ?1"),
		@NamedQuery(name = "findCustomerByCity", query = "select myCustomer from Customer myCustomer where myCustomer.city = ?1"),
		@NamedQuery(name = "findCustomerByCityContaining", query = "select myCustomer from Customer myCustomer where myCustomer.city like ?1"),
		@NamedQuery(name = "findCustomerByContactfirstname", query = "select myCustomer from Customer myCustomer where myCustomer.contactfirstname = ?1"),
		@NamedQuery(name = "findCustomerByContactfirstnameContaining", query = "select myCustomer from Customer myCustomer where myCustomer.contactfirstname like ?1"),
		@NamedQuery(name = "findCustomerByContactlastname", query = "select myCustomer from Customer myCustomer where myCustomer.contactlastname = ?1"),
		@NamedQuery(name = "findCustomerByContactlastnameContaining", query = "select myCustomer from Customer myCustomer where myCustomer.contactlastname like ?1"),
		@NamedQuery(name = "findCustomerByCountry", query = "select myCustomer from Customer myCustomer where myCustomer.country = ?1"),
		@NamedQuery(name = "findCustomerByCountryContaining", query = "select myCustomer from Customer myCustomer where myCustomer.country like ?1"),
		@NamedQuery(name = "findCustomerByCreditlimit", query = "select myCustomer from Customer myCustomer where myCustomer.creditlimit = ?1"),
		@NamedQuery(name = "findCustomerByCustomername", query = "select myCustomer from Customer myCustomer where myCustomer.customername = ?1"),
		@NamedQuery(name = "findCustomerByCustomernameContaining", query = "select myCustomer from Customer myCustomer where myCustomer.customername like ?1"),
		@NamedQuery(name = "findCustomerByCustomernumber", query = "select myCustomer from Customer myCustomer where myCustomer.customernumber = ?1"),
		@NamedQuery(name = "findCustomerByPhone", query = "select myCustomer from Customer myCustomer where myCustomer.phone = ?1"),
		@NamedQuery(name = "findCustomerByPhoneContaining", query = "select myCustomer from Customer myCustomer where myCustomer.phone like ?1"),
		@NamedQuery(name = "findCustomerByPostalcode", query = "select myCustomer from Customer myCustomer where myCustomer.postalcode = ?1"),
		@NamedQuery(name = "findCustomerByPostalcodeContaining", query = "select myCustomer from Customer myCustomer where myCustomer.postalcode like ?1"),
		@NamedQuery(name = "findCustomerByPrimaryKey", query = "select myCustomer from Customer myCustomer where myCustomer.customernumber = ?1"),
		@NamedQuery(name = "findCustomerBySalesrepemployeenumber", query = "select myCustomer from Customer myCustomer where myCustomer.salesrepemployeenumber = ?1"),
		@NamedQuery(name = "findCustomerByState", query = "select myCustomer from Customer myCustomer where myCustomer.state = ?1"),
		@NamedQuery(name = "findCustomerByStateContaining", query = "select myCustomer from Customer myCustomer where myCustomer.state like ?1") })
@Table(schema = "CLASSICCARS", name = "CUSTOMER")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ShowMeTheMoney/com/showme/domain", name = "Customer")
@XmlRootElement(namespace = "ShowMeTheMoney/com/showme/domain")
public class Customer implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 */

	@Column(name = "CUSTOMERNUMBER", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@XmlElement
	Integer customernumber;
	/**
	 */

	@Column(name = "CUSTOMERNAME", length = 50)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String customername;
//** This is only a partial listing of the generated file **//
Full listing of Customer.java

CustomerDAOImpl

The CustomerDAOImpl class is the implementation of the CustomerDAO interface which defines the behaviors required to persist Customers. This class is an annotated Spring @Repository and which has @Transactional annotations for transaction management. The EntityManager is injected by Spring. There are methods for each of the NamedQueries from the Customer JPA Object. This class is used by the Service Layer to manage the persistence of Customers.

package com.showme.dao;

import com.showme.domain.Customer;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Customer entities.
 * 
 */
@Repository("CustomerDAO")
@Transactional
public class CustomerDAOImpl extends AbstractJpaDao implements CustomerDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set> dataTypes = new HashSet>(Arrays.asList(new Class<?>[] { Customer.class }));

	/**
	 * EntityManager injected by Spring for persistence unit MyEclipse_Derby
	 *
	 */
	@PersistenceContext(unitName = "MyEclipse_Derby")
	private EntityManager entityManager;

	/**
	 * Instantiates a new CustomerDAOImpl
	 *
	 */
	public CustomerDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit MyEclipse_Derby
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findCustomerByCreditlimit
	 *
	 */
	@Transactional
	public Set findCustomerByCreditlimit(java.math.BigDecimal creditlimit) throws DataAccessException {

		return findCustomerByCreditlimit(creditlimit, -1, -1);
	}

	/**
	 * JPQL Query - findCustomerByCreditlimit
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set findCustomerByCreditlimit(java.math.BigDecimal creditlimit, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCustomerByCreditlimit", startResult, maxRows, creditlimit);
		return new LinkedHashSet(query.getResultList());
	}

Full listing of CustomerDAOImpl.java

CustomerServiceImpl

The CustomerServiceImpl class is the implementation for the CustomerService interface which defines the behavior for a CRUD service for Customer. This class is a Session scoped bean and is annotated as a Spring @Service. This class has the DAO’s it needs injected by Spring and has methods to manage the basic CRUD operations as well as dependency management for Customer.

package com.showme.service;

import com.showme.dao.CustomerDAO;
import com.showme.dao.PaymentDAO;

import com.showme.domain.Customer;
import com.showme.domain.Payment;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Scope;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Customer entities
 * 
 */
@Scope("session")
@Service("CustomerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {

	/**
	 * DAO injected by Spring that manages Customer entities
	 * 
	 */
	@Autowired
	private CustomerDAO customerDAO;

	/**
	 * DAO injected by Spring that manages Payment entities
	 * 
	 */
	@Autowired
	private PaymentDAO paymentDAO;

	/**
	 * Instantiates a new CustomerServiceImpl.
	 *
	 */
	public CustomerServiceImpl() {
	}

	/**
	 * Load an existing Customer entity
	 * 
	 */
	@Transactional
	public Set loadCustomers() {
		return customerDAO.findAllCustomers();
	}

	/**
	 * Save an existing Customer entity
	 * 
	 */
	@Transactional
	public void saveCustomer(Customer customer) {
		customer = customerDAO.store(customer);
		customerDAO.flush();
	}
//** This is only a partial listing of the generated file **//
Full listing of CustomerServiceImpl.java

Published at DZone with permission of its author, Jack Kennedy.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)