java - How to switch from Hibernate to OpenJpa -


i've hibernate project developed spring mvc 3 , built maven 3. manage persistence jpa in particular hibernate framework; guess isn't difficult configure existing source openjpa, i'm new , i'd know steps configure project switch in consistent way hibernate openjpa.

greetings

follow below steps:

to switch annotations, you’ll need to:

add hibernate-annotations , jpa jars annotate classes modify hibernate.cfg.xml use classes instead of mapping files modify hibernateutil class use different configuration (this step tripped me up) 

to add right jars, i’m using maven , added following additional dependencies:

<dependency>    <groupid>javax.persistence</groupid>    <artifactid>persistence-api</artifactid>    <version>1.0</version>  </dependency>  <dependency>   <groupid>org.hibernate</groupid>    <artifactid>hibernate-annotations</artifactid>    <version>3.3.1.ga</version>  </dependency> 

there many fine resources available on jpa annotations i’m not going discuss in detail. you’ll have looks this:

@entity  @table(name = “users”)  public class user {  @id @generatedvalue  @column(name = “user_id”)  private long id;  @column(name = “first_name”)  private string firstname;  @column(name = “last_name”)  private string lastname;  @column(name = “email”)  private string email;  // etc  } 

to update hibernate.cfg.xml file, you’ll want change lines this:

<mapping resource="org/terracotta/reference/exam/domain/user.hbm.xml"/> 

to this:

<mapping class="org.terracotta.reference.exam.domain.user"> 

if did , stopped there, might see exception this:

exception in thread "main" java.lang.exceptionininitializererror     @ org.terracotta.reference.exam.domain.hibernateutil.<clinit>(hibernateutil.java:15)     @ org.terracotta.reference.exam.domain.main.main(main.java:12) caused by: org.hibernate.mappingexception: annotationconfiguration instance required use <mapping class="org.terracotta.reference.exam.domain.user"/>     @ org.hibernate.cfg.configuration.parsemappingelement(configuration.java:1600)     @ org.hibernate.cfg.configuration.parsesessionfactory(configuration.java:1555)     @ org.hibernate.cfg.configuration.doconfigure(configuration.java:1534)     @ org.hibernate.cfg.configuration.doconfigure(configuration.java:1508)     @ org.hibernate.cfg.configuration.configure(configuration.java:1428)     @ org.hibernate.cfg.configuration.configure(configuration.java:1414)     @ org.terracotta.reference.exam.domain.hibernateutil.<clinit>(hibernateutil.java:13) 

and, me, puzzled. turns out when using annotations need use annotationconfiguration, subclass of configuration, specific annotations. so, hibernateutil might instead:

package org.terracotta.reference.exam.domain;  import org.hibernate.sessionfactory; import org.hibernate.cfg.annotationconfiguration;  public class hibernateutil {  private static sessionfactory sessionfactory;  static {  try {  sessionfactory = new annotationconfiguration().configure().buildsessionfactory();  } catch(throwable t) {  throw new exceptionininitializererror(t);  }  }  public static sessionfactory getsessionfactory() {  return sessionfactory;  }  public static void shutdown()  {  getsessionfactory().close();  }  } 

hope helps!!


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -