6/6/11

Enumerated Types Java and Databases

Enumerated types is one of the best thing in Java as in any programming language in general.
The problem is that is not supported by all databases. PostgreSQL and MySQL has support Oracle and a few others don't.
This creates a problem especially for those who want to build portable systems.
(Even though many are against portability in RDBMS)

The solution is to define the type as Enumerated Type in the Java part of your system but as VARCHAR or CHARACTER VARYING etc. That is saved in db as string/text and you can enforce control through type checking in the Java side of your system. If the Java side is not the only entry point of your system, then one solution is to add CHECK CONSTRAINT on the field in question and even use exclusively Stored Procedures where you make sure no illegal values found their way into the db.

Now if you are using JPA and especially if you e.g. are using NetBeans which automatically generates classes for all tables then just replace the type String with the ENUM_TYPE you have already defined.

One example: In my schema I have a principal table with a PRINCIPAL_STATUS
the generated code for the field was
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "principal_status")
    private String principalStatus;

I changed the above code to the following

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "principal_status")
    @Enumerated(EnumType.STRING)
    private PRINCIPAL_STATUS principalStatus;

don't forget to import
import eu.minipay.entities.types.PRINCIPAL_STATUS;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;

Of course you must change accordingly the get and set methods.

6/5/11

WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context , because request parameters have already been read, or ServletRequest.getReader() has already been called

While using Glassfish v3.1 with JSF 2.0 I ended up getting this very annoying warning.
WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context /, because request parameters have already been read, or ServletRequest.getReader() has already been called


This seems to be very common with a very hard to find solution on google.
Finnally after a lot of search I found this link which had the solution.

http://mpashworth.wordpress.com/2011/06/01/glassfish-3-1-pwc4011-warning-with-jsf-applications/

I hope by placing the link here it will come up in google's answers


Place the following in the glassfish-web.xml of the web application.
<parameter-encoding default-charset="UTF-8"/>


<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
 <parameter-encoding default-charset="UTF-8"/>
...
</glassfish-web-app>