0

我在 Java 6 上使用 Hibernate 3 / JPA。我试图从数据库(已填充)中获取一个简单的行。

创建了以下 JPA 注释域对象:

@Entity
@Table(name = "person", catalog = "mydatabase", uniqueConstraints = {
        @UniqueConstraint(columnNames = "person_name") })
public class Person {
    private Integer id;

    private String personName;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return id;
    }

    @Column(name = "person_name", unique = true, nullable = false, length = 30)
    public String getPersonName() {
        return personName;
    }

    public void setPersonName(String personName) {
        this.person = person;
    }
}

我的 hibernate.cfg.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydatabase</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="current_session_context_class">thread</property>
        <mapping class="com.myapp.domain.Person"></mapping>
    </session-factory>
</hibernate-configuration>

调用类的代码片段:

session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
org.hibernate.Query query = session.createQuery("SELECT p FROM Person p");
List<Person> persons = query.getResultList();

当我运行调用类时,这是我得到的异常:

java.lang.ClassCastException: org.hibernate.impl.QueryImpl cannot be cast to javax.persistence.Query
    at com.myapp.PersonApp(PersonApp.java:15)

发生此 ClassCastException 的第 15 行是:

org.hibernate.Query query = session.createQuery("SELECT p FROM Person p");

这是我的依赖项:

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.0.8</version>
    </dependency>

    <dependency>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>hibernate3-maven-plugin</artifactId>
        <version>3.0</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.5.6-Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.2.0.Final</version>
    </dependency>

    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>

    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.0.0</version>
        <scope>provided</scope>
    </dependency>

我可能做错了什么?我只想针对人员表调用一个简单的选择查询。

感谢您抽出时间来阅读。

4

0 回答 0