persistent.xml hibernate 利用sql script 自定义生成 table 表

Posted Bigben

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了persistent.xml hibernate 利用sql script 自定义生成 table 表相关的知识,希望对你有一定的参考价值。

<?xml version="1.0" encoding="UTF-8"?>
<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="primary">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>

        <properties>
            <!-- <property name="javax.persistence.schema-generation.database.action" 
                value="drop-and-create"/> -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.mysqlDialect" />


            <property name="javax.persistence.schema-generation.database.action"
                value="drop-and-create" />

            <property name="javax.persistence.schema-generation.create-source"
                value="script" />
            <property name="javax.persistence.schema-generation.create-script-source"
                value="file:///e:/create.sql" />
            <property name="javax.persistence.schema-generation.drop-source"
                value="script" />
            <property name="javax.persistence.schema-generation.drop-script-source"
                value="file:///e:/drop.sql" />

            <!-- <property name="javax.persistence.sql-load-script-source" value="META-INF/data.sql"/> -->




            <!-- 
            <property name="javax.persistence.schema-generation.scripts.action" 
                value="drop-and-create"/> 
            <property name="javax.persistence.schema-generation.scripts.create-target" 
                value="./create.sql"/> 
            <property name="javax.persistence.schema-generation.scripts.drop-target" 
                value="./drop.sql"/> -->



        </properties>
    </persistence-unit>
</persistence>

 注意:

sql语句创建的表要和实体bean对应。否则会出错。

create table Seat (id bigint not null auto_increment, booked bit not null, seat_id bigint, zhibin varchar(25),  primary key (id))
create table seat_type (id bigint not null auto_increment, description varchar(25) not null, position varchar(255), price integer not null, quantity integer not null, primary key (id))
alter table Seat add constraint FK9pohag9bkh1suwck1bxtr7cys foreign key (seat_id) references seat_type (id)

sql文件可以放在meta-inf里面:

<property name="javax.persistence.sql-load-script-source" value="META-INF/data.sql"/>

真实路径要在:src/main/resources/META-INF/create.sql

./drop.sql是widlfly从哪里运行的当前目录。

以上是关于persistent.xml hibernate 利用sql script 自定义生成 table 表的主要内容,如果未能解决你的问题,请参考以下文章

如何修复'无法检索unitName null的EntityManagerFactory'

Hibernate→ 《Hibernate程序开发》教材大纲

hibernate问题

Spring和Hibernate的注解整合 hibernate3和hibernate4/5的区别

hibernate.merge()方法怎么用

hibernate 异常 怎么解决