簡體   English   中英

Hibernate Spring:未創建表[不成功:如果存在,則刪除表]

[英]Hibernate Spring : table not created [Unsuccessful: drop table if exists]

我正在一個angularjs spring mvc項目上工作,並面臨使用hibernate創建表的問題。

INFO: HV000001: Hibernate Validator 5.2.2.Final
Jun 18, 2016 4:54:49 AM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Hibernate: drop table if exists AppUser
Jun 18, 2016 4:54:49 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: drop table if exists AppUser
Jun 18, 2016 4:54:49 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: ORA-00933: SQL command not properly ended

Hibernate: create table AppUser (id integer not null auto_increment, dob varchar(255), email varchar(50), firstName varchar(255), lastName varchar(255), password varchar(255) not null, primary key (id))
Jun 18, 2016 4:54:49 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: create table AppUser (id integer not null auto_increment, dob varchar(255), email varchar(50), firstName varchar(255), lastName varchar(255), password varchar(255) not null, primary key (id))
Jun 18, 2016 4:54:49 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: ORA-00907: missing right parenthesis

模型類是一個簡單的類,看起來像

@Entity
public class AppUser {


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

    @Email
    @Size(max = 50)
    private String email;

    @Column
    private String dob;

    @Column
    private String firstName;

    @Column
    private String lastName;

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

    //getter setter

}

數據配置文件如下所示

@Configuration
@EnableTransactionManagement
public class AppDatabaseConfig {

    @Bean
    public HikariDataSource dataSource() {
       final HikariDataSource ds = new HikariDataSource();
       ds.setMaximumPoolSize(100);
       ds.setDriverClassName("oracle.jdbc.driver.OracleDriver"); 
       ds.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:XE"); ;
       ds.setUsername("");
       ds.setPassword("");
       return ds;
    }

    @Bean
    public HibernateTransactionManager transactionManager() {
        HibernateTransactionManager manager = new HibernateTransactionManager();
        manager.setSessionFactory(hibernate5SessionFactoryBean().getObject());
        return manager;
    }

    @Bean
    public LocalSessionFactoryBean hibernate5SessionFactoryBean(){
        LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
        localSessionFactoryBean.setDataSource(dataSource());
        localSessionFactoryBean.setAnnotatedClasses(AppUser.class);

        Properties properties = new Properties();
        properties.put("hibernate.dialect","org.hibernate.dialect.MySQLDialect");
        //properties.put("hibernate.current_session_context_class","thread");
        properties.put("hibernate.hbm2ddl.auto","create");
        properties.put("hibernate.show_sql","true");

        localSessionFactoryBean.setHibernateProperties(properties);
        return localSessionFactoryBean;
    }

}

我嘗試更改/ createdrop / create,但每次都會出錯。 該數據庫不包含該表。 不確定如何解決?

您使用了錯誤的方言。

properties.put("hibernate.dialect","org.hibernate.dialect.MySQLDialect")

對於Oracle 11G,您必須使用

org.hibernate.dialect.Oracle10gDialect

請參考官方鏈接以選擇適當的方言

https://docs.jboss.org/hibernate/orm/3.6/reference/zh-CN/html/session-configuration.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM