Hibernate + JPA不会自动在oracle中创建表

时间:2017-04-04 02:13:41

标签: oracle hibernate maven jpa

我正在使用spring4 hibernate entitymanager4进行项目。我正在尝试从hibernate自动在oracle db中创建表。

但该表未按预期在db中生成。

这是我的persist

的pom.xml文件
<?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>

下面是我的persistence.xml,这里我配置了我的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>

我有一个用户的Entity类,这个实体没有在我的本地数据库中创建。

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

}

我已将hbm2ddl.auto值更改为update。但是没有结果。

任何人都可以在这里提供帮助,我缺少什么。

2 个答案:

答案 0 :(得分:1)

我可以看到你没有在persistence.xml文件中映射你的实体类。尝试在persistence.xml文件中包含这些内容。像这样:

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

答案 1 :(得分:1)

您应该在persistence.xml中包含以下内容,以便根据您的Hibernate模型自动生成表格。

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

如需了解更多信息,请查看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>