Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove JPA #3

Closed
mvysny opened this issue Jun 7, 2017 · 26 comments
Closed

Remove JPA #3

mvysny opened this issue Jun 7, 2017 · 26 comments
Assignees

Comments

@mvysny
Copy link
Owner

mvysny commented Jun 7, 2017

This talk is hilarious: https://vimeo.com/28885655 and helped to soothe my anger a lot while fighting with Hibernate's

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.example.vok.Article.comments, could not initialize proxy - no Session
	at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:582)
	at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:201)
	at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:561)
	at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:132)
	at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:277)
	at kotlin.collections.CollectionsKt___CollectionsKt.joinTo(_Collections.kt:2013)
	at kotlin.collections.CollectionsKt___CollectionsKt.joinToString(_Collections.kt:2031)
	at kotlin.collections.CollectionsKt___CollectionsKt.joinToString$default(_Collections.kt:2030)
	at com.example.vok.ArticleView$refreshComments$1.invoke(ArticleView.kt:71)
	at com.example.vok.ArticleView$refreshComments$1.invoke(ArticleView.kt:16)
	at com.github.vok.framework.DBKt.db(DB.kt:146)

As it turns out, I had a detached article and there is no way of reattaching it back to the session! How dumb is that? https://stackoverflow.com/questions/912659/what-is-the-proper-way-to-re-attach-detached-objects-in-hibernate/4438358#4438358

So instead of article.comments.forEach {...} you need to write Article.find(article.id!!)!!.comments.forEach {...} to force Hibernate to reload the entity and make the bloody comments collection attached. Smooth 👎

So there goes Hibernate. I kinda liked Ebean until I realized it needs to do some compile-time class enhancement with Maven Tiles - 👎 , sorry, no, I'm not going to install a plugin into my IDE just to develop with Ebean. There goes Ebean.

I remember EclipseLink having troubles with sequences as primary keys generators: I believe EclipseLink started with 51 instead of 1, and then EclipseLink wondered where the hell is record number 1: http://stackoverflow.com/questions/18474046/eclipselink-and-sequence-generator-preallocation 👎

Anyway, it seems that JPA is hated by the interwebs with glaring passion: https://virgo47.wordpress.com/2014/10/09/jpa-is-it-worth-it-horror-stories-with-eclipselink-and-hibernate/
and https://www.reddit.com/r/java/comments/ln2st/jpa_the_mother_of_all_leaky_abstractions/

JPA - the mother of all leaky abstractions :-D Kinda says it all. There's no way I'm going to touch Spring Data (because it has Spring in it 👎 ), which leaves us either with (yet) another JDBC wrapper library, or https://github.com/JetBrains/Exposed . It looks quite foreign to me, but I'll evaluate.

More blogs from people pissed by Hibernate:

@mvysny
Copy link
Owner Author

mvysny commented Jun 7, 2017

Careful here: is it possible to attach JSR303 annotations to Exposed entities, and pass them around to the Vaadin Binder? Needs investigation.

@mvysny
Copy link
Owner Author

mvysny commented Jun 8, 2017

It appears that I should either adopt JPA (but switch to EclipseLink); or evaluate other options.
The alternatives:

@mvysny
Copy link
Owner Author

mvysny commented Jun 8, 2017

jOOQ is nice but it needs to generate classes for a database (or you can write THREE classes per table: a TableImpl, a Record and the pojo).

@mvysny
Copy link
Owner Author

mvysny commented Jun 8, 2017

QueryDSL can not perform entity instrumentation in runtime and requires maven plugin to do so. 👎

@mvysny
Copy link
Owner Author

mvysny commented Jun 17, 2017

MyBatis: I can't seem to find a way to just run a SELECT and map it into a POJO.

@skoude
Copy link

skoude commented Jul 6, 2017

Why not use SQL2O? I'm using it in my kotlin projects and it works great...

@mvysny
Copy link
Owner Author

mvysny commented Jul 6, 2017

@skoude that is an awesome find! Thank you, I'll evaluate.

@mvysny
Copy link
Owner Author

mvysny commented Jul 6, 2017

JDBI requires you to write DAO interfaces. Maybe this can be avoided, but it's certainly not that easy 👎
http://www.sql2o.org/ looks awesome:

  1. It can map objects to results
  2. It can do inserts and deletes without having DAO interfaces
  3. Evaluate: Can it be used with HikariCP? Edit: Yes it can, via DataSource. Awesome.

