【问题标题】:Exception in thread "main" org.hibernate.exception.SQLGrammarException: ORA-02289: sequence does not exist线程“主”org.hibernate.exception.SQLGrammarException 中的异常:ORA-02289:序列不存在
【发布时间】:2016-01-27 21:58:22
【问题描述】:

我正在尝试执行一个基本的休眠应用程序。但是,我一直收到问题中发布的错误。

下面贴的是我的项目结构code:

下面是 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截图

任何帮助将不胜感激。 谢谢!

【问题讨论】:

  • 请发布错误消息,而不是部分消息的屏幕截图。无论如何,原因很明显:数据库中缺少SEQUENCE
  • @MickMnemonic 你能告诉我你在说什么序列吗?
  • 如果不查看完整的堆栈跟踪并知道错误的来源,真的很难知道。它可能是用于生成联系人 ID 的 SEQUENCE

标签: java spring oracle hibernate


【解决方案1】:

您已将Contact 实体注释为使用SEQUENCE 作为策略。但是您没有指定应该使用哪个序列。 (我相信这是您可能遇到的错误。如果没有,发布异常堆栈跟踪会有所帮助。)

在这种情况下,默认情况下,hibernate 会查找名为 hibernate_sequence 的序列,并使用此名称创建序列。

或者,如果您希望 hibernate 使用您已经创建的序列(例如 your_sequence_name),那么进一步限定 @Id 属性如下应该会有所帮助:

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

【讨论】:

    【解决方案2】:

    通常我们不需要在我们的数据库中创建表/序列,hibernate 可以管理相同的。

    为此,您需要在 hibernate.cfg.xml 中添加以下标签

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

    hbm2ddl.auto 可能的值很少:

    • create :当您创建 SessionFactory 时,休眠删除所有内容(如果存在)并为您重新创建它。

    • create-drop : 在启动时创建所有内容并在关闭时删除它们

    • 更新:更新架构。

    • validate :验证架构,不对数据库进行任何更改。

    无论如何,如果你想显式创建表/序列,你可以创建它们。

    但是在使用@GeneratedValue 时,您必须指定您的序列。

    【讨论】:

      猜你喜欢
      • 2014-08-31
      • 2015-07-22
      • 1970-01-01
      • 2015-06-30
      • 1970-01-01
      • 2021-09-19
      • 2016-07-23
      • 2021-09-03
      • 1970-01-01
      相关资源
      最近更新 更多