我有这个问题:
Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: fb-persistence] Unable to configure EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:378)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
at it.synclab.fb.test.Test.main(Test.java:13)
Caused by: java.lang.ClassCastException: org.hibernate.mapping.UnionSubclass cannot be cast to org.hibernate.mapping.RootClass
at org.hibernate.cfg.annotations.PropertyBinder.bind(PropertyBinder.java:209)
at org.hibernate.cfg.annotations.PropertyBinder.makePropertyValueAndBind(PropertyBinder.java:200)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2061)
at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:796)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:707)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:4008)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3962)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1371)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1348)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1522)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1100)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:282)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:366)
... 4 more
谁能帮我解决?我已经使用JPA配置了Hibernate,文件persistence.xml是:
<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="fb-persistence" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>it.synclab.fb.jpa.Plugin</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.username" value="***"/>
<property name="hibernate.connection.password" value="***"/>
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:RAFFAELE"/>
</properties>
</persistence-unit>
</persistence>
和我的类映射是:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity @Table(name="PLUGIN")
public class Plugin {
private int id;
private String nome;
private String descrizione;
public Plugin(){}
@Id
@Column(name="ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="NOME")
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@Column(name="DESCRIZIONE")
public String getDescrizione() {
return descrizione;
}
public void setDescrizione(String descrizione) {
this.descrizione = descrizione;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Plugin other = (Plugin) obj;
if (id != other.id)
return false;
return true;
}
}
有什么问题?帮我?我忘了,我已经使用maven2配置了dependdenci,文件pom.xml是:
...
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
...
由于
答案 0 :(得分:0)
我敢打赌你的类路径上有多个hibernate jar,这就是为什么它会抛出一个类转换异常。
另一个看起来错误(但可能是正确的)是您正在使用JTA事务。如果您不知道JTA的含义,请将persistance.xml上的事务类型更改为transaction-type="RESOURCE_LOCAL"
答案 1 :(得分:0)
我曾经在拥有彼此继承的Entity bean时遇到此错误。即使它们被核心注释(根据JPA2规范),它们也不适用于Hibernate(Hibernate JPA2实现存在许多此类错误)。最后我所做的是为鉴别器列(@DiscriminatorValue("SomeValue")
)添加非常具体的(虽然从JPA2s观点来看不是强制性的)配置(通过注释),用于JOIN(@PrimaryKeyJoinColumn(name = "idColumn")
)的列。从您的代码剪切后看来,您似乎没有使用继承,但如果您实际上是,请尝试使用。
此外,如果您的项目允许,请从Hibernate切换到Eclipselink作为JPA2实现。我有无数次出现,其中正确的JPA2代码在EclipseLink中运行良好,但在Hibernate中引发了bizzare异常。