簡體   English   中英

線程“主” org.hibernate.exception.SQLGrammarException中的異常:ORA-02289:序列不存在

[英]Exception in thread “main” org.hibernate.exception.SQLGrammarException: ORA-02289: sequence does not exist

我正在嘗試執行一個基本的休眠應用程序。但是,我一直在遇到問題中所發布的錯誤。

下面發布的是我的項目結構 在此處輸入圖片說明 碼:

以下是app.java中存在的代碼

公共類應用{

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    Session session =hibernate_utils.getSessionFactory().openSession();
    session.beginTransaction();
    contact  contact=new contact();
    contact.setFirstname("xxx");
    contact.setLastname("xxx");
    contact.setEmail("xxxxxxx@gmail.com");
    contact.setTelephone("xxxxxxxxxx");
    session.save(contact);
    session.getTransaction().commit();
    System.out.println("saved");
    }

  }

下面發布的是contact.java文件中存在的代碼

package net.rishanth.contact.form;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Column;
import static javax.persistence.GenerationType.SEQUENCE;

@Entity
@Table(name = "contacts")
public class contact {

@Id
@GeneratedValue(strategy=SEQUENCE)
@Column(name = "id", unique = true, nullable = false)     
public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}

@Column(name = "firstname", nullable = false)
public String getFirstname() {
    return firstname;
}
public void setFirstname(String firstname) {
    this.firstname = firstname;
}
@Column(name = "lastname", nullable = false)
 public String getLastname() {
    return lastname;
}
public void setLastname(String lastname) {
    this.lastname = lastname;
}
@Column(name = "email", nullable = false)
 public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
@Column(name = "telephone", nullable = false)

public String getTelephone() {
    return telephone;
}
public void setTelephone(String telephone) {
    this.telephone = telephone;
}
private String firstname;
private String lastname;
private String email;
private String telephone;
private Integer id;

} 

下面發布的是服務包中存在的hiber_utils類的代碼。

package net.rishanth.contact.service;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class hibernate_utils {

private static final SessionFactory sessionfactory= buildSessionFatory();

@SuppressWarnings("deprecation")
private static SessionFactory buildSessionFatory(){
    // TODO Auto-generated method stub

        return new Configuration().configure().buildSessionFactory();



}
public static SessionFactory getSessionFactory()
{

    return sessionfactory;
}
public static void shutdown()
{

    getSessionFactory().close();
}

}

下面是hibernate.cnfg.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">oracle.jdbc.driver.OracleDriver
 </property>
 <property   
 name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:XE
 </property>
 <property   name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect
 </property>
 <property name="hibernate.connection.username">system</property>
 <property name="hibernate.connection.password">xxxxx</property>
 <mapping class="net.rishanth.contact.form.contact"></mapping>
 </session-factory>
 </hibernate-configuration>

下面的附件是我的oracle屏幕截圖 在此處輸入圖片說明

任何幫助將不勝感激。 謝謝!

您已經注釋了Contact實體,使用SEQUENCE作為策略。 但是您尚未指定應使用哪個序列。 (我相信這是您可能會遇到的錯誤。如果沒有,則發布異常堆棧跟蹤會有所幫助。)

在這種情況下,默認情況下,hibernate查找一個名為hibernate_sequence的序列,並使用該名稱創建一個序列會有所幫助。

或者,如果希望休眠使用已創建的序列(例如,your_sequence_name),則進一步限定@Id屬性如下所示將有幫助:

@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="mySeq")
@GenericGenerator(name="mySeq", strategy="sequence", 
                parameters={
                        @Parameter(name="sequence_name", value="your_sequence_name")
                })

通常我們不需要在數據庫中創建表/序列,hibernate可以對其進行管理。

為此,您需要在hibernate.cfg.xml中添加以下標記

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

hbm2ddl.auto有幾個可能的值:

  • create:創建SessionFactory時,休眠刪除所有內容(如果存在)並再次為您創建。

  • create-drop:在啟動時創建所有內容,在關閉時創建它們

  • update:更新架構。

  • validate:驗證模式,不對數據庫進行任何更改。

無論如何,如果要顯式創建表/序列,則可以創建它們。

但是隨后在使用@GeneratedValue時,您必須指定序列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM