src/main/resources/META-INF/persistence.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="test">
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:p6spy:postgresql://localhost:5432/test" />
      <property name="javax.persistence.jdbc.user" value="postgres" />
      <property name="javax.persistence.jdbc.password" value="postgres" />

      <property name="javax.persistence.schema-generation.scripts.action" value="create" />
      <property name="javax.persistence.schema-generation.scripts.create-target" value="ddl.sql" />
    </properties>
  </persistence-unit>
</persistence>
package org.example.demo.hibernate;

import java.sql.Blob;
import java.util.UUID;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.Lob;

import org.hibernate.annotations.LazyGroup;

@Entity(name = "Customer")
public class Customer {
    @Id
    private Integer id;

    private String name;

    @Basic(fetch = FetchType.LAZY)
    private UUID accountsPayableXrefId;

    @Lob
    @Basic(fetch = FetchType.LAZY)
    @LazyGroup("lobs")
    private Blob image;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public UUID getAccountsPayableXrefId() {
        return accountsPayableXrefId;
    }

    public void setAccountsPayableXrefId(UUID accountsPayableXrefId) {
        this.accountsPayableXrefId = accountsPayableXrefId;
    }

    public Blob getImage() {
        return image;
    }

    public void setImage(Blob image) {
        this.image = image;
    }
}
package org.example.demo.hibernate;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity(name = "Person")
public class Person {
    @Id
    private Long id;

    private String name;

    @OneToMany(mappedBy = "author")
    private List<Book> books = new ArrayList<>();

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Book> getBooks() {
        return books;
    }
}
package org.example.demo.hibernate;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

import org.hibernate.annotations.NaturalId;

@Entity(name = "Book")
public class Book {
    @Id
    private Long id;

    private String title;

    @NaturalId
    private String isbn;

    @ManyToOne
    private Person author;

    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 Person getAuthor() {
        return author;
    }

    public void setAuthor(Person author) {
        this.author = author;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }
}
package org.example.demo.hibernate;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Test {
    public static void main(String[] args) {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
        factory.close();
    }
}

生成的 ddl.sql

create table Book (id int8 not null, isbn varchar(255), title varchar(255), author_id int8, primary key (id))
create table Customer (id int4 not null, accountsPayableXrefId uuid, image oid, name varchar(255), primary key (id))
create table Person (id int8 not null, name varchar(255), primary key (id))
alter table Book add constraint UK_u31e1frmjp9mxf8k8tmp990i unique (isbn)
alter table Book add constraint FKrxrgiajod1le3gii8whx2doie foreign key (author_id) references Person

Importing script files

以下不变

  • Customer
  • Person
  • Book
  • Test

更改

  • src/main/resources/META-INF/persistence.xml

增加

  • src/main/resources/schema-generation.sql

src/main/resources/META-INF/persistence.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="test">
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:p6spy:postgresql://localhost:5432/test" />
      <property name="javax.persistence.jdbc.user" value="postgres" />
      <property name="javax.persistence.jdbc.password" value="postgres" />

      <property name="hibernate.hbm2ddl.auto" value="create" />
      <property name="hibernate.hbm2ddl.import_files" value="schema-generation.sql" />
    </properties>
  </persistence-unit>
</persistence>

src/main/resources/schema-generation.sql

create sequence book_sequence start with 100 increment by 1

注意 schema-generation.sql 的路径要在 classpath 下!

运行 Test 将依次执行

alter table Book drop constraint FKrxrgiajod1le3gii8whx2doie
drop table if exists Book cascade
drop table if exists Customer cascade
drop table if exists Person cascade

create table Book (id int8 not null, isbn varchar(255), title varchar(255), author_id int8, primary key (id))
create table Customer (id int4 not null, accountsPayableXrefId uuid, image oid, name varchar(255), primary key (id))
create table Person (id int8 not null, name varchar(255), primary key (id))
alter table Book add constraint UK_u31e1frmjp9mxf8k8tmp990i unique (isbn)
alter table Book add constraint FKrxrgiajod1le3gii8whx2doie foreign key (author_id) references Person
create sequence book_sequence start with 100 increment by 1

results matching ""

    No results matching ""