gpt4 book ai didi

java - Hibernate 5,Oracle12c 标识列 - 找不到序列

转载 作者:行者123 更新时间:2023-12-05 07:32:29 24 4
gpt4 key购买 nike

我在尝试将 Person 记录插入数据库时​​遇到以下错误。

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

我正在使用 Hibernate 5.2.12.Final、Oracle 12 c 数据库并使用数据库中的标识列作为“id”。

select 1 from dual

有效。所以我确信数据库连接是成功的。

配置如下:

数据库:

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);

hibernate 工具

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);
}
}
}

个人实体:

@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;
}

主要():

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

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

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

HibernateUtil.shutdown();

非常感谢任何帮助。:)

最佳答案

相反,GenerationType.IDENTITY 尝试使用 GenerationType.SEQUENCE,它应该可以工作,如果不能,请告诉我,因为除了那个之外,您的代码看起来没有任何问题。

关于java - Hibernate 5,Oracle12c 标识列 - 找不到序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51202297/

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