3

I am facing the following error when trying to insert a Person record to the DB.

Caused by: java.sql.SQLSyntaxErrorException: ORA-02201: sequence not allowed here

I am wokring with Hibernate 5.2.12.Final, Oracle 12 c database and making use of Identity column in the DB for 'id'.

select 1 from dual

Works. So I am sure the DB connection is a success.

Below is the config:

DB:

CREATE TABLE person
(
id NUMBER(18) GENERATED ALWAYS AS IDENTITY
            INCREMENT BY 1 START WITH 1 NOT NULL,
name VARCHAR2(40) NOT NULL
);

ALTER TABLE person ADD CONSTRAINT pk_person PRIMARY KEY (id);

HibernateUtil

public class HibernateUtil {
private static StandardServiceRegistry registry;
private static SessionFactory sessionFactory;

public static SessionFactory getSessionFactory() {
    if (sessionFactory == null) {


        StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
                .configure(new File("path-to-hibernate.cfg.xml"))
                .build();

        Metadata metadata = new MetadataSources(standardRegistry)
                .addAnnotatedClass(Person.class)
                .addAnnotatedClassName("com.example.entity.Person")
                .getMetadataBuilder()
                .applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE)
                .build();

        sessionFactory = metadata.getSessionFactoryBuilder()
                .build();


    }
    return sessionFactory;
}

public static void shutdown() {
    if (registry != null) {
        StandardServiceRegistryBuilder.destroy(registry);
    }
}
}

Person entity:

@Entity
@Table(name = "Person")
public class Person implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

public Person(){}

public void setId(Long id) {
    this.id = id;
}

public void setName(String name) {
    this.name = name;
}

public Person(final String name){
    this.name = name;
}

public Long getId() {
    return id;
}

public String getName() {
    return name;
}

@Column(name = "name")
private String name;
}

main ():

Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();

    session.save(new Person("code")); // Error here

    session.getTransaction().commit();
    session.close();

    HibernateUtil.shutdown();

Any help is highly appreciated.:)

Raghav Sharma
  • 68
  • 1
  • 9

1 Answers1

0

Instead GenerationType.IDENTITY try to use GenerationType.SEQUENCE and it should work, let me know if it doesn't because nothing looks wrong in your code except that.

Santosh Jadi
  • 1,479
  • 6
  • 29
  • 55
  • With that change, I get the following in the log: `Hibernate: select system.hibernate_sequence.nextval from dual` `Hibernate: insert into system.Person (name, id) values (?, ?)` But the error remains. Sequence not allowed here. Also, I would want to use the IDENTITY sequence associated with 'id' and not hibernate_sequence – Raghav Sharma Jul 06 '18 at 03:14
  • Thanks, can you please provide me sample code in zip file – Sudarshan Deshmukh Jul 06 '18 at 18:56
  • Creating the same under a non-system user seemed to solve the issue. No code changes were done. – Raghav Sharma Jul 18 '18 at 07:12