Skip to content

Instantly share code, notes, and snippets.

@mikaelhg
Created November 26, 2011 10:08
Show Gist options
  • Save mikaelhg/1395408 to your computer and use it in GitHub Desktop.
Save mikaelhg/1395408 to your computer and use it in GitHub Desktop.
Spring Data JPA example snippets
@Autowired
private ExampleDao dao;
model.put("examples", dao.findAll());
model.put("comedies", dao.findByNameLike("%y%"));
12:16:57.385 [qtp400586934-21 - /] DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 13223026173
12:16:57.386 [qtp400586934-21 - /] DEBUG o.h.e.criteria.CriteriaQueryCompiler - Rendered criteria query -> select generatedAlias0 from com.gueck.spring3.Example as generatedAlias0 where generatedAlias0.name like :param0
12:16:57.386 [qtp400586934-21 - /] DEBUG o.h.hql.ast.QueryTranslatorImpl - parse() - HQL: select generatedAlias0 from com.gueck.spring3.Example as generatedAlias0 where generatedAlias0.name like :param0
12:16:57.389 [qtp400586934-21 - /] DEBUG org.hibernate.hql.ast.AST - --- HQL AST ---
\-[QUERY] Node: 'query'
+-[SELECT_FROM] Node: 'SELECT_FROM'
| +-[FROM] Node: 'from'
| | \-[RANGE] Node: 'RANGE'
| | +-[DOT] Node: '.'
| | | +-[DOT] Node: '.'
| | | | +-[DOT] Node: '.'
| | | | | +-[IDENT] Node: 'com'
| | | | | \-[IDENT] Node: 'gueck'
| | | | \-[IDENT] Node: 'spring3'
| | | \-[IDENT] Node: 'Example'
| | \-[ALIAS] Node: 'generatedAlias0'
| \-[SELECT] Node: 'select'
| \-[IDENT] Node: 'generatedAlias0'
\-[WHERE] Node: 'where'
\-[LIKE] Node: 'like'
+-[DOT] Node: '.'
| +-[IDENT] Node: 'generatedAlias0'
| \-[IDENT] Node: 'name'
\-[COLON] Node: ':'
\-[IDENT] Node: 'param0'
12:16:57.390 [qtp400586934-21 - /] DEBUG org.hibernate.hql.ast.ErrorCounter - throwQueryException() : no errors
12:16:57.390 [qtp400586934-21 - /] DEBUG o.h.hql.antlr.HqlSqlBaseWalker - select << begin [level=1, statement=select]
12:16:57.390 [qtp400586934-21 - /] DEBUG o.hibernate.hql.ast.tree.FromElement - FromClause{level=1} : com.gueck.spring3.Example (generatedAlias0) -> example0_
12:16:57.390 [qtp400586934-21 - /] DEBUG o.h.hql.ast.tree.FromReferenceNode - Resolved : generatedAlias0 -> example0_.id
12:16:57.391 [qtp400586934-21 - /] DEBUG o.h.hql.ast.tree.FromReferenceNode - Resolved : generatedAlias0 -> example0_.id
12:16:57.392 [qtp400586934-21 - /] DEBUG org.hibernate.hql.ast.tree.DotNode - getDataType() : name -> org.hibernate.type.StringType@3c017b73
12:16:57.392 [qtp400586934-21 - /] DEBUG o.h.hql.ast.tree.FromReferenceNode - Resolved : generatedAlias0.name -> example0_.name
12:16:57.393 [qtp400586934-21 - /] DEBUG o.h.hql.antlr.HqlSqlBaseWalker - select : finishing up [level=1, statement=select]
12:16:57.393 [qtp400586934-21 - /] DEBUG org.hibernate.hql.ast.HqlSqlWalker - processQuery() : ( SELECT ( {select clause} example0_.id ) ( FromClause{level=1} examples example0_ ) ( where ( like ( example0_.name example0_.id name ) ? ) ) )
12:16:57.393 [qtp400586934-21 - /] DEBUG o.h.hql.ast.util.JoinProcessor - Using FROM fragment [examples example0_]
12:16:57.393 [qtp400586934-21 - /] DEBUG o.h.hql.antlr.HqlSqlBaseWalker - select >> end [level=1, statement=select]
12:16:57.394 [qtp400586934-21 - /] DEBUG org.hibernate.hql.ast.AST - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (examples)
+-[SELECT_CLAUSE] SelectClause: '{select clause}'
| +-[ALIAS_REF] IdentNode: 'example0_.id as id0_' {alias=generatedAlias0, className=com.gueck.spring3.Example, tableAlias=example0_}
| \-[SQL_TOKEN] SqlFragment: 'example0_.name as name0_'
+-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[generatedAlias0], fromElementByTableAlias=[example0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}
| \-[FROM_FRAGMENT] FromElement: 'examples example0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=generatedAlias0,role=null,tableName=examples,tableAlias=example0_,origin=null,columns={,className=com.gueck.spring3.Example}}
\-[WHERE] SqlNode: 'where'
\-[LIKE] BinaryLogicOperatorNode: 'like'
+-[DOT] DotNode: 'example0_.name' {propertyName=name,dereferenceType=ALL,propertyPath=name,path=generatedAlias0.name,tableAlias=example0_,className=com.gueck.spring3.Example,classAlias=generatedAlias0}
| +-[ALIAS_REF] IdentNode: 'example0_.id' {alias=generatedAlias0, className=com.gueck.spring3.Example, tableAlias=example0_}
| \-[IDENT] IdentNode: 'name' {originalText=name}
\-[NAMED_PARAM] ParameterNode: '?' {name=param0, expectedType=org.hibernate.type.StringType@3c017b73}
12:16:57.394 [qtp400586934-21 - /] DEBUG org.hibernate.hql.ast.ErrorCounter - throwQueryException() : no errors
12:16:57.395 [qtp400586934-21 - /] DEBUG o.h.hql.ast.QueryTranslatorImpl - HQL: select generatedAlias0 from com.gueck.spring3.Example as generatedAlias0 where generatedAlias0.name like :param0
12:16:57.395 [qtp400586934-21 - /] DEBUG o.h.hql.ast.QueryTranslatorImpl - SQL: select example0_.id as id0_, example0_.name as name0_ from examples example0_ where example0_.name like ?
12:16:57.395 [qtp400586934-21 - /] DEBUG org.hibernate.hql.ast.ErrorCounter - throwQueryException() : no errors
12:16:57.400 [qtp400586934-21 - /] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
12:16:57.400 [qtp400586934-21 - /] DEBUG org.hibernate.jdbc.ConnectionManager - opening JDBC connection
12:16:57.401 [qtp400586934-21 - /] DEBUG org.hibernate.SQL - select example0_.id as id0_, example0_.name as name0_ from examples example0_ where example0_.name like ?
12:16:57.402 [qtp400586934-21 - /] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open ResultSet (open ResultSets: 0, globally: 0)
12:16:57.402 [qtp400586934-21 - /] DEBUG org.hibernate.loader.Loader - result row: EntityKey[com.gueck.spring3.Example#3]
12:16:57.402 [qtp400586934-21 - /] DEBUG org.hibernate.loader.Loader - result row: EntityKey[com.gueck.spring3.Example#6]
12:16:57.402 [qtp400586934-21 - /] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close ResultSet (open ResultSets: 1, globally: 1)
12:16:57.402 [qtp400586934-21 - /] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
12:16:57.402 [qtp400586934-21 - /] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
12:16:57.402 [qtp400586934-21 - /] DEBUG org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
12:16:57.402 [qtp400586934-21 - /] DEBUG org.hibernate.engine.TwoPhaseLoad - resolving associations for [com.gueck.spring3.Example#3]
12:16:57.403 [qtp400586934-21 - /] DEBUG org.hibernate.engine.TwoPhaseLoad - done materializing entity [com.gueck.spring3.Example#3]
12:16:57.403 [qtp400586934-21 - /] DEBUG org.hibernate.engine.TwoPhaseLoad - resolving associations for [com.gueck.spring3.Example#6]
12:16:57.403 [qtp400586934-21 - /] DEBUG org.hibernate.engine.TwoPhaseLoad - done materializing entity [com.gueck.spring3.Example#6]
12:16:57.403 [qtp400586934-21 - /] DEBUG o.h.e.StatefulPersistenceContext - initializing non-lazy collections
12:16:57.403 [qtp400586934-21 - /] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
12:16:57.403 [qtp400586934-21 - /] DEBUG o.s.o.jpa.EntityManagerFactoryUtils - Closing JPA EntityManager
package com.gueck.spring3;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
public interface ExampleDao extends CrudRepository<Example, Long> {
List<Example> findByNameLike(final String nameLike);
}
select example0_.id as id0_, example0_.name as name0_ from examples example0_ where example0_.name like ?
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="default" />
</persistence>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>2.0.5</version>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>process-sources</phase>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>1.2.0.Final</version>
<scope>compile</scope>
</dependency>
</dependencies>
</plugin>
<jpa:repositories base-package="com.gueck.spring3" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="persistenceUnitName" value="default" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<beans profile="production">
<bean id="dataSource" class="org.postgresql.ds.PGPoolingDataSource" destroy-method="close">
<property name="serverName" value="${db.serverName}"/>
<property name="databaseName" value="${db.databaseName}"/>
<property name="user" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="POSTGRESQL" />
</bean>
</beans>
<beans profile="dev">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:com/gueck/spring3/dev-schema.sql"/>
<jdbc:script location="classpath:com/gueck/spring3/dev-test-data.sql"/>
</jdbc:embedded-database>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="H2" />
</bean>
</beans>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment