HibernateException:无法从类[java.lang.Boolean]期望的实例/子类中解析实体名称 [英] HibernateException: Unable to resolve entity name from Class [java.lang.Boolean] expected instance/subclass of

查看:116
本文介绍了HibernateException:无法从类[java.lang.Boolean]期望的实例/子类中解析实体名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体代表 xml 根元素,该文件是 unmarshalled xml java 使用 jaxb 。我试图使用hibernate EntityManager.persist(elementname)来保持它,但它抛出了一个 HibernateException 。异常消息只是声明它不能将 Boolean 值转换为特定的对象类型。

I have an entity that represents the root element of a large xml file that was unmarshalled from xml to java using jaxb. I am trying to persist it using hibernate EntityManager.persist(elementname), but it is throwing a HibernateException. The exception message simply states that it cannot cast a Boolean value as a specific object type.

这是代码在行抛出错误持续存在(cd)

public Long saveToDatabase(ClinicalDocument cd){
    Long id = null;
    try{
        final EntityManager saveManager = entityManagerFactory.createEntityManager();
        saveManager.getTransaction().begin();
        saveManager.persist(cd);
        saveManager.getTransaction().commit();
        saveManager.close();
        //After the object is saved, we can get the generated id:
        id = cd.getHjid();
    }catch(HibernateException sqle){sqle.printStackTrace();}
    return id;
}

我上传了一个精简的工作eclipse项目,可以让你重新创建这个问题在几分钟内。这是一个zip文件,您可以从此链接下载。要在您的机器上重新创建问题,只需:

I uploaded a stripped down working eclipse project that can enable you to recreate this problem in a couple minutes. It is a zip file that you can download from this link. To recreate the problem on your machine, just:

1.) unzip the folder  
2.) import the folder into eclipse as an existing project
3.) change `persistence.properties` to include a valid username, password, and database name on your machine  
4.) then right click on `Main.java` and choose `Run As..Java Application`.   

然后它将重新创建错误。然后,您可以查看 schema.xsd 文件中的模式以及 po.xml 文件中的精简xml。

It will then recreate the error. You can then review the schema in the schema.xsd file and the stripped down xml in the po.xml file.

您可以查看目录结构并查看 persistence.properties po的位置.xml schema.xsd ,以及 Main.java ,如下面的屏幕截图:

You can view the directory structure and see the locations of persistence.properties, po.xml, schema.xsd, and Main.java in the following screen shot below:

我还得到以下堆栈跟踪:

I also get the following stack trace:

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.HibernateException: Unable to resolve entity name from Class [java.lang.Boolean] expected instance/subclass of [org.jvnet.hyperjaxb3.ejb.tests.pocustomized.II]
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1215)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1148)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1154)
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:678)
    at productionmain.DataFunctions.saveToDatabase(DataFunctions.java:265)
    at productionmain.Main.main(Main.java:21)
Caused by: org.hibernate.HibernateException: Unable to resolve entity name from Class [java.lang.Boolean] expected instance/subclass of [org.jvnet.hyperjaxb3.ejb.tests.pocustomized.II]
    at org.hibernate.tuple.entity.PojoEntityTuplizer.determineConcreteSubclassEntityName(PojoEntityTuplizer.java:360)
    at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassEntityPersister(AbstractEntityPersister.java:3941)
    at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1494)
    at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:202)
    at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:531)
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:102)
    at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:799)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:791)
    at org.hibernate.engine.EJB3CascadingAction$1.cascade(EJB3CascadingAction.java:48)
    at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392)
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335)
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
    at org.hibernate.engine.Cascade.cascade(Cascade.java:161)
    at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:450)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:282)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
    at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:69)
    at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:179)
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135)
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
    at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:808)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:782)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:786)
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:672)
    ... 2 more


推荐答案

我相信你这里有一个奇怪的命名分歧。您有两个名为 id setId 的属性。每个属性,JAXB生成两个getter( getXXX ),如果指示isSetter来检查属性是否设置 - 对基本类型有用。这个isSetter命名为 isSetXXX

I believe you have a weird naming colission here. You have two properties named id and setId. Per property, JAXB generated two a getter (getXXX) and, if instructed "isSetter" to check if the property is set - useful for primitive types. This "isSetter" is named isSetXXX.

在这种情况下,你的isSetter为 setId id 的getter冲突。

In this case your isSetter for setId collides with the getter for id.

的issetter setId

@Transient
public boolean isSetId() {
    return (this.id!= null);
}

Getter for id

Getter for id:

 public II getSetId() {
    return setId;
}

注意 isSetId vs 。 getSetId 。这可能会让Hibernate变得疯狂,因为它认为 isSetId 是一个布尔getter,而它需要一个enity getter。

Note isSetId vs. getSetId. This probably makes Hibernate crazy because it thinks that isSetId is a boolean getter whereas it expects an enity getter.

解决方案:在绑定文件中使用 jaxb:property 重命名模型中的 setId 属性。

Solution: rename the setId property in you model using jaxb:property in your bindings file.

这篇关于HibernateException:无法从类[java.lang.Boolean]期望的实例/子类中解析实体名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