我是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模式中,如下图所示: / p>
以下是创建表的实体:
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实现这一目标?
答案 0 :(得分:1)
我可以建议你一件事,虽然我不知道它是否会起作用。添加属性
<entry key="hibernate.default_schema" value="ABC" />
并更改<entry key="hibernate.hbm2ddl.auto" value="create" />
到
<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
答案 1 :(得分:0)
我正在寻找的是创建新的数据库实例
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"/>
将确保所有表都以正确的架构创建。