hibernate持久性不会创建表

时间:2016-05-30 16:54:34

标签: spring hibernate persistence persistence.xml

我在我的spring mvc项目中使用了以下maven依赖项:

<!-- Hibernate Validator -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.2.0.Final</version>
</dependency>

<!-- hibernate -->


<dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.1-901.jdbc4</version>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.2.16.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.0-api</artifactId>
    <version>1.0.0.Final</version>
</dependency>

这是我的 persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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_1_0.xsd" version="1.0">

<persistence-unit name="PersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.sepehrgh.domain.entity.UploadEntity</class>

    <properties>
        <property name="hibernate.connection.url" value="jdbc:postgresql://localhost/test_jpa" />
        <property name="hibernate.connection.useUnicode" value="true" />
        <property name="hibernate.connection.characterEncoding" value="UTF-8" />
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
        <property name="hibernate.connection.username" value="postgres" />
        <property name="hibernate.connection.password" value="" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect" />
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
    </properties>
</persistence-unit>
</persistence>

UploadEntity

@Entity
@Table(name = "uploads", schema = "public")
public class UploadEntity {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column
    private long id;
    @Column
    private String title;
    @Column(columnDefinition = "text")
    private String description;

    public long getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

当我运行项目时。我收到以下错误,因此没有创建表: 201

6-05-30 21:07:38 ERROR SchemaExport:425 - HHH000389: Unsuccessful: create table public.uploads (id  bigserial not null, description text, title varchar(255), primary key (id))
2016-05-30 21:07:38 ERROR SchemaExport:426 - type not found or user lacks privilege: BIGSERIAL

它完全奇怪,因为我在另一个较旧的项目中使用相同的代码和逻辑,并且完全正常工作!我错过了什么吗?

此外,您可能需要知道persistence.xml位于标记为资源的目录中的META-INF下。它在我所有其他工作项目中的位置相同!任何想法?

仅供参考:当我在postgresql服务器中运行此查询“create table public.uploads (id bigserial not null, description text, title varchar(255), primary key (id))”时,它运行正常!我的postgresql版本是9.4,我正在使用TomEE web-profile 1.7在Intellij Idea中运行我的项目

1 个答案:

答案 0 :(得分:0)

在EE服务器中,JPA对RESOURCE_LOCAL持久性单元使用非jta-datasource。

在resources.xml / tomee.xml中配置数据源或在conf / system.properties中设置:openejb.guess.resource-local-datasource-properties-configured = true