Hibernate初学者---5.2.10版本_SchemaExpor 自动生成数据表

Hibernate 版本: v5.2.10
Eclipse版本:v4.7.1
JDK版本: v1.8
MySQL版本:v5.7

网上Hibernate资料少得可怜,又不能半途而废,真心想好好学一下Hibernate. 直接上源码。

这是一个外键关联的案例,所以有两个POJO类。

第一个POJO类

package com.bai.fan;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class One2One_A {
    int id;
    String name;
    One2One_B w2;
    @Id
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }
@OneToOne
    public One2One_B getW2() {
        return w2;
    }

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



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

    public void setW2(One2One_B w2) {
        this.w2 = w2;
    }


}


第二个POJO类

package com.bai.fan;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class One2One_B {
    int id;
    String name;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

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

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

}

hibernate.cfg.xml

  



<hibernate-configuration>
    <session-factory>

        <property name="connection.driver_class">com.mysql.jdbc.Driverproperty>

        <property name="connection.url">jdbc:mysql://localhost/hibernateproperty>

        <property name="connection.username">rootproperty>

        <property name="connection.password">shwythnn00property>

        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialectproperty>

        <property name="show_sql">trueproperty>

        <property name="hibernate.format_sql">trueproperty>

        <property name="hibernate.hbm2ddl.auto">updateproperty>

        <property name="">property>

        
        <mapping class="com.bai.fan.One2One_A" />
        <mapping class="com.bai.fan.One2One_B" />
            // 这里要特别注意,两个POJO类的 Mapping 都要配置。
    session-factory>
hibernate-configuration>  

Junit TEST类

package com.bak.bum;

import java.util.EnumSet;

import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.junit.Test;

public class MyTest {

    @Test
    public void test() {
        ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().configure().build();   
        Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();  
        SchemaExport schemaExport = new SchemaExport();
        schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata); 
    }

}

运行结果

由于是外键关联,Hibernate 做了三次操作:create两次,alter一次
Hibernate初学者---5.2.10版本_SchemaExpor 自动生成数据表_第1张图片


MySQL确认数据表;
Hibernate初学者---5.2.10版本_SchemaExpor 自动生成数据表_第2张图片

你可能感兴趣的:(hibernate)