Saturday, October 09, 2010

Moving from MySQL to Derby

Developing locally i wanted to move my Database from MySQL to Derby on my laptop. Since i use JPA Entities i thought it's an easy task just clicking some menus.

Unfortunately triing to create the Tables from Entities were not as easy as i hoped.

Constraints 'SQL101008073950601' and 'SQL101008073950600' have the same set of columns, which is not allowed.

This was the first Problem i had to fix. Since google (or any other search engine) is the developers best friend i found this bug information:
https://issues.apache.org/jira/browse/DERBY-789?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel

This Link is also very informative:
https://forum.hibernate.org/viewtopic.php?f=1&t=998193

In the end it was this little Attribute in Column (unique = true)
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "hi_id", unique = true, nullable = false)
derby column, or type mapping 'VARCHAR(65535)' is not valid
I found this link:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-1501
@Column(name = "hi_text", length = 65535)
I had to cut the length to 32000.

Tuesday, August 17, 2010

Hamcrest 1.1 Security Error on Eclipse Gallileo With JMock

Finally i started working on unit tests and already got a gotcha on eclipse galileo with jMock 2.5.1.
Galileo Ships with jUnit 4 but unfortunately without org.hamcrest.TypeSafeMatcher which is needed by jMock.

Adding jMock through Maven still leaves the Problem with Classpath and who comes first.

I tried the following links with no luck:
http://www.southsearepublic.org
http://cedar715.wordpress.com/2009/10/20/86/

Then i removed jUnit Libraries in Project Properties under Java Build Path => Libraries and added jUnit through Maven Dependencies.

Works like a charm now.

Tuesday, July 27, 2010

jPitfalls - Enums in Hibernate

In Hibernate you have two ways defining the association between database fields and Java properties. You can use xml definitions or Annotations.

I for example started with xml and later changed everything to Annotations. Either way, i used jBoss Hibernate Tools in Eclipse for generating the Java Class Files and xml Configuration.

Fortunately both Database and Java have Enums. Unfortunately the Hibernate Tool does not. (don't get me started on Hibernate at this point) So while you may think "Hey, i used Enums in my Database Design because i am a genius" and "That's great, now i can use them in Java" you can't. (Who is the genius).

Using Annotations it's easier. Creating Enums, adding them into the Hibernate Class Files and voila, you have Enums. (But don't forget to maintain them = meaning if you change the Enum in Java or the Database you have to change the other Part two by hand).

Using xml is a complete different Story. You have to Define the Type and adjust it by Hand. There is even a GenericEnumUserType explained in the jBoss Community.

In the end i still think that it should also be possible to create Enums automatically and some day it might, but in the meantime stay sharp and check you Enums.

Linktipps:
http://community.jboss.org/wiki/Java5EnumUserType
http://weblog.dangertree.net/2007/09/23/mapping-java-5-enums-with-hibernate/