Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

Update

Well, that was quick. It looks like there is now an easy way to do this. I don’t know if this is a Spring 4 feature, but I’m using Spring 4.2.4.

You can use @PersistenceContext to inject the LocalContainerEntityManagerFactoryBean directly.

@PersistenceContext(unitName = "entityManagerFactory")
private EntityManager entityManager;

NOTE: that the unitName was specified for the PersistenceContext. In this case, it is the id of the LocalContainerEntityManagerFactoryBean.

You don’t need the SharedEntityManagerBean

==============================================================================

 

 
When working with Springframework, it is common to define an entity manager factory like the following.

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:dataSource-ref="mySqlDs" p:packagesToScan="com.javaninja.cam" p:persistenceUnitName="persistenceUnit" p:jpaVendorAdapter-ref="jpaVendorAdapter">
	<property name="jpaProperties">
		<props>
			<prop key="hibernate.show_sql">false</prop>
			<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
		</props>
	</property>
</bean>

It is common for Data Access Objects (DAO) to inject the EntityManager via

@PersistenceContext(name = "entityManager")
private protected EntityManager entityManager;

In this case, injecting that way will not work because you don’t have an EntityManager. To create an EntityManager for your factory, use the SharedEntityManagerBean to do the following

<bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
	<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

Now that you have an EntityManager, you can inject it via

@PersistenceContext(name = "entityManager")
private protected EntityManager entityManager;

The PersistenceContext annotation takes one of two properties.

  • name is used when an EntityManager is being specified
  • unitName is used when a PersistenceUnit name is being specified

January 23rd, 2016

Posted In: Java, java ninja, Javaninja, Spring

Leave a Comment

We had a need to include information about the current Git version of the files in the build for the purposes of troubleshooting the deploy process.

I stumbled upon the maven Git Commit Id plugin that provides that functionality.

Here is a list of the values that it makes available.

git.build.user.email
git.build.host
git.dirty
git.remote.origin.url
git.closest.tag.name
git.commit.id.describe-short
git.commit.user.email
git.commit.time
git.commit.message.full
git.build.version
git.commit.message.short
git.commit.id.abbrev
git.branch
git.build.user.name
git.closest.tag.commit.count
git.commit.id.describe
git.commit.id
git.tags
git.build.time
git.commit.user.name

These values are made available to replace elements in your specified directory that have the proper key and are surrounded with ${}. An example is.

<constructor-arg index="0" value="${git.commit.id.abbrev}"/>

Here are the steps to using it.

Step 1 – Set the resources directory as modifiable

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>

Step 2 – Include the Git Commit Id plugin

<plugins>
    <plugin>
        <groupId>pl.project13.maven</groupId>
        <artifactId>git-commit-id-plugin</artifactId>
        <version>2.2.0</version>
        <executions>
            <execution>
                <goals>
                    <goal>revision</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <!--
                If you'd like to tell the plugin where your .git directory is,
                use this setting, otherwise we'll perform a search trying to
                figure out the right directory. It's better to add it explicitly IMHO.
            -->
            <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
        </configuration>
    </plugin>
</plugins>

Additional configuration

To have the plugin generate a file with all of it’s values, instead of replacing values in your source, use the generateGitPropertiesFile configuration. The file will be created in whatever directory you specified in the resources element in Step 1.

<configuration>
    <!-- this is false by default, forces the plugin to generate the git.properties file -->
    <generateGitPropertiesFile>true</generateGitPropertiesFile>
</configuration>

An example of a generated git.properties is below.

#Generated by Git-Commit-Id-Plugin
#Thu Jan 14 13:32:07 EST 2016
git.build.user.email=norris.shelton@java.ninja.com
git.build.host=cdimac0001.java.ninja.com
git.dirty=true
git.remote.origin.url=https\://github.java.ninja.com/my_project.git
git.closest.tag.name=
git.commit.id.describe-short=4b3fea6-dirty
git.commit.user.email=norris.shelton@java.ninja.com
git.commit.time=14.01.2016 @ 11\:19\:21 EST
git.commit.message.full=r18.i1 - US12193 - Fixing package scan in Spring Config files
git.build.version=3.17.0
git.commit.message.short=r18.i1 - US12193 - Fixing package scan in Spring Config files
git.commit.id.abbrev=4b3fea6
git.branch=R18tc
git.build.user.name=Norris Shelton
git.closest.tag.commit.count=
git.commit.id.describe=4b3fea6-dirty
git.commit.id=4b3fea6863da76d37980527b0cc28310f88d7540
git.tags=
git.build.time=14.01.2016 @ 13\:32\:07 EST
git.commit.user.name=Norris.Shelton

By default, the plugin will search for your .git directory. The plugin documentation recommends explicitly setting it. If you are using a multi-module project, you will need to point your dotGitDirectory to a directory above what it expects.

<configuration>
    <!--
        If you'd like to tell the plugin where your .git directory is,
        use this setting, otherwise we'll perform a search trying to
        figure out the right directory. It's better to add it explicitly IMHO.
    -->
    <dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
</configuration>

To instruct the plugin to work in verbose mode, specify the following configuration.

<configuration>
    <!-- false is default here, it prints some more information during the build -->
    <verbose>true</verbose>
</configuration>

January 15th, 2016

Posted In: Git, GitHub, Java, java ninja, Javaninja, Maven

Leave a Comment

To use CXF 3.x as your restful interface, use the following dependency.

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxrs</artifactId>
    <version>${cxf.version}</version>
</dependency>

To use CXF 3.x as your rest client, use the following dependency.

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-rs-client</artifactId>
    <version>${cxf.version}</version>
</dependency>

January 13th, 2016

Posted In: CXF, Java, java ninja, Javaninja, json

Tags: , , , ,

Leave a Comment

I was migrating some old Jackson code to Jackson 2 (FasterXML). I ran into where they were telling the Jackson to ignore unknown properties via code.

ObjectMapper mapper = new ObjectMapper();
mapper.getDeserializationConfig().set(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

The new way to do this in Jackson 2 (FasterXML) is

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

January 13th, 2016

Posted In: Java, java ninja, Javaninja, json, xml

Leave a Comment

LinkedIn Auto Publish Powered By : XYZScripts.com