0

I am working in project with spring4 hibernate entitymanager4. I am trying to create table in my oracle db from hibernate automatically.

But the table is not getting generated in db as expected.

Here is my pom.xml file of my persist

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                             http://maven.apache.org/maven-v4_0_0.xsd">
    <parent>
        <groupId>com.spectra.lev</groupId>
        <artifactId>myca</artifactId>
        <version>0.1-SNAPSHOT</version>
    </parent>
    <artifactId>myca_persist</artifactId>
    <packaging>jar</packaging>
    <url>http://maven.apache.org</url>
    <modelVersion>4.0.0</modelVersion>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
        <skipTests>true</skipTests>
        <hibernate.version>4.3.11.Final</hibernate.version>
        <spring.version>4.1.4.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
    </dependencies>
</project>

Below is my persistence.xml, here I configured my oracle db

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">

    <persistence-unit name="oracDB" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:oracDB" />
            <property name="javax.persistence.jdbc.user" value="SYSTEM" />
            <property name="javax.persistence.jdbc.password" value="password" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
            <property name="hibernate.event.merge.entity_copy_observer"
                value="allow" />
        </properties>
    </persistence-unit>
</persistence>

And I have a Entity class for user, This entity is not getting created in my local db.

import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "User")
public class UserEntity {

    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    @Column(name = "_id")
    private String id;

    @Column(name = "firstName")
    private String firstName;

    @Column(name = "lastName")
    private String lastName;

    @Column(name = "updatedDate")
    private Date updatedDate = new Date();


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Date getUpdatedDate() {
        return updatedDate;
    }

    public void setUpdatedDate(Date updatedDate) {
        this.updatedDate = updatedDate;
    }

}

I have changed hbm2ddl.auto value to update. But there is no result.

Can anyone assist here, what I am missing.

2 Answers 2

1

You should include the following in your persistence.xml in order to generate tables automatically based on your Hibernate models.

<property name="hibernate.hbm2ddl.auto" value="update" />                        

For more info,please take a look http://www.onlinetutorialspoint.com/hibernate/hbm2ddl-auto-example-hibernate-xml-config.html

    <properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost/DB" />
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
        <property name="hibernate.connection.username" value="username" />
        <property name="hibernate.connection.password" value="password" />
        <property name="hibernate.connection.autoReconnect" value="true" /> 
        <property name="hibernate.connection.autoReconnectForPools" value="true" />
        <property name="hibernate.connection.is-connection-validation-required" value="true" />
        <property name="hibernate.connection.testOnBorrow" value="true" />
        <property name="hibernate.connection.validationQuery" value="SELECT 1" />
        <property name="hibernate.archive.autodetection" value="class" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
    </properties>
Sign up to request clarification or add additional context in comments.

Comments

1

I can see that you don't have your entity classes mapped in the persistence.xml file. Try including those in the persistence.xml file. Something like this:

<persistence-unit name="my-pu">
 <description>My Persistence Unit</description>
 <provider>com.objectdb.jpa.Provider</provider>
 <mapping-file>META-INF/mappingFile.xml</mapping-file>
 <jar-file>packedEntity.jar</jar-file>
 <class>sample.MyEntity1</class>
 <class>sample.MyEntity2</class>
 <properties>
   <property name="javax.persistence.jdbc.url"
             value="objectdb://localhost/my.odb"/>
   <property name="javax.persistence.jdbc.user" value="admin"/>
   <property name="javax.persistence.jdbc.password" value="admin"/>
 </properties>
   </persistence-unit>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.