@mvysny mvysny self-assigned this Jul 6, 2017
@skoude
Copy link

skoude commented Jul 6, 2017

Yes, SQL2O can be used with HikariCP.. I have tested it, but do not use it in production yet. SQL2O is very easy to deal with kotlin, and I like it a lot, because you can just write SQL and do not have to deal with the problems that come with JPA implementations.. It's just so simple to use. If you need some examples for using the SQL2O, just say :)

@mvysny
Copy link
Owner Author

mvysny commented Jul 6, 2017

I like it a lot, because of the simplicity it offers. I quite like how easily you can map selects to objects. Could you perhaps attach a very simple CRUD examples with a simple bean? Also, is it possible to fetch, say, a pair of objects, coming from a select join?

@mvysny
Copy link
Owner Author

mvysny commented Jul 6, 2017

@skoude which version are you using, 1.6.0-RC3?

@kskyle
Copy link

kskyle commented Jul 7, 2017

@mvysny I'm currently using 1.5.4, and it's working great...

@kskyle
Copy link

kskyle commented Jul 7, 2017

Here is a few examples of select queries.. Kotlin + SQL2o select queries

@kskyle
Copy link

kskyle commented Jul 7, 2017

And here is an update / insert example: Kotlin + Sql2o update or insert

@mvysny
Copy link
Owner Author

mvysny commented Jul 7, 2017

Those examples look awesome, thank you! I quite like the simplicity. I'll look at this, and perhaps port the CRUD example app to SQL2O ;)

@mvysny
Copy link
Owner Author

mvysny commented Jul 7, 2017

Too bad that SQL2O doesn't support programmatic creation of queries (other than creating the String SQL). However, looking at the horror which is JPA Criteria API, no support for such API may actually be a blessing.

@kskyle
Copy link

kskyle commented Jul 7, 2017

Yeap, the programmatic creation of queries is the only drawback. But when not having that we have the perfect freedom to make any kind of query we like :) But still, it could be quite easy to create an interfaces for programmatic query support for sql2o with Kotlin.. Basically we would need to check what database we are using, and then make the library so that it will use correct queries for specified database..

@mvysny
Copy link
Owner Author

mvysny commented Jul 7, 2017

True. But perhaps we can only use really simple SQL92 which should be supported by all databases. SQL92 should be more than enough for simple Grid filters.

@kskyle
Copy link

kskyle commented Jul 7, 2017

Yeap, that's correct. I was just thinking that it would be great to support for example postgresql json features, but that's completely other story. SQL92 dialect support would be just great.

@mvysny
Copy link
Owner Author

mvysny commented Jul 7, 2017

Javadoc is missing from nearly all Query methods; it's quite important to have javadoc there, since I have no idea how, say, Query.keys work. Other than that, it seems to work just perfectly 👍

@mvysny
Copy link
Owner Author

mvysny commented Jul 8, 2017

Initial support for sql2o pushed into VoK. It's totally untested and rudimentary, please see MappingTest.kt for more details ;)

@mvysny
Copy link
Owner Author

mvysny commented Jul 9, 2017

Just ported the example project to sql2o. It starts way faster than with JPA/Hibernate; love it. Need to update docu.

@skoude
Copy link

skoude commented Jul 10, 2017

It looks great now 👍 Will try to test it and take a deep dive into code some point at this week.

@mvysny
Copy link
Owner Author

mvysny commented Jul 10, 2017

Thanks! Done, the documentation is updated and the Getting Started guide now uses Sql2o instead of JPA: http://www.vaadinonkotlin.eu/gettingstarted.html

@mvysny mvysny closed this as completed Jul 10, 2017
@mvysny
Copy link
Owner Author

mvysny commented Feb 25, 2018

I have extracted the library into an external project: https://github.com/mvysny/vok-orm
This way I believe the library forms its own island which can be studied apart from VoK, and thus it can be understood much easier. Also the library can now be used even outside of VoK apps. In additional to Sql2o, the library supports:

  • A simple programmatic way of creating WHERE clauses;
  • A simple support for storing the data back into the database;
  • A simple way to create finders

Please try it out and let me know :)

@mvysny
Copy link
Owner Author

mvysny commented Sep 11, 2019

JDBI with API 3.0 doesn't require you to create the DAO interfaces anymore. It has a couple of dependencies totaling 1,9MB, but that's okay. It's more actively maintained than SQL2O, so maybe we should switch to that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants