Im working with hibernate and mysql, and i got it working to put data in the DB via hibernate, but when i try to retrieve data it always returns null…
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
User user = (User) session.get(User.class, username);
session.getTransaction().commit();
User is a class with annotations, but the variable user will always be null, even if i put a username that i know exists in the DB. Username is the ID of the table, so it should find it
EDIT
Can it be that i use <property name="hbm2ddl.auto">create</property> in the config? What should i use so the DB create the table only if it does not exist?
the create schema option will delete all the existing data!
Check out the documentation : here
Following are the specified options:
validate: validate the schema, However no changes are made to the database.update: update the schema.create: creates the schema, destroying previous data.create-drop: drop the schema at the end of the session.Try using the
updateversion. The rest of the code snippet looks fine to me.Hope this helps