gpt4 book ai didi

java - Spring /hibernate/甲骨文 : ORA-02289 Sequence Does Not Exist?

转载 作者:行者123 更新时间:2023-11-30 08:34:27 26 4
gpt4 key购买 nike

尝试将新对象插入我的 Oracle 表时出现 java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist。该表确实有一个序列,每个条目都会自动递增。

我已经坚持这个问题几个小时了,在遵循了对这个问题和其他文章的类似回答之后,我仍然坚持下去。

我的类(class):

import java.sql.Timestamp;

import javax.persistence.Column;
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 org.springframework.stereotype.Component;

@Entity
@Table(name = "MY_SCHEMA.MY_TABLE")
@Component
public class SomeClass {
@Id
@SequenceGenerator(name = "MY_SEQ", sequenceName = "MY_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MY_SEQ")
@Column(name = "MY_ID")
private Integer myId;

@Column(name = "MY_TS")
private Timestamp ts;

@Column(name = "MY_PARAM")
private String myParameters;

@Column(name = "ANOTHER_TS")
private Timestamp anotherTimestamp;

// empty constructor and getters/setters

}

类的 DAO:

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.springframework.stereotype.Component;

import mypackage.mysubpackage.SomeClass;

@Component
public class SomeClassDAO {

private Session currentSession;
private Transaction currentTransaction;

private static SessionFactory getSessionFactory() {
Configuration configuration = new Configuration().configure();
configuration.addAnnotatedClass(SomeClass.class);
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
SessionFactory factory = configuration.buildSessionFactory(builder.build());
return factory;
}

public Session openCurrentSession() {
currentSession = getSessionFactory().openSession();
return currentSession;
}

public Session openCurrentSessionWithTransaction() {
currentSession = getSessionFactory().openSession();
currentTransaction = currentSession.beginTransaction();
return currentSession;
}

public void closeCurrentSession() {
currentSession.close();
}

public void closeCurrentSessionWithTransaction() {
currentTransaction.commit();
currentSession.close();
}

public Session getCurrentSession() {
return currentSession;
}

public void setCurrentSession(Session currentSession) {
this.currentSession = currentSession;
}

// post
public void insertNew() {
SomeClass obj = new SomeClass();
obj.setParameters("abc");
getCurrentSession().save(obj);
}

}

序列的 DDL 片段:

begin
if inserting then
if :NEW."MY_ID" is null then
select MY_SEQ.nextval into :NEW."MY_ID" from dual;
end if;
end if;
end;

hibernate .cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-5.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:oracle:thin:@servername.company.net:123:ABC</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="connection.username">user</property>
<property name="connection.password">pass</property>
<property name="show_sql">true</property>


</session-factory>

</hibernate-configuration>

mvc-dispatchet-servlet.xml 片段:

<context:component-scan base-package="mypackage.mysubpackage"></context:component-scan>
<mvc:annotation-driven/>
<context:annotation-config/>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@servername.company.net:123:ABC"/>
<property name="username" value="user"/>
<property name="password" value="pass"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:packagesToScan="mypackage.mysubpackage"
p:dataSource-ref="dataSource">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true"/>
<property name="showSql" value="true"/>
</bean>
</property>
</bean>

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

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

最佳答案

begin
if inserting then
if :NEW."MY_ID" is null then
select MY_SEQ.nextval into :NEW."MY_ID" from dual;
end if;
end if;
end;

这在我看来像是 oracle 的一部分 trigger而不是实际的甲骨文 Sequence .检查您的模式中是否实际存在名称为“MY_SEQ”的序列。

如果您的 id 列上有当前 JPA 注释的序列,则不需要触发器。 JPA本身可以在没有触发器的情况下获取序列的下一个值。

如果您仍想继续使用触发器,请阅读 here .

关于java - Spring /hibernate/甲骨文 : ORA-02289 Sequence Does Not Exist?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38832059/

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