Reputation: 2649
I am new to hibernate and I am following a tutorial of hibernate within Java EE application, here is my persistence.xml file:
<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>
And here my hibernate configuration it is in file called 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>
The problem I have is when I start the server (for first time) no new database is created but only a table for an entity I have in my application, within the SYS schema as you can see in the image below:
Here is the entity which table is created:
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;
}
}
While what I am looking for is to create the new database instance (if it does not exist) for the application I am developing and to give it (the same that the application for example )
Does anyone know how can I achieve this with hibernate ?
Upvotes: 1
Views: 1387
Reputation: 3086
I can suggest you one thing, though i don't know whether it will work or not. Add property
<entry key="hibernate.default_schema" value="ABC" />
and change <entry key="hibernate.hbm2ddl.auto" value="create" />
to
<entry key="hibernate.hbm2ddl.auto" value="update" />.
You can check these links!
spring boot automatically create database schema Error
https://developer.jboss.org/thread/106922]
https://docs.jboss.org/jbossas/jboss4guide/r2/html/ch13.html
Upvotes: 1
Reputation: 3830
what I am looking for is to create the new database instance
What the likes of MySQL call a database, Oracle calls a schema. But whereas in MySQL, a database is separate and distinct from a user, in Oracle a schema is pretty much synonymous with a user. So effectively that user / schema needs to be created before your application (or, at least, the Hibernate SessionFactory) starts. The Oracle JDBC driver is not going to do that for you unfortunately.
You can create a user as follows:
create user foobar identified by somepass;
then give it the appropriate privileges
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;
...
then the application can connect to the database as that user and hibernate will be able to create the tables in that user's schema.
Alternatively, you can just create the user / schema and continue to connect as a different user (e.g. SYS) but set the default schema in Hibernate:
<property name="hibernate.default_schema" value="foobar"/>
which will ensure that all the tables are created in the correct schema.
Upvotes: 2