Skip to content

Instantly share code, notes, and snippets.

@cherniag
cherniag / java.jpa.hibernate.p6spy
Last active October 17, 2020 00:37
Add p6spy to intercept database queries from Hibernate
1. Add dependency
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>2.1.3</version>
</dependency>
2. Add spy.properties to classpath with content:
# driver class name
realdriver=org.h2.Driver
@cherniag
cherniag / java.jpa.hibernate.logging
Created April 27, 2015 12:36
Hibernate log config
http://docs.jboss.org/hibernate/core/3.5/reference/en/html/session-configuration.html#configuration-logging
Category Function
org.hibernate.SQL Log all SQL DML statements as they are executed
org.hibernate.type Log all JDBC parameters
org.hibernate.tool.hbm2ddl Log all SQL DDL statements as they are executed
org.hibernate.pretty Log the state of all entities (max 20 entities) associated with the session at flush time
org.hibernate.cache Log all second-level cache activity
org.hibernate.transaction Log transaction related activity
org.hibernate.jdbc Log all JDBC resource acquisition
@cherniag
cherniag / java.jpa.hibernate.dynamic
Last active August 29, 2015 14:20
Hibernate dynamic-insert and dynamic-update
When the dynamic-insert property is set to true , Hibernate does not include null values for properties (for properties that aren’t set by the application) during an INSERT operation. With the dynamic-update property set to true, Hibernate does not include unmodified properties in the UPDATE operation. How to enable (Hibernate 3 - just add @org.hibernate.annotations.Entity to entity):
@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Table (name="CAR")
if inheritance - @org.hibernate.annotations.Entity doesn't work if only on parent entity, should be on CHILD one
http://www.mkyong.com/hibernate/hibernate-dynamic-update-attribute-example/
@cherniag
cherniag / java.jpa.entitymanager.merge
Last active August 29, 2015 14:20
Hibernate high lewel save flow
org.springframework.transaction.interceptor.TransactionAspectSupport#invokeWithinTransaction
-------------------------------
org.springframework.data.jpa.repository.support.SimpleJpaRepository#save(S)
org.hibernate.ejb.AbstractEntityManagerImpl#merge
org.hibernate.impl.SessionImpl#merge(java.lang.String, java.lang.Object)
org.hibernate.impl.SessionImpl#fireMerge(org.hibernate.event.MergeEvent)
* loop through MergeEventListener's
org.hibernate.event.def.DefaultMergeEventListener#onMerge(org.hibernate.event.MergeEvent)
org.hibernate.event.def.DefaultMergeEventListener#onMerge(org.hibernate.event.MergeEvent, java.util.Map)
* decide entity state
select * from Batch where __key__ HAS ANCESTOR KEY(Campaign, 6273189772525568)
@cherniag
cherniag / gist:02d232f46fd301da06d3
Created November 3, 2015 19:14
OPEN_SHIFT JAVA8 MAVEN COMPILER
in pom.xm: (solution)
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<verbose>true</verbose>
@cherniag
cherniag / gist:576aa8ae8eb45caa5ddd
Created November 3, 2015 19:40
web.xml mvc class config
<web-app>
<!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
@cherniag
cherniag / CustomizedArgumentParser
Last active December 1, 2024 10:04
CustomizedPredicateBuilderStrategy for RSQL JPA, processes defined custom RSQL operator (=containsPair= in this case) and transforms to JPQL predicate to query by map's key and value
import com.github.tennaito.rsql.misc.ArgumentFormatException;
import com.github.tennaito.rsql.misc.DefaultArgumentParser;
import java.sql.Timestamp;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
// default DefaultArgumentParser can't parse Timestamps :(
public class CustomizedArgumentParser extends DefaultArgumentParser {
@cherniag
cherniag / liquibase mysql add index on text field
Created February 29, 2016 16:05
liquibase mysql add index on text field
An index for a MySQL CLOB column cannot be created with Liquibase createIndex at the moment,
since MySQL requires a length limit for this index, see http://dev.mysql.com/doc/refman/5.5/en/create-index.html
As a workaround, you can use the modifySql to 'fix' the sql generated like so:
<createIndex tableName="foo" indexName="i_foo">
<column name="myClobColumn"/>
</createIndex>
<modifySql dbms="mysql">
<replace replace="myClobColumn" with="myClobColumn(80)"/>
</modifySql>