Category Archives: hibernate

getting hibernate to generate sql statements for my database (schema)

i needed to create the scripts needed to create a database schema for my persistence objects (JPA) using hibernate. there is a bunch of documentation about using specific ant tasks to do this. but as usual, beyond configuring ant with some arcane hibernate tools tasks etc. I found it easier to just do the following code

public class HibernateExporter {  public static void main(String[] args) {   
    System.out.println("<<<<BELOW IS THE SQL GENERATED FROM HIBERNATE IN ORDER TO CREATE THE DATABASE." + System.getProperty("line.separator") + " HOWEVER, REMEMBER TO CHECK THAT YOU MODIFIED THIS GENERATOR TO INCLUDE ALL THE RELEVANT CLASSES THE ARE TO BE PERSISTED.");
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(MyPersistentObject1.class);
    cfg.addAnnotatedClass(MyPersistentObject2.class);
    cfg.setProperty(Environment.USER, "root");
    cfg.setProperty(Environment.PASS, "");
    cfg.setProperty(Environment.URL, "jdbc:mysql://localhost/mfw_db");
    cfg.setProperty(Environment.DIALECT, "org.hibernate.dialect.MySQL5InnoDBDialect");
    cfg.setProperty(Environment.DRIVER, "com.mysql.jdbc.Driver");
    SchemaExport schema = new SchemaExport(cfg);    schema.setOutputFile("schema.sql");
    schema.create(true, false);  
}
}

This would, of course, require one to check that this list of classes matches the ones defined in persistence.xml, which might lead to bugs with naughty duplication and forgetting to update this one. Anyways, gets me through the day so maybe some day I will get wiser.

Generally related to this, I find it rather difficult to need to define a persistence.xml file and put it in the META-INF directory. Makes it hard to modify database parameters when you have everything packed up in a Jar file and the persistence.xml is hidden away.

 

OSGI, JPA

Trying to use JPA in OSGI always gives me the error “javax.persistence.PersistenceException: No Persistence provider for EntityManager named …”. Reason: Persistence.createEntityManagerFactory() uses the ClassLoader to load resources. It seems to be the ThreadContext ClassLoader. So in OSGI it fails due to the OSGI classloader tricks. Using the tricks given in http://lsd.luminis.nl/jpa-persistence-in-osgi-with-openjpa/ I got this to work. That is, the following..

ClassLoader oldCL = Thread.currentThread().getContextClassLoader();

Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());

emf = Persistence.createEntityManagerFactory(“my-persistence-manager”);

Thread.currentThread().setContextClassLoader(oldCL);

And no matter what you do, you get the same error. Even if it never finds the persistence.xml file, or if you typoed tha name in it, or anything else. Oh what a wonderful error message, how informative.

You might think the EclipseLink being the reference JPA implementation you might get proper OSGI consideration but no. After all, it claims to be all OSGIfied. I guess that is no more than export some packages then. And after this fix, Hibernate works for me just as well as, just need to add some exports and manage the dependencies myself. Not a big deal compared to debugging this in the first place.

hiiiibernateee

another set of points

-ran into trouble trying to get hibernate to check my 2nd entity that is referencing the first one. the mysql create broke on the wrong innodb syntax noted before. and could not just leave the second one in as i wanted to reference the first one. solution. org.hibernate.dialect.MySQL5InnoDBDialect is a correct dialect to create innodb tables correctly. note the “5” in the middle. and search the hibernate reference manual for “mysql5”. nothing found, wheehee but it works.

-how do i reference an entity from another entity and have jpa persist it? value reference bm but not the other way around. solution. name the reference in value as @ManyToOne (several values can refer to the same bm). leave out @Column since it will be named same as the @Id in bm. uu yea seems to generate a sensible table at least..

hibernate and constructors with arguments

the hibernate docs tell me to create my entity classes with a no-args constructor. so what if i add a constructor with all parameters and also set the object property tagged as the hibernate id? well, it works fine. the value i pass for the id is ignored and hibernate overwrites it with a generated one. the rest of the values end up fine in the db.

some hibernate twinkies

notes to self,

-JPA is the standard java annotation thingy for hibernate type things

-hibernate reference document lists nicely all configuration options. to get a list of relevant ones to set better to google for tutorial for hibernate and that db, then check the docs for what the options are.

-“hibernate.hbm2ddl.auto”, “create” leads to breaking of the mysql innodb syntax. at least it gives the syntax to create the table, just need to run this first and fix manually the engine part.

-annotationconfiguration class in hibernate is deprecated. just use “configuration” class in the same way. reference manual configuration section also helps in the first paragraphs.