0

I'm not expert on hibernate, but I'm trying to learn it with Scala. So far I could only find hibernate documentation with Java, but no scala.

My confusion is about when I should use persistence.xml, and when an hibernate.cfg.xml.

I have this doubt because I see examples with both, and my code is sometimes complaining about not finding persistence.xml, and when it finds it then again complains about not finding hibernate properties...so I feel confuse. I feel a kind of friction between Scala/Java or different standards...

Any suggestion?

This is my main app:

package nl.busa.jpa


import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

object HibernateJpaScalaTutorial {
    var entityManagerFactory: EntityManagerFactory = Persistence.createEntityManagerFactory("nl.busa.jpa.HibernateJpaScalaTutorial")
    var entityManager: EntityManager = entityManagerFactory.createEntityManager()

def main(args: Array[String]) {

    entityManager.getTransaction().begin()
    entityManager.persist(new Buddy("Natalino", "Busa"))
    entityManager.persist(new Buddy("Angelina", "Jolie"))
    entityManager.persist(new Buddy("Kate", "Moss"))
    entityManager.getTransaction().commit()

    entityManager.getTransaction().begin();
    val allBuddies = entityManager.createQuery("From Buddy", classOf[Buddy]).getResultList
    println(allBuddies);

    entityManager.close();
}
}

This is my hibernate.cfg.xml

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
            "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory name="hibernate.cfg.xml">
    <property name = "hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name = "hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name = "hibernate.connection.url">jdbc:mysql://localhost/test</property>
    <property name = "hibernate.connection.username">root</property>
    <property name = "hibernate.connection.password"></property>
    <property name="hibernate.hbm2ddl.auto">create"</property>
</session-factory>
</hibernate-configuration>

And my error message is this:

Could not find any META-INF/persistence.xml file in the classpath

Francisco Albert
  • 1,577
  • 2
  • 17
  • 34
  • 1
    on [what is the purpose of two config files for Hibernate?](https://stackoverflow.com/a/3808406/10386912) there are some details about the purpose of persistence.xml and hibernate.cfg.xml. The EntityManager and all related classes are from JPA library hence the persistence.xml lookup –  Dec 05 '18 at 17:50

1 Answers1

3

Quite probably this question is at least a partial duplicate of

what is the purpose of two config files for Hibernate?

but the underlying question is a lot broader: in short: persistence.xml is for Hibernate in JPA mode, hibernate.cfg.xml for Hibernate's native API). At first sight your code looks like JPA, so you need persistence.xml. Your best course of action is probably to study JPA first, unless you have a good reason to want to use the proprietary Hibernate API. Plenty of good resources covering JPA on the internet, eg the official Oracle tutorial or Lars Vogel's

JPA is implemented in many ORMs (not only Hibernate but also eg Eclipselink or OpenJPA - see What is a JPA implementation? for some more info. Learning/using Hibernate is in my opinion only necessary if you are only ever going to use Hibernate as your ORM solution.

If you plan on using application servers one day, they typically come with their embedded JPA implementation and it's a whole lot easier to use and manage these in-built facilities than to provide another ORM.

fvu
  • 32,488
  • 6
  • 61
  • 79