线程“main”中的异常org.hibernate.exception.SQLGrammarException:ORA-02289:序列不存在

时间:2016-01-27 21:58:22

标签: java spring oracle hibernate

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

发布的是我的项目结构enter image description here代码:

以下是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 screenshot enter image description here

任何帮助都将受到高度赞赏。 谢谢!

2 个答案:

答案 0 :(得分:1)

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

在这种情况下,默认情况下,hibernate会查找名为@Id的序列,并创建一个具有此名称的序列应该会有所帮助。

或者,如果您希望hibernate使用您已创建的序列(例如,your_sequence_name),那么进一步限定@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="mySeq") @GenericGenerator(name="mySeq", strategy="sequence", parameters={ @Parameter(name="sequence_name", value="your_sequence_name") }) 属性如下所示应该有所帮助:

PID,USER,PR,NI,VIRT,RES,SHR,S,%CPU,%MEM,TIME+,COMMAND
3983,arun,20,0,1874616,500356,24132,R,47.6,10.3,53:19.92,gnome-shell
11,root,20,0,0,0,0,R,4.8,0.0,0:22.51,rcuos/0
2016,root,20,0,219396,44408,9120,S,4.8,0.9,9:53.96,Xorg
12081,arun,20,0,2689552,304132,7076,S,4.8,6.3,13:36.17,java
1,root,20,0,141660,5336,2940,S,0.0,0.1,0:08.40,systemd
3983,arun,20,0,1874616,500356,24132,R,56.2,10.3,53:20.99,gnome-shell
2016,root,20,0,219396,44408,9120,R,18.8,0.9,9:54.17,Xorg
4969,arun,20,0,1287084,162948,48472,S,12.5,3.4,5:39.99,chrome
16108,arun,20,0,130020,1692,1172,R,6.2,0.0,0:00.01,top
1,root,20,0,141660,5336,2940,S,0.0,0.1,0:08.40,systemd
3983,arun,20,0,1874616,500356,24132,R,43.8,10.3,53:24.56,gnome-shell
4969,arun,20,0,1275992,147104,32424,S,37.5,3.0,5:41.10,chrome
1,root,20,0,141660,5336,2940,S,0.0,0.1,0:08.40,systemd
2,root,20,0,0,0,0,S,0.0,0.0,0:00.06,kthreadd
3,root,20,0,0,0,0,S,0.0,0.0,0:04.93,ksoftirqd/0

答案 1 :(得分:0)

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

为此,您需要在hibernate.cfg.xml

中添加以下标记
<property name="hbm2ddl.auto">create</property>

hbm2ddl.auto有几个可能的值:

  • 创建:当您创建SessionFactory时,hibernate会删除所有内容(如果存在)并再次为您创建。

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

  • 更新:更新架构。

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

无论如何,如果你想明确地创建表/序列,你可以创建它们。

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