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.
Tuesday, October 27, 2009
Friday, October 16, 2009
Specify initial size for Commons DBCP BasicDatasource
It is fairly easy to setup database pooling with Spring and Commons DBCP BasicDatasource. However, the pool's default initial size is 0 if you look it up in the source code. To change that, specify "initialSize" in your configuration.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="initialSize">
<value>10</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="initialSize">
<value>10</value>
</property>
</bean>
Thursday, October 8, 2009
Get rid of "No runnable methods"
I have a couple of base tests. They are simply for setting up and extension by sub test classes, and have no test methods. Their names are the pattern of "*Tests". This works fine with maven "mvn test". But in Eclipse, when you right click "src/test/java" and "Run as > JUnit test", Eclipse will throw errors "No runnable methods". The trick is to add @Ignore on these base test classes so Eclipse won't bother again. This is of course JUnit 4 style annotation.
Thursday, September 10, 2009
Prefer getters to accessors in Hibernate domain object
I ran into something tricky today. I have a domain object.
public class User {
private String firstName;
private String lastName;
// getters and setters
// a utility method
public String getFullName() {
return this.lastName + ", " + this.firstName;
}
}
Everything looks reasonable except the full name is always "null, null" at runtime. Then I realized Hibernate has lazy loading. "this.lastName" and "this.firstName" are not populated until the getter methods are called. It worked fine after switching to getters.
public String getFullName() {
return this.getLastName() + ", " + this.getFirstName();
}
The encapsulation forces you to user getters outside User class. However, it turns out that in this case, I'd better stick to getters instead of "this." accessor anywhere even inside the User class.
public class User {
private String firstName;
private String lastName;
// getters and setters
// a utility method
public String getFullName() {
return this.lastName + ", " + this.firstName;
}
}
Everything looks reasonable except the full name is always "null, null" at runtime. Then I realized Hibernate has lazy loading. "this.lastName" and "this.firstName" are not populated until the getter methods are called. It worked fine after switching to getters.
public String getFullName() {
return this.getLastName() + ", " + this.getFirstName();
}
The encapsulation forces you to user getters outside User class. However, it turns out that in this case, I'd better stick to getters instead of "this." accessor anywhere even inside the User class.
Override equals/hashcode in Hibernate domain object
First of all, make sure that you really need to do that. According to this post,
"You only need to implement equals()/hashCode() on your persistent classes if
- it is a used as a composite primary key class
- instances of that class, loaded in different Hibernate Sessions, are in the same Set (or should be compared)"
When you do, be careful as you might run into a tricky problem. If you were me, you would use Eclipse's wizard to create equals/hashcode, which generates code including the below
if (getClass() != other.getClass()) {
return false;
}
The problem is that Hibernate domain objects are proxies, by Javassist for example. This could return false as one is the domain class and the other is the proxy class. A workaround can be found here.
Again, equals/hashcode are very important and don't take a light decision on overriding them. Use this Hibernate page as reference.
"You only need to implement equals()/hashCode() on your persistent classes if
- it is a used as a composite primary key class
- instances of that class, loaded in different Hibernate Sessions, are in the same Set (or should be compared)"
When you do, be careful as you might run into a tricky problem. If you were me, you would use Eclipse's wizard to create equals/hashcode, which generates code including the below
if (getClass() != other.getClass()) {
return false;
}
The problem is that Hibernate domain objects are proxies, by Javassist for example. This could return false as one is the domain class and the other is the proxy class. A workaround can be found here.
Again, equals/hashcode are very important and don't take a light decision on overriding them. Use this Hibernate page as reference.
Thursday, August 27, 2009
Hibernate session and Wicket filters
I am working on a web application with Spring, Hibernate and Wicket. I need to define Hibernate session filter in web.xml for "open session in view". But I kept getting Hibernate errors when the app started. The fix, as noted in "Wicket in Action", is to define Hibernate session filter ahead of Wicket filter in web.xml.
Here is a complete web.xml as example.
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>skills</display-name>
<!-- Hibernate session filter -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--
There are three means to configure Wickets configuration mode and they
are tested in the order given. 1) A system property:
-Dwicket.configuration 2) servlet specific <init-param> 3) context
specific <context-param> The value might be either "development"
(reloading when templates change) or "deployment". If no configuration
is found, "development" is the default.
-->
<filter>
<filter-name>wicket.skills</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>com.guident.skills.web.SkillsWicketApplication</param-value>
</init-param>
<init-param>
<param-name>configuration</param-name>
<param-value>development</param-value>
<!--
change "development" to "deployment" for production
<param-value>deployment</param-value>
-->
</init-param>
</filter>
<filter-mapping>
<filter-name>wicket.skills</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring context loader -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
classpath:service.xml
classpath:dao.xml
</param-value>
</context-param>
</web-app>
Here is a complete web.xml as example.
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>skills</display-name>
<!-- Hibernate session filter -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--
There are three means to configure Wickets configuration mode and they
are tested in the order given. 1) A system property:
-Dwicket.configuration 2) servlet specific <init-param> 3) context
specific <context-param> The value might be either "development"
(reloading when templates change) or "deployment". If no configuration
is found, "development" is the default.
-->
<filter>
<filter-name>wicket.skills</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>com.guident.skills.web.SkillsWicketApplication</param-value>
</init-param>
<init-param>
<param-name>configuration</param-name>
<param-value>development</param-value>
<!--
change "development" to "deployment" for production
<param-value>deployment</param-value>
-->
</init-param>
</filter>
<filter-mapping>
<filter-name>wicket.skills</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring context loader -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
classpath:service.xml
classpath:dao.xml
</param-value>
</context-param>
</web-app>
Create MySQL Triggers
I need to create a trigger in MySQL in the GUI tool Query Browser. However, I kept running into syntax errors. It turns out that I had to specify a delimiter other than ";" like the following.
Delimiter $$
CREATE TRIGGER tr_skill_path BEFORE INSERT ON skill
FOR EACH ROW
BEGIN
DECLARE parent_path CHAR;
IF NEW.parent_skill_id is not null THEN
select path into parent_path from skill where skill_id = NEW.parent_skill_id;
END IF;
IF parent_path is null THEN
SET NEW.path = CAST(NEW.skill_id AS CHAR);
ELSE
SET NEW.path = CONCAT(parent_path, '.', CAST(NEW.skill_id AS CHAR));
END IF;
END$$
Delimiter ;
This made it pass the syntax error. But another error came up:
Access denied; you need the SUPER privilege for this operation
According to MySQL Manual, "SUPER privileges are administrative and can only be granted globally". So it won't work if the grant is only for the schema. More on this later...
Delimiter $$
CREATE TRIGGER tr_skill_path BEFORE INSERT ON skill
FOR EACH ROW
BEGIN
DECLARE parent_path CHAR;
IF NEW.parent_skill_id is not null THEN
select path into parent_path from skill where skill_id = NEW.parent_skill_id;
END IF;
IF parent_path is null THEN
SET NEW.path = CAST(NEW.skill_id AS CHAR);
ELSE
SET NEW.path = CONCAT(parent_path, '.', CAST(NEW.skill_id AS CHAR));
END IF;
END$$
Delimiter ;
This made it pass the syntax error. But another error came up:
Access denied; you need the SUPER privilege for this operation
According to MySQL Manual, "SUPER privileges are administrative and can only be granted globally". So it won't work if the grant is only for the schema. More on this later...
Subscribe to:
Posts (Atom)