开发者

Problem with JPA2 and Hibernate

开发者 https://www.devze.com 2023-03-16 07:48 出处:网络
I have this problem: Exception in thread \"main\" javax.persistence.PersistenceException: [PersistenceUnit: fb-persistence] Unable to configure EntityManagerFactory

I have this problem:

 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

Who can help me to solve? I have configured Hibernate with JPA, the file persistence.xml is:

<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>

and my class with the mapping is:

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;
    }
}

What is the problem? Help me? I was forgetting, i have configured the dependenci with maven2 and file pom.xml is:

...
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.6.3.Final</version>
        </dependency>
       <dependency>
            <groupId>org.hibernate&开发者_运维技巧lt;/groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.2.0.Final</version>
        </dependency>
...

Thanks


I would bet that you have more than one hibernate jar on your classpath and that's why it's throwing a class cast exception.

The other bit that looks wrong (but might be right) is that you're using JTA transactions. If you have no clue what JTA means, change the transaction-type on persistance.xml to transaction-type="RESOURCE_LOCAL"


I used to get this error when having Entity beans that inherited from one another. Even though they were corectly annotated (in accordance to JPA2 specs), they didn't work in Hibernate (there are numerous such bugs with Hibernate JPA2 implementation). Finally what I did is add very specific (though not mandatory from JPA2s point of view) configuration (via annotations) for the discriminator column (@DiscriminatorValue("SomeValue")), the columns used for the JOINs (@PrimaryKeyJoinColumn(name = "idColumn")). From your code snipped it doesn't appear that you're using inheritance, but if you infact are, try that.

Also, if your project allows it, switch from Hibernate to Eclipselink as a JPA2 implementation. I had countless occurances where correct JPA2 code worked fine in EclipseLink but threw bizzare exceptions in Hibernate.

0

精彩评论

暂无评论...
验证码 换一张
取 消