This is my first time using hibernate, I have gone through a tutorial or two and I have quickly mocked this class up to see if I can get something from my database:
import java.util.Properties;
import org.eclipse.emf.teneo.PersistenceOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
public class Connector {
public static void doStuff() {
Properties props = new Properties();
props.setProperty(Environment.DRIVER, "com.postgres.Driver");
props.setProperty(Environment.USER, "postgres");
props.setProperty(Environment.URL, "jdbc:postgres://127.0.0.1:5432/epic");
props.setProperty(Environment.PASS, "postgres");
props.setProperty(Environment.DIALECT, org.hibernate.dialect.ProgressDialect.class.getName());
props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_CONTAINMENT,
"REFRESH,PERSIST,MERGE");
props.setProperty(PersistenceOptions.INHERITANCE_MAPPING, "JOINED");
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");// populates the data of the
// configuration file
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction t = session.beginTransaction();
Query query = session.createQuery("select * from otar");
java.util.List list = query.list();
System.out.println(list);
t.commit();
session.close();
}
public static void main(String[] args) {
doStuff();
}
}
the compiler says that I am missing the hibernate.cfg.xml
file, I have read that it needs to be in my src
directory of my project, I have also read that it doesn't get generated for me, but I have also read the opposite that it does get generated for me ... my understanding of this file is that it is needed for table column mapping...it probably has a much more complex and useful use but I have only just started this so that is as much as I know, here are my questions:
- Does it get generated for me or do I need to create it?
- Is it ok to create? like is it good practice?
- what are the benefits of me making it by hand.
- Are there any instructions on how to go about creating one?
EDIT I forgot to mention that this is a plug-in project that I am using.
There's a difference between Hibernate configuration file (hibernate.cfg.xml) and Hibernate mapping files (*.hbm). If you have heard about generated code it concerns last ones, but first one should have included those mapping files. There's two or three different approaches to development. First to create Java classes with mappings and generate database schema, Second create database schema and generate Java classes (reverse engineering), Third is create classes and mapping for existed schema. Whichever approach you use is up to you, but
hibernate.cfg.xml
you should create manually if it's not created by IDE. Some frameworks like Spring can provide it's own configuration for Hibernate, thus completely ignoringhibernate.cfg.xml
. There's nothing magical with creatinghibernate.cfg.xml
file. You can start withAnd you are right, it should be in the
src
folder of your project. At runtime it should be where the.class
files generated, i.e. on classpath.