使用Hibernate OGM和MongoDb的每个类配置的表不起作用

时间:2017-01-11 21:02:31

标签: java mongodb hibernate hibernate-ogm

我正在尝试使用Hibernate OGM + MongoDb为每个类配置表。 我有一个BaseEntity如下:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class BaseEntity {
    protected UUID id;

    @Id
    // @GeneratedValue  //<--tried this only this one as well
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    public UUID getId() {
        return id;
    }

    //omitted setters and other stuffs for brevity
}

然后我有其他类继承自BaseEntity

@Entity
public class OtherEntity extends BaseEntity { 
}

当我尝试按如下方式创建EntityManagerFactory时:

entityManagerFactory = Persistence.createEntityManagerFactory("persistenceUnitName");

我收到以下错误:

javax.persistence.PersistenceException: [PersistenceUnit: persistenceUnitName] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:954)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:882)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
    at org.hibernate.ogm.jpa.HibernateOgmPersistence.createEntityManagerFactory(HibernateOgmPersistence.java:59)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at service.util.HibernateOgmUtil.<clinit>(HibernateOgmUtil.java:20)
    at service.data.repositories.GenericRepository.<init>(GenericRepository.java:31)
    at service.data.repositories.QuestionRepository.<init>(QuestionRepository.java:13)
    at Playground.main(Playground.java:31)
Caused by: org.hibernate.MappingException: No Dialect mapping for JDBC type: -2
    at org.hibernate.dialect.TypeNames.get(TypeNames.java:70)
    at org.hibernate.dialect.TypeNames.get(TypeNames.java:101)
    at org.hibernate.dialect.Dialect.getTypeName(Dialect.java:341)
    at org.hibernate.mapping.Column.getSqlType(Column.java:231)
    at org.hibernate.hql.spi.id.AbstractMultiTableBulkIdStrategyImpl.buildIdTableCreateStatement(AbstractMultiTableBulkIdStrategyImpl.java:144)
    at org.hibernate.hql.spi.id.persistent.PersistentTableBulkIdStrategy.buildIdTableInfo(PersistentTableBulkIdStrategy.java:130)
    at org.hibernate.hql.spi.id.persistent.PersistentTableBulkIdStrategy.buildIdTableInfo(PersistentTableBulkIdStrategy.java:43)
    at org.hibernate.hql.spi.id.AbstractMultiTableBulkIdStrategyImpl.prepare(AbstractMultiTableBulkIdStrategyImpl.java:84)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:453)
    at    org.hibernate.ogm.boot.impl.OgmSessionFactoryBuilderImpl.build(OgmSessionFactoryBuilderImpl.java:54)
    at org.hibernate.ogm.boot.impl.OgmSessionFactoryBuilderImpl.build(OgmSessionFactoryBuilderImpl.java:23)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)
    ... 8 more

当我调试时,似乎UUID无法转换为等效的sql类型。但是,评论@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)使得错误消失了,但是,它也没有产生我想要的东西。

此外,InheritanceType.TABLE_PER_CLASS会发出警告:

  

实体&#34; BaseEntity&#34;使用table-per-concrete-class继承,它不可移植,JPA提供程序可能不支持

如何为每个类的表有效配置Hibernate OGM + MongoDb?

1 个答案:

答案 0 :(得分:0)

如果您使用@MappedSuperClass,它将会有效。

@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class BaseEntity {

...
}

我将打开一个关于此的问题,因为我们应该支持它,或者至少抛出一个更好的例外。

或者,如果您可以更改ID的类型,并且只需要将实体与MongoDB一起使用,那么这将起作用:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class BaseEntity {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)    
   protected ObjectId id;
   ...
}