为了账号安全,请及时绑定邮箱和手机立即绑定

线程“主”javax.persistence.PersistenceException 中的异常:

线程“主”javax.persistence.PersistenceException 中的异常:

慕少森 2022-05-21 20:39:10
我正在使用带有 orcale 12c 和 java 9 的休眠 5 当我执行此代码错误时发生休眠无法执行语句我在 Hibernate 编写了简单的程序并捕获了一堆异常。我无法找出到底出了什么问题。我有三个班级——学生、主班和hibernate.cfg、xml。这是非常简单的休眠代码,只涉及一个类,即学生类学生.JAVApackage hibernate.demo.entity;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity//@Table(name="student")public class Student {    @Id//  @GeneratedValue(strategy=GenerationType.AUTO)    @Column(name="id")    private int id;    @Column(name="first_Name")    private String firstName;    @Column(name="last_Name")    private String lastName;    @Column(name="email")    private String email;    public Student() {    }    public Student(String firstName, String lastName, String email) {        super();        this.firstName = firstName;        this.lastName = lastName;        this.email = email;    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getFirstName() {        return firstName;    }    public void setFirstName(String firstName) {        this.firstName = firstName;    }    public String getLastName() {        return lastName;    }    public void setLastName(String lastName) {        this.lastName = lastName;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }    @Override    public String toString() {        return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";    }}
查看完整描述

3 回答

?
繁华开满天机

TA贡献1816条经验 获得超4个赞

stacktrace 中的错误清楚地表明该表未找到。


Caused by: java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist

因此,您可以在 oracle db 中的模式中创建一个表,或者告诉 hibernate 在创建会话工厂时创建表。


您可以在休眠配置文件中包含以下属性


<property name="hibernate.hbm2ddl.auto" value="create"/>

所以hibernate会为你创建一个表。


此外,您需要使用 @Table 属性注释您的实体类我看到该行在您的代码中已注释



查看完整回答
反对 回复 2022-05-21
?
慕哥9229398

TA贡献1877条经验 获得超6个赞

就我而言,当有人使用最新版本的休眠时会生成此错误。

为了解决它,只需转到hibernate.cfg.xml文件并根据您的版本更改方言。就我而言,我在 MySQL Workbench 中使用版本 5。

休眠.cfg.xml

<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

唯一完成的更改是:

<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>


查看完整回答
反对 回复 2022-05-21
?
Qyouu

TA贡献1786条经验 获得超11个赞

我遇到了同样的问题-此错误是因为您的依赖项彼此不兼容。


Use the below dependencies and the error should be gone. 


<dependencies>

    <dependency>

        <groupId>org.hibernate</groupId>

        <artifactId>hibernate-agroal</artifactId>

        <version>5.3.15.Final</version>

        <type>pom</type>

    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->

    <dependency>`enter code here`

        <groupId>mysql</groupId>

        <artifactId>mysql-connector-java</artifactId>

        <version>8.0.15</version>

    </dependency>

     <!--https://mvnrepository.com/artifact/org.hibernate/antlr-->

    <dependency>

        <groupId>org.hibernate</groupId>

        <artifactId>antlr</artifactId>

        <version>2.7.5H3</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->

    <dependency>

        <groupId>org.hibernate</groupId>

        <artifactId>hibernate-core</artifactId>

        <version>5.4.10.Final</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/org.hsqldb/hsqldb -->

    <dependency>

        <groupId>org.hsqldb</groupId>

        <artifactId>hsqldb</artifactId>

        <version>2.4.0</version>

        <scope>test</scope>

    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml/classmate -->

    <dependency>

        <groupId>com.fasterxml</groupId>

        <artifactId>classmate</artifactId>

        <version>1.3.1</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->

    <dependency>

        <groupId>dom4j</groupId>

        <artifactId>dom4j</artifactId>

        <version>1.6.1</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.el/el-api -->

    <dependency>

        <groupId>javax.el</groupId>

        <artifactId>el-api</artifactId>

        <version>2.2</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.geronimo.specs/geronimo-jta_1.1_spec -->

    <dependency>

        <groupId>org.apache.geronimo.specs</groupId>

        <artifactId>geronimo-jta_1.1_spec</artifactId>

        <version>1.1.1</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/org.jboss/jandex -->

    <dependency>

        <groupId>org.jboss</groupId>

        <artifactId>jandex</artifactId>

        <version>2.1.2.Final</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/javassist/javassist -->

    <dependency>

        <groupId>javassist</groupId>

        <artifactId>javassist</artifactId>

        <version>3.12.1.GA</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.activation/javax.activation-api -->

    <dependency>

        <groupId>javax.activation</groupId>

        <artifactId>javax.activation-api</artifactId>

        <version>1.2.0</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core -->

    <dependency>

        <groupId>com.sun.xml.bind</groupId>

        <artifactId>jaxb-core</artifactId>

        <version>2.3.0.1</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->

    <dependency>

        <groupId>com.sun.xml.bind</groupId>

        <artifactId>jaxb-impl</artifactId>

        <version>2.3.2</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->

    <dependency>

        <groupId>javax.persistence</groupId>

        <artifactId>javax.persistence-api</artifactId>

        <version>2.2</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->

    <dependency>

        <groupId>javax.xml.bind</groupId>

        <artifactId>jaxb-api</artifactId>

        <version>2.3.1</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime -->

    <dependency>

        <groupId>org.glassfish.jaxb</groupId>

        <artifactId>jaxb-runtime</artifactId>

        <version>2.3.1</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/org.jboss.logging/jboss-logging -->

    <dependency>

        <groupId>org.jboss.logging</groupId>

        <artifactId>jboss-logging</artifactId>

        <version>3.3.2.Final</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/org.jboss.spec.javax.transaction/jboss-transaction-api_1.2_spec -->

    <dependency>

        <groupId>org.jboss.spec.javax.transaction</groupId>

        <artifactId>jboss-transaction-api_1.2_spec</artifactId>

        <version>1.1.1.Final</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/org.jvnet.staxex/stax-ex -->

    <dependency>

        <groupId>org.jvnet.staxex</groupId>

        <artifactId>stax-ex</artifactId>

        <version>1.8</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/org.glassfish.jaxb/txw2 -->

    <dependency>

        <groupId>org.glassfish.jaxb</groupId>

        <artifactId>txw2</artifactId>

        <version>2.3.1</version>

    </dependency>

</dependencies>



查看完整回答
反对 回复 2022-05-21
  • 3 回答
  • 0 关注
  • 115 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信