【问题标题】:How to create a new oracle database with hibernate如何使用hibernate创建一个新的oracle数据库
【发布时间】:2018-05-27 04:36:56
【问题描述】:

我是 hibernate 新手,我正在学习 Java EE 应用程序中的 hibernate 教程,这是我的 persistence.xml 文件:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">

    <persistence-unit name="punit">

    </persistence-unit>
</persistence>

这里是我的休眠配置,它位于名为 jpaContext.xml 的文件中。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="punit" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
            </bean>
        </property>

        <property name="jpaPropertyMap">
            <map>
                <entry key="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
                <entry key="hibernate.hbm2ddl.auto" value="create" />
                <entry key="hibernate.format_sql" value="true" />
            </map>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
        <property name="username" value="SYS AS SYSDBA" />
        <property name="password" value="password" />
    </bean>

我遇到的问题是,当我(第一次)启动服务器时,没有创建新的数据库,但只有我在应用程序中拥有的实体的表,在 SYS 模式中,如下图所示:

这是创建表的实体:

package com.pluralsight.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Table;
import javax.persistence.Id;

import org.hibernate.validator.constraints.Range;

@Entity
@Table(name="goals")
public class Goal {

    @Id 
    @GeneratedValue
    private Long Id;

    @Range(min = 1, max = 120)
    @Column(name="MINUTES")
    private int minutes;

    public int getMinutes() {
        return minutes;
    }

    public void setMinutes(int minutes) {
        this.minutes = minutes;
    }

    public Long getId() {
        return Id;
    }

    public void setId(Long id) {
        Id = id;
    }

}

虽然我正在寻找的是为我正在开发的应用程序创建新的数据库实例(如果它不存在)并提供它(与应用程序相同)

有谁知道我如何使用休眠实现这一目标?

【问题讨论】:

  • 您是否遇到任何错误,因为您的配置文件看起来正确!

标签: java hibernate oracle12c


【解决方案1】:

我正在寻找的是创建新的数据库实例

MySQL 之类的称为数据库,Oracle 称为模式。但是,在 MySQL 中,数据库与用户是分开的且不同的,而在 Oracle 中,模式几乎是用户的同义词。如此有效地需要在您的应用程序(或者至少是 Hibernate SessionFactory)启动之前创建用户/模式。不幸的是,Oracle JDBC 驱动程序不会为您执行此操作。

您可以按如下方式创建用户:

create user foobar identified by somepass;

然后给它适当的权限

grant connect to foobar;
grant create sequence to foobar;
grant create session to foobar;
grant create table to foobar;
grant create trigger to foobar;
grant unlimited tablespace to foobar;
...

然后应用程序可以连接到数据库,因为该用户和休眠将能够在该用户的架构中创建表。

或者,您可以只创建用户/架构并继续以其他用户(例如 SYS)的身份连接,但在 Hibernate 中设置默认架构:

<property name="hibernate.default_schema" value="foobar"/>

这将确保在正确的架构中创建所有表。

【讨论】:

    【解决方案2】:

    我可以建议你一件事,虽然我不知道它是否有效。添加属性

    <entry key="hibernate.default_schema" value="ABC" /> 
    

    并更改&lt;entry key="hibernate.hbm2ddl.auto" value="create" /&gt;

    <entry key="hibernate.hbm2ddl.auto" value="update" />.
    

    您可以查看这些链接!

    spring boot automatically create database schema Error

    https://developer.jboss.org/thread/106922]

    https://docs.jboss.org/jbossas/jboss4guide/r2/html/ch13.html

    【讨论】:

    猜你喜欢
    • 2019-04-16
    • 1970-01-01
    • 2017-02-11
    • 2017-05-18
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 2020-09-02
    • 2012-02-23
    相关资源
    最近更新 更多