Hibernate - OGM [PersistenceUnit: person] Unable to create Hibernate SessionFactory

I am getting below error

Exception on thread "main" javax.persistence.PersistenceException: [PersistenceUnit: person] Failed to create Hibernate SessionFactory at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException (EntityManpaagerFactoryBuilderImpl.java) on. boot.internal.EntityManagerFactoryBuilderImpl.access $ 600 (EntityManagerFactoryBuilderImpl.java:120) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl $ 4.performityBuilderManagerFactoryBuilderImpl $ 4.performa (EntityManager) .perform (EntityManagerFactoryBuilderImpl.java:850) at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl (ClassLoaderServiceImpl.java:425) at org.hibernate.jpa.boot.internal.imm.build (EntityManagerFactoryBuilderImpl.java:849) at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory (HibernatePersistenceProvider.java:75) at org.hibernate.ogm.jpa.HibernatePersistencePersistence.create.jpa.HibernateOgmPersistence.creation .createEntityManagerFactory (Persistence.java:55) at javax.persistence.Persistence.createEntityManagerFactory (Persistence.java:39) at com.ihappyk.utility.Utility.setUpEntityManagerFactory (Utility.java:11) at com.Pershappyk.work.orker main (PersonWorker.java:14) Caused by: org.hibernate.MappingException: Failed to instantiate generator id [object_name = com.ihappyk.model.Person] at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator (DefaultIdentifierFactoryGenerator java:123) at org.hibernate.mapping.SimpleValue.createIdentifierGenerator (SimpleValue.java:225) at org.hibernate.internal.SessionFactoryImpl. (SessionFactoryImpl.java:323) at org.hibernate.cfg.Configuration.buildSessionFactory (Configuration.java:1859) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl $ 4.perform (EntityManagerFactoryBuilderImpl) ... 9 Thrown: org.hibernate.HibernateException: Unexpected return type [java.lang.Long] to convert UUID to org.hibernate.id.UUIDGenerator.configure (UUIDGenerator.java:111) at org.hibernate.id.factory.internal .DefaultIdentifierGeneratorFactory.createIdentifierGenerator (DefaultIdentifierGeneratorFactory.java:117) ... 13 morehibernate.internal.SessionFactoryImpl. (SessionFactoryImpl.java:323) at org.hibernate.cfg.Configuration.buildSessionFactory (Configuration.java:1859) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl $ 4.perform (EntityManagerFactoryBuilderImpl) ... 9 Thrown by: org.hibernate.HibernateException: Unexpected return type [java.lang.Long] to convert UUID to org.hibernate.id.UUIDGenerator.configure (UUIDGenerator.java:111) at org.hibernate.id.factory.internal .DefaultIdentifierGeneratorFactory.createIdentifierGenerator (DefaultIdentifierGeneratorFactory.java:117) ... 13 morehibernate.internal.SessionFactoryImpl. (SessionFactoryImpl.java:323) at org.hibernate.cfg.Configuration.buildSessionFactory (Configuration.java:1859) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl $ 4.perform (EntityManagerFactoryBuilderImpl) ... 9 Thrown by: org.hibernate.HibernateException: Unexpected return type [java.lang.Long] to convert UUID to org.hibernate.id.UUIDGenerator.configure (UUIDGenerator.java:111) at org.hibernate.id.factory.internal .DefaultIdentifierGeneratorFactory.createIdentifierGenerator (DefaultIdentifierGeneratorFactory.java:117) ... 13 moreEntityManagerFactoryBuilderImpl $ 4.perform (EntityManagerFactoryBuilderImpl.java:857) ... 9 more Called by: org.hibernate.HibernateException: unexpected return type [java.lang.Long] to convert UUID to org.hibernate.id.UIDGenerator (UUID.UIDGenerator .java: 111) at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator (DefaultIdentifierGeneratorFactory.java:117) ... 13 moreEntityManagerFactoryBuilderImpl $ 4.perform (EntityManagerFactoryBuilderImpl.java:857) ... 9 more Called by: org.hibernate.HibernateException: unexpected return type [java.lang.Long] to convert UUID to org.hibernate.id.UIDGenerator (UUID.UIDGenerator .java: 111) at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator (DefaultIdentifierGeneratorFactory.java:117) ... 13 morecreateIdentifierGenerator (DefaultIdentifierGeneratorFactory.java:117) ... 13 morecreateIdentifierGenerator (DefaultIdentifierGeneratorFactory.java:117) ... 13 more

