Posts

Showing posts with the label Hibernate

Java DB queries - All of the Options (2014)

When you want to query a DB in java, you have two main options to go: 1. Straight forward queries to the DB This is the most intuitive (imho), just write down the query you want and get the results from the DB. The most notable downside to this method is that you pollute your code with sql queries which can be all over the code, so there is no "one point of change" to the queries, which makes it very hard to change the DB schema for example. 2. ORM style queries This way requires more work to begin qurying the DB but then you use java objects instead of queries so you keep your code clean. You also are able to separate your model from your controller thus change your DB schema with much less pain. Which ORMs exist? Many, while the most notable ones are hibernate (which was the first afaik) & eclipse/total link. But most ORM are not only a third party library (like hibernate when it began) but are now part of JEE implementation of the ...

Iterate over your whole DB using hibernate

In this post I'll assume you already have your hibernate object corresponding to your DB schema. What will we try to achieve here? I will iterate all of the DB using hibernate objects, and here is a little java program which will attempt to achieve just that: import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.Session; import org.hibernate.Query; import org.hibernate.metadata.ClassMetadata; import java.util.Map; /** * Created by IntelliJ IDEA. * User: avih * Date: May 11, 2010 * Time: 7:43:37 AM */ public class Main { private static final SessionFactory ourSessionFactory; static { try { ourSessionFactory = new AnnotationConfiguration(). configure("hibernate.cfg.xml"). buildSessionFactory(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } ...