Hibernate初学者---schemaExport自动生成联合主键关联数据表

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

OPJO类一,

package com.bak.bum.union;

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

@Entity
@IdClass(CoursePK.class)
public class Course {
    private String account;

    private int id;

    private String name;

    public String getAccount() {
        return account;
    }

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public int getId() {
        return id;
    }

    @Id
    public String getName() {
        return name;
    }

    public void setAccount(String account) {
        this.account = account;
    }

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

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

}

联合主键类

package com.bak.bum.union;

public class CoursePK {
    int id;

    String name;

    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;
    }
}

POJO类二:

package com.bak.bum.union;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.OneToOne;
@Entity
public class Timetables {
    Course course;

    int id;

    String name;

    String string;

    @OneToOne
    @JoinColumns({@JoinColumn(name="CourseID",referencedColumnName="id"),@JoinColumn(name="CourseNAME",referencedColumnName="name")})
    public Course getCourse() {
        return course;
    }

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getString() {
        return string;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

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

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

    public void setString(String string) {
        this.string = string;
    }

}

Junit – TEST类

package com.bak.fan.test;

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;

public class Test {

    @org.junit.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);


}
}

LOG输出

Hibernate初学者---schemaExport自动生成联合主键关联数据表_第1张图片

SHOW数据库表

Hibernate初学者---schemaExport自动生成联合主键关联数据表_第2张图片

你可能感兴趣的:(hibernate,hibernate)