Java – hibernate – OGM [persistenceunit: person] cannot build hibernate sessionfactory
•
Java
I received the following error
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>
Solution
The root exception explains everything:
Caused by: org.hibernate.HibernateException: Unanticipated return type [java.lang.Long] for UUID
You are using the UUID ID generator on a type that it does not support In this case, you should use string instead of long
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码