DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Hibernate Projections Using Spring

Hibernate Projections Using Spring

Dale Wyttenbach user avatar by
Dale Wyttenbach
·
Dec. 09, 09 · News
Like (0)
Save
Tweet
Share
21.78K Views

Join the DZone community and get the full member experience.

Join For Free

Last week I wanted to learn about Hibernate projections, and while the online documentation was certainly helpful, I was wishing for a sample project that included some data to play with.  I couldn't find one, so I went ahead worked one up myself.

First I used JPA annotations to define a model class:

@Entity
@Table(name = "DOCUMENT")
public class Document {
@Id
@SequenceGenerator(name = "DOCUMENT_SEQ", sequenceName = "DOCUMENT_SEQ",allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "DOCUMENT_SEQ")
@Column(name = "DOCUMENT_ID")
private Long id;

@Column(name = "BUS_SEG")
private String busSeg;

@Column(name = "NOTE")
private String note;

@Column(name = "RCVD_DATE")
private Date receivedDate;
...

For reference, here is a corresponding Oracle table definition:

CREATE TABLE document (
document_id number(10) PRIMARY KEY,
bus_seg varchar2(256),
note varchar2(256),
rcvd_date date
);

Then I defined a simple DAO interface defining CRUD methods and implemented it by extending Spring's HibernateDAOSupport class:

public class HibernateDocumentDAO extends HibernateDaoSupport implements
DocumentDAO {

public Serializable create(Document document) {
return getHibernateTemplate().save(document);
}
...

Now for the projections bit. Once you have a few Documents persisted in your database you may want to see a summary count of the persisted Documents, as distributed over business segments (e.g., 'Federal', 'State', 'Commercial').  For good measure, you want to compute the minimum (oldest) received date of the Documents in each group, in order to locate Documents that have been waiting longest for human attention:

SELECT count(*), bus_seg, MIN(rcvd_date) 
FROM document
GROUP BY bus_seg;

You can write queries like that using Hibernate projections.  But how about a projection service that exposes the projection concept to the client side?  The service could be written in terms of a technology-neutral QuerySpecification for which a Hibernate implementation is provided:

		QuerySpecification spec = new QuerySpecification();
spec.addProjection(Projection.ROW_COUNT);
spec.addProjection(new QuerySpecification.Projection(DocumentDAO.BUS_SEG, QuerySpecification.Function.GROUP_BY));
spec.addProjection(new QuerySpecification.Projection(DocumentDAO.RCVD_DATE, QuerySpecification.Function.MIN));
List<Object[]> results = (List<Object[]>) documentService.query(spec);

Each member of the results collection represents a row returned from the Hibernate projection.  The first element of each row is an integer (the count), the second element is a String (the businesss segment), and the third element is a java.sql.Timestamp (the minimum received date)

I've deliberately left in a smelly bit of code in the QuerySpecification example above.  The client of the documentService is making use of String constants defined in the DAO interface.  At one level, this is because I was too lazy to re-define the property names in the service layer.  At another level, I think it is a sign that I am bending the object-relational model a bit here by exposing projections at the service layer.  Ideally, clients of the document service shouldn't know if Hibernate or even SQL is being used for persistence.  However, I hope you forgive the liberty taken if you see the power of using projections to summarize your data.

I have the complete example in the form of a maven project that I am willing to share with anyone interested.

 

 

 

Hibernate

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Exploring the Benefits of Cloud Computing: From IaaS, PaaS, SaaS to Google Cloud, AWS, and Microsoft
  • Using QuestDB to Collect Infrastructure Metrics
  • How To Use Terraform to Provision an AWS EC2 Instance
  • What Java Version Are You Running? Let’s Take a Look Under the Hood of the JDK!

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: