我一直在研究java spring项目,在其中创建了一个类,其唯一目的是记录数据库中已调用的每个服务的请求正文。
该表包含一个名为transaction_id的主键和其他记录请求所必需的字段。 Java中的表模型如下所示:
@Entity
@Table(name="ForntEndLogHistory")
public class FrontEndLog {
@Id
@Column(name = "TRANSACTION_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_generator")
@SequenceGenerator(name="id_generator", sequenceName = "id_seq2", allocationSize=1)
private Long transactionId;
private String request;
}
在数据库中,我创建了一个名为 id_seq2 的序列,而 id_seq1 已经存在:
新创建的id_seq2:
CREATE SEQUENCE "id_seq2" MINVALUE 0 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE ;
commit;
已经存在id_seq1 dll:
CREATE SEQUENCE "id_seq1" MINVALUE 0 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 3467 NOCACHE NOORDER NOCYCLE ;
情况1:
每当我使用下面的序列生成器(序列名称为id_seq2)触发邮递员的任何服务时,都会出现错误“序列不存在”。但是,该序列已经存在于数据库中。
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_generator")
@SequenceGenerator(name="id_generator", sequenceName = "id_seq2", allocationSize=1)
private Long transactionId;
项目war文件已成功部署在服务器上。之后,只要邮递员触发了服务,就会发生以下错误:
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:124)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:88)
at org.hibernate.id.SequenceGenerator.generateHolder(SequenceGenerator.java:122)
at org.hibernate.id.SequenceHiLoGenerator.generate(SequenceHiLoGenerator.java:73)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:654)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:646)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:642)
at com.etisalat.mwallet.data.BaseDAO.save(BaseDAO.java:49)
at com.etisalat.mwallet.service.impl.LoggerService.logFrontEnd(LoggerService.java:59)
at com.etisalat.mwallet.service.impl.LoggerService.logFrontEndSuccess(LoggerService.java:29)
at com.etisalat.mwallet.rest.config.ResponseFilter.doFilter(ResponseFilter.java:180)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3436)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3402)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2285)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2201)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2179)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1572)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:255)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:263)
Caused by: java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:522)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:587)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:225)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:53)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:774)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:925)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1111)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:4798)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:4845)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1501)
at weblogic.jdbc.wrapper.PreparedStatement.executeQuery(PreparedStatement.java:141)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)
情况2:
随后出于测试目的,我将序列名称从id_seq2更改为id_seq1并在服务器上重新部署了代码,令人惊讶的是我能够成功触发服务。
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_generator")
@SequenceGenerator(name="id_generator", sequenceName = "id_seq1", allocationSize=1)
private Long transactionId;
输出:
{
“ responseCode”:“ 0”
}
我不知道我在哪里犯了错误,因为我认为序列生成器中缺少某些东西。
答案 0 :(得分:1)
在您的情况下,该序列必须与您登录的模式/用户位于相同的模式中。如果您以其他用户身份登录,则可以尝试这种不礼貌的解决方案。
create public synonym "id_seq2" for "id_seq2";
grant select on "id_seq2" to public;
或者尝试找出“ id_seq1”中有哪些priv和同义词,并将其也应用于“ id_seq2”。
答案 1 :(得分:1)
存在的缺失序列可能是由于两个原因引起的:
1)序列存在于您所连接的其他模式中
在这种情况下,您必须引用schema_owner
。schema_name
2)序列存在于另一个架构中,并且您无权访问它
您首先应该意识到在哪个架构中创建了 existing 序列。
您可以通过以下查询进行验证(根据需要替换序列名称)
select OWNER, OBJECT_NAME SEQUENCE_NAME
from all_objects where object_type = 'SEQUENCE'
and lower(object_name) = 'id_seq1';
如果看不到任何内容,则说明该序列要么不存在,要么在连接时处于不同的模式中,并且没有SELECT
特权。
如果您使用的模式来自与Hibernate连接用户不同的模式(这是很常见的情况),则必须使用@SequenceGenerator
批注的schema
参数并传递适当的模式所有者。