Hibernate初学者---多对一/一对多 双向关联

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

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.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "300_user")
public class Users {
    int id;
    String name;
    Groups groups;

    @ManyToOne
    public Groups getGroups() {
        return groups;
    }

    public void setGroups(Groups groups) {
        this.groups = groups;
    }

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    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 java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "300_group")
public class Groups {
    int id;
    String name;
    Set users = new HashSet();

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

    public String getName() {
        return name;
    }

    @OneToMany(mappedBy="groups")
    public Set getUsers() {
        return users;
    }

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

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

    public void setUser(Set users) {
        this.users = users;
    }

}

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


}
}

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">createproperty>

        <property name="">property>


        <mapping class="com.bak.bum.union.Groups" />
        <mapping class="com.bak.bum.union.Users" />
    session-factory>
hibernate-configuration>  

输出结果

Hibernate初学者---多对一/一对多 双向关联_第1张图片

SHOW数据库
Hibernate初学者---多对一/一对多 双向关联_第2张图片

JAR
Hibernate初学者---多对一/一对多 双向关联_第3张图片

Hibernate初学者---多对一/一对多 双向关联_第4张图片

你可能感兴趣的:(hibernate)