Utilty.java

public class Utility {
    private static EntityManagerFactory entityManagerFactory;

    //@BeforeClass
    public static EntityManagerFactory setUpEntityManagerFactory() {
        entityManagerFactory = Persistence.createEntityManagerFactory( "person" );
        return entityManagerFactory;
    }

    //@AfterClass
    public static void closeEntityManagerFactory() {
        entityManagerFactory.close();
    }
}

      

Persistent class

@Entity
public class Person {

        @Id
        @GeneratedValue(generator = "uuid")
        @GenericGenerator(name = "uuid", strategy = "uuid2")
        private long id;

        private String firstName;
        private String lastName;

        public long getId() {
            return id;
        }
        public void setId(long id) {
            this.id = id;
        }
        public String getFirstName() {
            return firstName;
        }
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
        public String getLastName() {
            return lastName;
        }
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public Person(){

        }

        public Person(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }    
}

      

main class

public class PersonWorker {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        EntityManagerFactory emf = Utility.setUpEntityManagerFactory();

        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();

            // create a Person
        Person bob = new Person( "Bob", "McRobb" );

        em.persist( bob );
        em.getTransaction().commit();
        em.close();
        emf.close();

    }

}

      

persistance.xml

<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

    <persistence-unit name="person" transaction-type="JTA">
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
       <class>com.ihappyk.model.Person</class>
        <properties>
            <property name="hibernate.ogm.datastore.provider" value="mongodb" />
            <property name="hibernate.ogm.datastore.database" value="hibernateOGM" />
            <property name="hibernate.ogm.datastore.host" value="127.0.0.1" />
            <property name="hibernate.ogm.datastore.port" value="27017" />
            <property name="hibernate.ogm.datastore.provider" value="org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider"/>

            <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform"/>
        </properties>
    </persistence-unit>
</persistence>

      

+3


source to share


4 answers


The root exception specifies everything:

Caused by: org.hibernate.HibernateException: Unanticipated return type [java.lang.Long] for UUID

      



You are using a UUID generator for a type that it does not support. You should use String

instead Long

in this case.

+6


source


  • If you are using this deprecated org.hibernate.ejb.HibernatePersistence install the new provider as shown below.

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

  1. if your value

<property name="hibernate.hbm2ddl.auto" value="update" />

install "create-drop"



<property name="hibernate.hbm2ddl.auto" value="create-drop" />

  1. XML path: resources / META -INF / persistence.xml
  2. For mysql

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

        <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
            <properties>
                  <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
                  <property name="hibernate.hbm2ddl.auto" value="create-drop" />
                  <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
                  <property name="hibernate.show_sql" value="true"/>
                  <property name="hibernate.connection.username" value="yourname"/>
                  <property name="hibernate.connection.password" value="yourpassword"/>
                  <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/yourschema"/>
                  <property name="hibernate.max_fetch_depth" value="3"/>
            </properties>
        </persistence-unit>

    </persistence>

      

+1


source


First, you should share your full stack about exception and persistence.xml content. Then in my opinion, based on what I see, the problem might be that you did not specify the persistence provider in your persistence.xml. In fact, if you are working in a non container managed environment, you need to declare a persistence provider.

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
         version="2.0">
    <persistence-unit name="myunit" >
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    //Others properties
</persistence-unit>

      

0


source


I don't know if there is a problem, but I thought you should put the persistence.xml file in the src / main / META-INF folder. I see you put it in src / META-INF. Maybe this is the problem? and of course the objects you want to link also belong in the src / main folder :)

-1


source







All Articles