Friday, March 26, 2010

Download artifact from remote repository

Sometimes you may want to download an artifact from a remote repository without adding the dependency in pom.xml. You can do this with dependency:get.

mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get \
-DrepoUrl=http://download.java.net/maven/2/ \
-Dartifact=robo-guice:robo-guice:0.4-SNAPSHOT


Here is a Reference.

Friday, December 18, 2009

Where clause in Hibernate class mapping

I have a table with a column STATUS. There are two possible values: 1 and 0. 1 means active and 0 inactive. The data with inactive status are for archives only so I'd like to filter them out in the application. Of course, I can do this in the query, criteria or filter. But I wonder if there is a way to do this in mapping, once and for all. It turns out you can. You just need to specify the "where" attribute in class mapping. For example,

<class name="my.com.Report" table="IREPORT" where="STATUS=1">



This will include the active report in query. However, as always, there is a catch. This restriction does not work by default in association. According to a reference here, if the class is on the many side of one-to-many relationship, you need to add this "where" explicitly in the one-to-many mapping. But I could not figure out how to do this yet.

Tuesday, November 3, 2009

Schema Creator for HSQLDB in Unit Testing Hibernate

I am working on an application with Spring, Hibernate and MySQL database. There are several options I can unit test Hibernate DAOs. One is to use EasyMock, but it won't really test the queries. Another one is to Spring AbstractTransactionalJUnit4SpringContextTests, connecting to a test instance of MySQL. But then the tests rely on the external resource. As an alternative, I want to use a in-memory HSQLDB during unit testing to cut the dependency. The database will be created and dropped during the tests with Hibernate property "hibernate.hbm2ddl.auto=create-drop".

Sounds great. However, here is the catch. Basically, when using in-memory HSQLDB to test Hibernate with hibernate.hbm2ddl.auto=create-drop option, Hibernate does not know to create the schema BEFORE creating tables. Thus it leads to "invalid schema name" error in unit tests.

There is an ingenious solution. It created a schema creator and define it as a "depends-on" for Hibernate session factory in Spring configuration file. It thus forces the schema creation.

public final class HSQLSchemaCreator implements InitializingBean {

/**
* schema name.
*/
private String schema;

/**
* data source.
*/
private DataSource dataSource;

// setters and getters
public String getSchema() {
return schema;
}

public void setSchema(String schema) {
this.schema = schema;
}

public DataSource getDataSource() {
return dataSource;
}

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

/**
* Create schema.
* @throws Exception any exception
*/
public void afterPropertiesSet() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("CREATE SCHEMA " + schema + " AUTHORIZATION DBA");
}

}



    <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
depends-on="hsqlSchemaCreator">
...
</bean>

<bean id="hsqlSchemaCreator" class="com.guident.skills.util.HSQLSchemaCreator">
<property name="dataSource" ref="dataSource" />
<property name="schema" value="MY_SCHEMA" />
</bean>

Monday, November 2, 2009

DDL in Hibarenate Mapping File

If you set Hibernate property "hibernate.hbm2ddl.auto" to "create", Hibernate can automatically create the schema with the mapping files. Great! But what if you have things like triggers? It turns out you can add the specific DDL to element in the mapping file for this purpose.

<hibernate-mapping>
<class>
...
</class>

<database-object>
<create>
[CREATE statement]
</create>
<drop>
[DROP statement]
</drop>
<dialect-scope name="org.hibernate.dialect.HSQLDialect" />
</database-object>
</hibernate-mapping>


The elements restrict the custom CREATE or DROP statements to
a particular set of configured database dialects, which is useful if you’re deploying
on several systems and need different customizations.

Wednesday, October 28, 2009

JUnit4 public setup method in Eclipse

I converted a JUnit test case from JUnit3 to JUnit4 style. This mostly involves not extending TestCase and using annotations such as @Test and @Before etc. JUnit4 in 60 Seconds is an excellent tutorial.

I ran into a problem, however. A test method did not run after the conversion. Eclipse did not throw any error. Its JUnit View simply said "finished in 0 seconds", like it did not run it at all. Eventually I found that the setUp() method was still "protected" as in JUnit3. Everything worked after I changed it to "public".

    /**
* Setup.
*/
@Before
public void setUp() {
// ...
}



Nowhere said that the setup method must be public. It might be an Eclipse thing. Anyway, I just want to note it down here and will revisit it later.

Tuesday, October 27, 2009

Run service command on Linux

If cruisecontrol has been configured as a service on Linux, you can check status/start/stop it with the service command. For example,

service cruisecontrol status

To work withe other services, just replace "cruisecontrol" with other service names.

Checkstyle 5 and maven-checkstyle-plugin 2.3

I installed Eclipse Checkstyle plugin, which uses Checksytle 5. The latest maven-checkstyle-plugin, version 2.3, uses Checkstyle 4.4.

Everything works fine in Eclipse with Checkstyl5 5. Things got tricky when I ran "mvn checkstyle:checkstyle". If I didn't specify custom configuration, it uses the default template from Checkstyle 4.4. So the report will have a lot of false alarm because it did not recognize Java 5 checks. So I specified a custom configuration like this.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.3</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
</configuration>
</plugin>

The template, checkstyle.xml, is the default "Sun Checks" from Checkstyle 5.0. However, maven threw an error "Unable to instantiate JavadocPackageCheck". According to this post, it is because "You are trying to use a Checkstyle check that was introduced in Checkstyle 5.0. The Checkstyle Plugin currently uses Checkstyle 4.4."

It looks like we have to live with this discrepancy until maven-checkstyle-plugin 2.4, which is based on Checkstyle 5, is released.