Friday, 15 August 2014

Java Enum values() method

I remember there being a values() method that provides all possible values of an Enumerated Type in Java. I checked the javadoc1 and couldn't find it...

But it exists.

Which is weird.

Luckily, someone else had the same problem and asked it in [2].

I was intriged so I looked up the Language Spec3. Yes, the method is available in each and every Enum class, but it is 'especially' inserted by the compiler and not inherited from the superclass4.

Luckily in the javadoc1, values() and valueOf(String) methods are both mentioned in the description of the valueOf(Class<T> , String) method (nowadays).

References

[1] Javadoc - Enum
http://docs.oracle.com/javase/8/docs/api/java/lang/Enum.html
[2] CodeRanch - Values method documented Enum
http://www.coderanch.com/t/559674/java/java/values-method-documented-Enum
[3] Oracle - The Java Language Specification (Java 8)
http://docs.oracle.com/javase/specs/jls/se8/jls8.pdf
[4] Oracle Java Tutorials - Enum Types
http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

Friday, 8 August 2014

Mysql JDBC Issue

Hammered myself into a wall here. Turns out when I create my Java Entities from a Database Table (using NetBeans Wizards), I get the following:

@Entity
@Table(name = "mm_log", catalog = "mmud", schema = "")
@NamedQueries(
{
    @NamedQuery(name = "Log.findAll", query = "SELECT l FROM Log l"),
    @NamedQuery(name = "Log.findByName", query = "SELECT l FROM Log l WHERE l.name = :name"),
    @NamedQuery(name = "Log.findByMessage", query = "SELECT l FROM Log l WHERE l.message = :message")
})
public class Log implements Serializable

Notice the catalog and the schema in the annotation for Table?

Now, it turns out1 that "schema" is unsupported in MySQL, and "catalog", apparently, is just a fancy way in MySQL of saying "database".

So, if I create a brand new database (call it "newmmud"), create a nice JDBC Connection Pool to it in my Glassfish server, yet this blasted Entity (Log, in the example) will still refer to the database "mmud"!

That's what I call confusing!

Here's the message, for posterity's sake:
“You're correct. For legacy reasons (including compatibility with ODBC, and SQL Server), JDBC's concept of "catalog" maps to MySQL's concept of "databases".

Starting in MySQL-4.1, "schema" is an alias for "database", but it doesn't act like a SQL-standard schema, so we don't support the concept in the JDBC driver.”
- Mark Matthews

References

[1] Forum - Re: catalog versus schema
http://forums.mysql.com/read.php?39,137564,137629#msg-137629