Chariot Training Classes

Training Courses

I run Chariot's training and mentoring services. We provide training in AngularJS, HTML5, Spring, Hibernate, Maven, Scala, and more.

Chariot Education Services

Technology

Chariot Emerging Tech

Learn about upcoming technologies and trends from my colleagues at Chariot Solutions.

Resources

Chariot Conferences

Podcasts

Entries in chariot-news (45)

Tuesday
Jun022009

Gettin' all Testy - The Spring TestContext, Hibernate, and you...

I'm getting some materials ready around a project using the Hibernate Annotations API in Spring 2.5.6.  The client is using Hibernate rather than straight JPA, likely due to wanting to get features such as Filters, Criteria, etc...

I've been used to JUnit 3.x for a while, but as I'm testing my Hibernate knowledge, I came across the newer Spring Test Context framework.  If you install JUnit 4.4 (I'm using Maven, so I just added it as a test-time dependency) and also the spring test-context library, you'll have the ability to define tests that ramp up a Spring Context using the @ContextConfiguration annotation and coupled with extending a specific Spring abstract class, AbstractJUnit4SpringContextTests.

This is similar to the Junit 3.8 context configuration, but uses JUnit 4 semantics.  Also you can use TestNG, just use a different abstract class.  Here is a fragment:

@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class HibernateModelTest 
   extends AbstractJUnit4SpringContextTests {
...
}

 

Ok, that's cool.  You get an applicationContext member in the test class too. So, what if you wanted to interact with the Hibernate API directly?  I've mounted my Hibernate configuration using the Spring Hibernate AnnotationSessionFactoryBean, scanning for entities in the org.rimple.example.domain package as in this example:

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.
AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="org.rimple.example.domain"/>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.HSQLDialect
        hibernate.hbm2ddl.auto=create
        hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider
        hibernate.show_sql=true
        hibernate.current_session_context_class=thread
        hibernate.connection.pool_size=15
      </value>
    </property>
</bean>
       

Now I can just create a @Before and @After method to setup and tear down the session context.  I can get fancy and begin a transaction and end it, just like the transactional tests provided for Repository tests, but then I can open and close and interact with the session myself.

    private Session session;
    @Before
    public void setupSession() {
        SessionFactory sessionFactory = (SessionFactory) applicationContext.
getBean("mySessionFactory");
        session = sessionFactory.openSession();
        session.getTransaction().begin();
    }

    @After
    public void tearDownSession() {
        session.getTransaction().rollback();
        session.close();
    }

Now, all I have to do is just interact with Hibernate directly. Given this Entity:


@Entity
@Table(name="employees")
public class Employee {

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @Column(nullable=false, length=30)
    private String firstName;

    @Column(nullable=false, length=30)
    private String lastName;

    @Column(nullable=false)
    private Boolean active;

    @Column(nullable=false)
    @Temporal(TemporalType.DATE)
    private Date hireDate;

    @Column(nullable=true)
    private String comments;
    ...
}

We can use this test:

    @Test
    public void createEmployee() {
        Employee e = new Employee();
        e.setActive(true);
        e.setFirstName("Joe");
        e.setLastName("Smith");
        e.setComments("A new employee");
        e.setHireDate(new Date());

        session.save(e);
        assertNotNull(e);
    }

QED!  I am partial to the JPA Entity Manager and putting Hibernate behind a facade, but this works if your team decides they are using the Hibernate annotation and API instead.

Friday
Apr172009

Google App Engine - Now with Java "Freshness"

In the past two weeks, Google has announced support for Java and Groovy on the Google App Engine.  Various frameworks are supported, and the team at SpringSource is hard at work with a git repository fork of Grails for AppEngine.  

What does that mean for us Java-heads?

 

  • You will now be able to deploy a Java-based application to Google App Engine for free up to a certain number of page views per month (rumored to be somewhere near 5 million views per month)
  • You can use some major Java frameworks, including Spring, some of Spring MVC (apparently they are sorting through the spring: tag lib), etc...
  • You will have to code differently (just as the Python developers using the original App Engine did), because you're running on a non-relational data store (BigTable).  However, you can use JDO and most of the features of JPA.

 

This is an interesting announcement.  For quick applications (especially Grails-based) that you want to get out there for immediate feedback, you now have a place to drop them, free.  Of course, coding for App Engine puts some pretty hard limitations on your architecture; you'd have to figure out whether or not you'd want to do something like that and whether it fits your ultimate goals.  But for UI prototyping, quick burst apps like Facebook surveys, etc., it's an interesting alternative.

Even better, the Groovy team released Groovy 1.6.1, which is compatible with GAE.  There are sample applications using Groovy Ant builder running around.  Check here for a great article by Guillaume LaForge. Also see this, and this.  A good message board exists on the Google Groups page.

 

Tuesday
Mar172009

Interesting Plugin for Grails - javascript validation

Anyone who has programmed in Grails realizes the power of the domain-centric design pattern, including validation rules embedded as constraints in the Domain class.

Someone has written a plugin that dynamically generates Apache Commons Validator code on the fly.  It's called the javascript validator plugin and it is enabled in three steps:

Install the plugin:

grails install-plugin javascript-validator

Add to a given form with:

<jv:generateValidation domain="Employee" form="employeeForm">

Set up the form to include validation

<form id="employeeForm" onSubmit="return validateForm(this);" ... >

I'm going to give this a shot. Interesting how simple it could be to add client-side validation without re-architecting your whole code base. This is why Grails rocks!

Tuesday
Feb032009

Fix for "mac internal compiler error 139" - disable Instant Hijack! 

For the love of Pete, this took me forever to find. I have a 64-bit Mac Book Pro, and at some point I found that I could no longer use IntelliJ Idea, as it always returned the following, horrid and cryptic error message:

"Compiler internal error. Process terminated with exit code 139."

How rude! Even after re-installing my OS X from scratch and re-adding my applications, the error came back. That was because I installed Rogue Amoeba's Instant HiJack tool for my podcasting. For some reason, it messes with the compiler and forces it to an incompatible version.

The fix is located on the JetBrains site as http://www.jetbrains.net/devnet/docs/DOC-1114. You have to unload a service named hermesctl (which I assume disables instant hijack, another problem I'll have to work out).

sudo /usr/local/hermes/bin/hermesctl unload

So much for letting developers be podcasters!

Hope this helps someone else who has been frustrated at this problem.

Ken

Saturday
Jan312009

Great Flex Spring/BlazeDS Samples by Christophe Coenrets

Every once in a while you run across something amazing. Christophe Coenrets has put together some Spring and BlazeDS (that's the Adobe Open-Source remoting technology) integration samples, based on the Spring/BlazeDS API.

It comes as projects + a running Tomcat instance, complete with data. If you are at all interested in using the Flex RIA with your Spring Applications, this is one of two projects to pay close attention to. The other is the Grails framework, where you should be able to wire this API to Flex (the current Flex plugin was a proof-of-concept, but I think work is continuing in this area).

While not perfect, Flex is a great client-side tool.  I do remember many years spent at the dawn of client-server with a tool called PowerBuilder in days where we didn't care about super-complicated frameworks.  It just worked.  Spending some time with Flex makes me yearn for the days where the Client and the Server were different technologies that both did what they did well.  Maybe we're at the beginning of a new movement away from ugly browser coding into smart, three-tiered applications.  Well, a guy has to dream, eh? 

Happy research!  Great job, Christophe!  Want to be a guest on the Chariot Tech Cast? 

Here is the link:

New Spring/BlazeDS Integration Test Drive : Christophe Coenraets
Technorati Tags: , ,

Page 1 ... 5 6 7 8 9