seinecle seinecle - 5 months ago 6x
SQL Question

Java lib to work on mySQL with object oriented syntax?

Beginner in SQL, coming from MongoDB.

I see libs that write SQL queries with Strings like:

String sql =
"SELECT id, category, duedate " +
"FROM tasks " +
"WHERE category = :category";

then one must take this
sql String
and write something like:
List<Task> tasks = con.createQuery(sql);

My question:
Imagine a db made of tables where each line in the table is considered an object.
Is there a library for mySQL where the syntax would be more like:

List<Object> results = db.query().select(Object.oneField).where(Object.oneField.equals("myString").from(db.MyTable);

Note: the mySQL db is already created, I am not the owner.


I think Hibernate is what you are looking for.

At a very beginners level its like..

//create a user(Entity) object and set its properties User user = new User(); user.setName("xyz"); user.setCity("New Delhi");

and then you go on persisting(saving) the object to the database which will allow you to save the user properties in the database.

//get hibernate specific SessionFactory object session.persist(user); //this will make your object's properties persist in the database

Take a look at Hibernate docs for detailed info.