gpt4 book ai didi

java - Hibernate SQL异常

转载 作者:行者123 更新时间:2023-12-01 11:21:19 25 4
gpt4 key购买 nike

我想在我的一个表中插入一些数据,然后出现下一个异常:

`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>

现在,我尝试使用以下方法在角色表中插入一行:

@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 应该自动创建它。谁能解释一下为什么我会得到这个异常?

最佳答案

可能是你的权限问题,先执行下一条语句select * from all_sequences whereequence_name = 'YOUR_SEQUENCE';如果序列存在,你只需授予用户权限即可您在应用程序中使用的。使用 grant select on YOUR_SEQUENCE to YOU​​R_USER; 来解决您的问题。

关于java - Hibernate SQL异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31195647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com