Hibernate SQLException

时间:2015-07-02 22:25:10

标签: java spring hibernate jpa

我想在我的一个表中插入一些数据,然后我得到下一个例外:

`java.sql.SQLException: ORA-02289: sequence does not exist`

让我来展示我的代码。我有下一堂课:

@Entity
@Table(name="role")
public class Role implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO, generator="role_seq_gen")
    @SequenceGenerator(name="role_seq_gen", sequenceName="ROLE_SEQ")
    private Long roleId;

    @Column(name="role", unique=true)
    private String role;

    @ManyToMany(mappedBy = "roles")
    private List<Tipster> tipsters;

    // + getters and setters
}



@Entity
@Table(name="tipster")
public class Tipster implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO, generator="tipster_id_seq")
    @SequenceGenerator(name="tipster_id_seq", sequenceName="tipster_id_seq")
    @Column(name="tipsterId")
    private Long tipsterId;

    @NotEmpty
    @Column(name="username", unique=true)
    private String username;

    @NotEmpty
    @Column(name="email", unique=true)
    private String email;

    @NotEmpty
    @Column(name="password", unique=true)
    private String password;

    @Column(name="active")
    private int active;

    @ManyToMany
    @JoinTable
    private List<Role> roles;
    //+ getters and setters

}

这是我的应用程序上下文代码的一部分:

<context:annotation-config />

    <task:annotation-driven />

    <tx:annotation-driven transaction-manager="transactionManager" />


    <bean class="org.springframework.orm.jpa.JpaTransactionManager"
        id="transactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <jpa:repositories base-package="com.gab.gsn.repository" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@localhost:1521/XE" />
        <property name="username" value="gabrieltifui" />
        <property name="password" value="123321" />
    </bean>

    <bean
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        id="emf">
        <property name="packagesToScan" value="com.gab.gsn.entity" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
            </props>
        </property>
        <property name="persistenceProvider">
            <bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
        </property>
    </bean>

现在,我尝试使用此方法在Role表中插入一行:

@PostConstruct
    public void initDb(){
        Role role = new Role();
        role.setRole("User");
        roleRepository.save(role);
    }

当我在我的Apache服务器上的应用程序时,我得到下一个例外:

Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:89)
    at org.hibernate.id.SequenceGenerator.generateHolder(SequenceGenerator.java:122)
    at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:115)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:84)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1181)
    ... 57 more
Caused by: java.sql.SQLException: ORA-02289: sequence does not exist

    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
    at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:754)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:219)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:813)
    at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1051)
    at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:854)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1156)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3415)
    at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3460)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:80)
    ... 68 more

似乎我没有创建ROLE_SEQ序列,但我知道Hibernate应该自动创建它。谁能解释我为什么会遇到这个例外?

1 个答案:

答案 0 :(得分:1)

这可能是与您的权限相关的问题,首先执行下一个语句select * from all_sequences where sequence_name = 'YOUR_SEQUENCE' ;如果序列存在,您只需将权限授予您在应用程序中使用的用户。使用grant select on YOUR_SEQUENCE to YOUR_USER;来解决您的问题。