While working with Hibernate I stumbled over Derby. Derby has had quite a few owners (Cloudscape, hence the original name before it was contributed to Apache, Informix and then IBM) and has finally landed on planet Apache Foundation and found its way into the incubator.
It’s not officially on the list of databases supported by Hibernate (I’m using Hibernate v3.0.x, it’s a no-go with v2.1.x), but a dialect is available anyway. So I gave it a shot and I was amazed about how easy it was to use. Just put the jar into the class-path of choice (Ant might be the best idea ;) and configure Hibernate accordingly:
hibernate.dialect=org.hibernate.dialect.DerbyDialect hibernate.connection.url=jdbc:derby:<dbname>;create=true hibernate.connection.driver_class=org.apache.derby.jdbc.EmbeddedDriver
Insert for <dbname> whatever database name suites you. It’s frickin’ easy. And it just works neatly. Derby is quite an impressive piece of Java work, albeit a little slower than “the big ones”. But its embedded-ness makes (you get a fully featured relational database engine with all features you might ever wish for just by embedding a single jar) it quite easy to use, either in or out of the container. And at least for development and testing purposes I don’t need the fastest database ever.
If you want to be more in control of where your database is put (the default is to put it in whatever your current working directory is), just add a specific path:
hibernate.connection.url=jdbc:derby:/home/derby/ponydb
or, if you’re on Windows:
hibernate.connection.url=jdbc:derby:c:/windows/ponydb
That was easy, was it? So now you can go ahead and create your schema or let Hibernate do the dirty work for you via
hibernate.hbm2ddl.auto=create-drop
When you configure Hibernate and fetch your SessionFactory, it opens a “connection” to the embedded Derby instance. With the JDBC property create set to true, Derby will just create the database for you without any hassle. Might not be the best option for a production level release, but for testing purposes this is perfect.
Using it that way makes Derby a perfect companion for easy working with your persistence layer. If you use your persistent classes in unit tests, then Derby is a nice way to do that. There might be some arguing that you shouldn’t use a real database for unit-testing purposes, but I’d be lost without tests that assure that my mapping files are correct. And Derby filled that empty spot quite nicely.
A database doesn’t go off without any restriction and so Derby has some, of course. The most remarkable one is a restriction of column names to 30 characters. That shouldn’t be a big problem, if you have a well planned schema, but sometimes this gets in the way. Another issue is the restricted length for constraints. 18 characters isn’t that much and sometimes Hibernate seems to choose longer names for constraints.
So that’s it for today. There’s really nothing more to do when using Derby with Hibernate. But stay tuned for more Derby related stuff. I’ll have some deeper look at what you can do with it. Using it inside a container and as a server database is going to be interesting.
Mathias,
What about identity generator? I can’t find: does Derby support SEQUENCE? Does Derby/Hibernate understand “native” for id, like
? If not, what do you use?
Thanks
Derby doesn\’t support sequences, but identity columns which work perfectly fine with Hibernate, but if you use \”native\” Hibernate will default to its hi-lo id generator.
If I set
,
I get a error of
”
Exception in thread “main” org.hibernate.exception.SQLGrammarException: could not get or update next value”,
How can I solve it, thank you!
The following link can help you out brother
http://db.apache.org/derby/docs/10.2/ref/rrefsqlj37836.html
I have solved the problem just changin the type. Before it was type=”int” and now I have changed to type=”long” and it worked out ! Check if you are using INT or BIGINT in your column definition.
users
cd_user
Hi,
I’m new to Derby and try do exchange Postgres with Derby.
Although using the create=true param in the jdbc url, I still get the message in the hibernate log:
although the database does not yet exist. Any idea why that is?
Secondly, I tried using an existing database and then having the jdbc connection string without the create=true; but with user=APP; and in that case I always get
Table/View ‘USERDATA’ does not exist.
although the table exists in the schema APP. Somehow it seems Hibernate is looking somewhere else for the table, not in the APP schema? Any idea?
Thanks a lot for any advice!
Hi, I have the folowing error when I try to insert a new data in my table, and these table has a id as identity. this is my log error.
Caused by: java.sql.SQLException: Se ha intentado modificar una columna de identidad ‘ID_CURSO’.
The error is showed in spanish in my log.
My cuestion is……
how I could insert a new data in a DB Derby when the ID of my table is as identity.
Sorry i don’t have write in english very well..
Thanks
sorry I’m forget say you tha i’m using hibernate…. :)
Huy, I am new to JPA development i am facing a problem when accessing my derby DB with JPA using hibernate as persistence provider. i get an exception dialect not set but when i set my dialect to
it says dialect class cannot be initialized, an inner exception states
java.lang.ClassCastException: org.hibernate.dialect.DerbyDialect cannot be cast to org.hibernate.dialect.Dialect
please reply if you have any solution for this problem. i am stuck on this point from 2 days.
Thanks a loooot buddy.. Its working fine