gpt4 book ai didi

java - 如何使用hibernate创建新的oracle数据库

转载 作者:行者123 更新时间:2023-11-30 02:09:24 24 4
gpt4 key购买 nike

我是 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>

这里我的 hibernate 配置位于名为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 模式中创建了我的应用程序中的实体的表,如下图所示:

enter image description here

这是创建表的实体:

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

}

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

有谁知道如何使用 hibernate 来实现此目的?

最佳答案

what I am looking for is to create the new database instance

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

然后应用程序可以像该用户一样连接到数据库,并且 hibernate 将能够在该用户的架构中创建表。

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

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

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

关于java - 如何使用hibernate创建新的oracle数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50549043/

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