使用JPA2和Hibernate自动创建表

时间:2017-06-02 08:54:23

标签: java hibernate jpa lombok

我在数据库中自动创建表时遇到问题。

我的 gradle dependecies

compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.0.Final'
compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '4.3.6.Final'
compile group: 'org.postgresql', name: 'postgresql', version: '42.0.0'

我的实体类(另外两个类看起来相似):

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.Set;

@Data
@NoArgsConstructor
@Entity
public class StreetData {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @OneToMany(cascade = CascadeType.ALL)
    private Set<ParkingSpaceData> parkingSpaces;
}

我的 persistance.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="parking-database-unit">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <class>soa.parking.database.domain.StreetData</class>
    <class>soa.parking.database.domain.ParkingSpaceData</class>
    <class>soa.parking.database.domain.TicketData</class>
    <properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/parking" />
        <property name="javax.persistence.jdbc.user" value="postgres" />
        <property name="javax.persistence.jdbc.password" value="*********" />
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />

        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="false" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect" />
        <property name="hibernate.hbm2ddl.auto" value="create" />
    </properties>
</persistence-unit>

这些我打包到.war文件并在jBoss服务器上部署。在日志中,我看到Hibernate生成的SQL(如下所示),但在数据库中没有创建表。部署期间没有任何错误。

10:40:22,210 INFO  [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 737) HHH000227: Running hbm2ddl schema export
10:40:22,212 INFO  [stdout] (ServerService Thread Pool -- 737) Hibernate: alter table StreetData_ParkingSpaceData drop constraint FKdbu6ec2cjp0lytdht5ct34r2
10:40:22,212 INFO  [stdout] (ServerService Thread Pool -- 737) Hibernate: alter table StreetData_ParkingSpaceData drop constraint FKnp5q60wl9xmiwpc8o1in8m7si
10:40:22,212 INFO  [stdout] (ServerService Thread Pool -- 737) Hibernate: drop table if exists ParkingSpaceData cascade
10:40:22,214 INFO  [stdout] (ServerService Thread Pool -- 737) Hibernate: drop table if exists StreetData cascade
10:40:22,215 INFO  [stdout] (ServerService Thread Pool -- 737) Hibernate: drop table if exists StreetData_ParkingSpaceData cascade
10:40:22,215 INFO  [stdout] (ServerService Thread Pool -- 737) Hibernate: drop table if exists TicketData cascade
10:40:22,215 INFO  [stdout] (ServerService Thread Pool -- 737) Hibernate: create table ParkingSpaceData (id  bigserial not null, empty boolean, primary key (id))
10:40:22,216 INFO  [stdout] (ServerService Thread Pool -- 737) Hibernate: create table StreetData (id  bigserial not null, name varchar(255), primary key (id))
10:40:22,217 INFO  [stdout] (ServerService Thread Pool -- 737) Hibernate: create table StreetData_ParkingSpaceData (StreetData_id int8 not null, parkingSpaces_id int8 not null, primary key (StreetData_id, parkingSpaces_id))
10:40:22,217 INFO  [stdout] (ServerService Thread Pool -- 737) Hibernate: create table TicketData (id  bigserial not null, createdDate timestamp, invalidateDate timestamp, streetId int8, primary key (id))
10:40:22,218 INFO  [stdout] (ServerService Thread Pool -- 737) Hibernate: alter table StreetData_ParkingSpaceData add constraint UK_fpad6yr01tqc30pvgc4decjq7 unique (parkingSpaces_id)
10:40:22,218 INFO  [stdout] (ServerService Thread Pool -- 737) Hibernate: alter table StreetData_ParkingSpaceData add constraint FKdbu6ec2cjp0lytdht5ct34r2 foreign key (parkingSpaces_id) references ParkingSpaceData
10:40:22,220 INFO  [stdout] (ServerService Thread Pool -- 737) Hibernate: alter table StreetData_ParkingSpaceData add constraint FKnp5q60wl9xmiwpc8o1in8m7si foreign key (StreetData_id) references StreetData
10:40:22,222 INFO  [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 737) HHH000230: Schema export complete

我应该更改数据库中的表格?

[编辑]

表仅在“新鲜”数据库上创建,即在部署之前创建。我可以插入一些数据(如下所示)。但是在下一次工件部署之后表不会更新。我看到带有drop的相同日志并创建操作。如果数据库不存在,则会出现相同的日志事件。

StreetData street = new StreetData();
street.setName("A");

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("parking-database-unit");
EntityManager em = entityManagerFactory.createEntityManager();

em.getTransaction().begin();
em.persist(street);
em.getTransaction().commit(); 

[编辑2]
是否可能在启动时在hibernate缓存中创建实体而不是提交到数据库?我创建了DAO服务来管理另一个模块中的数据库(使用EJB连接)。

0 个答案:

没有答案