小编典典

org.hibernate.dialect.OracleDialect不支持身份密钥生成

hibernate

我试图将一个示例项目导入到Eclipse中,并且在运行该应用程序时遇到以下给定的错误。

Caused by: org.hibernate.MappingException: org.hibernate.dialect.OracleDialect does not support identity key generation
    at org.hibernate.dialect.Dialect.getIdentityColumnString(Dialect.java:743)
    at org.hibernate.dialect.Dialect.getIdentityColumnString(Dialect.java:733)
    at org.hibernate.mapping.Table.sqlCreateString(Table.java:426)
    at org.hibernate.cfg.Configuration.generateSchemaCreationScript(Configuration.java:1028)
    at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:125)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:492)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1782)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:247)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:373)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:358)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
    ... 32 more

按照 SO链接,我已更改了

@GeneratedValue(strategy = GenerationType.IDENTITY)

@GeneratedValue(strategy = GenerationType.AUTO) 要么 @GeneratedValue(strategy = GenerationType.TABLE)

但是没有用。

这是代码:

User.java:

@Entity
@Table(name = "users")
@ManagedBean
@ViewScoped
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "username", nullable = false)
    private String username;

    @Column(name = "password", nullable = false)
    private String password;

    @Column(name = "role", nullable = false)
    private String role;

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    }

从applicationContext.xml:

<!-- Session Factory Declaration -->
<bean id="SessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="DataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.crud.model.User</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>

        </props>
    </property>
</bean>

阅读 469

收藏
2020-06-20

共1个答案

小编典典

您可以使用告诉Hibernate使用序列来生成您的ID

@Id
@Column(name = "ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence")
@SequenceGenerator(name = "id_Sequence", sequenceName = "ID_SEQ")
private int id;

这个配置基本上告诉Hibernate使用一个名为ID_SEQ的数据库序列来生成该对象的ID。如果需要其他唯一ID,则可以在其他对象上指定其他序列;如果要在整个系统中使用全局唯一ID,则可以使用相同的序列。

唯一的缺点是不能执行批量插入(没有其他配置),因为Hibernate每次都需要从数据库中获取下一个序列值,如果要使用MySQL数据库,则不能使用此配置。
,因为它们不支持序列。

如果没有任何意义,请通知我,我将作进一步解释。

2020-06-20