Hibernate初學者---一對多 /多對一 單向關聯
阿新 • • 發佈:2019-02-12
Hibernate 版本: v5.2.10
Eclipse版本:v4.7.1
JDK版本: v1.8
MySQL版本:v5.7
一對多
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.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "100_group")
public class Groups {
int id;
String name;
Set<User> user = new HashSet<User>();
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int getId() {
return id;
}
public String getName() {
return name;
}
@OneToMany
@JoinColumn(name="Group_id")
public Set<User> getUser() {
return user;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this .name = name;
}
public void setUser(Set<User> user) {
this.user = user;
}
}
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.Table;
@Entity
@Table(name = "100_user")
public class User {
int id;
String name;
@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;
}
}
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">shwythnn00</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="com.bak.bum.union.Groups" />
<mapping class="com.bak.bum.union.User" />
</session-factory>
</hibernate-configuration>
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);
}
}
輸出結果
SHOW資料庫
JAR
多對一
- 與一對多基本一樣,只是新增對方引用的位置不一樣,換成了另一方,@One to Many換成@Many To one
- 多對一時,不用寫JoinColumn,( 一對多時,由於有
<Set>
的存在,所以Hibernate會把它當成多對多的情況處理,生成第三張中間表,所以需要寫JoinColumn 去告訴hibernate 我是有外來鍵的,有外來鍵當然就不是多對多了,所以它就不會生成第三張表了。多對一它能正確識別,所以不用寫JoinColumn)
POJO類一
package com.bak.bum.union;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "200_user")
public class User {
int id;
String name;
Groups groups;
@ManyToOne
public Groups getGroups() {
return groups;
}
public void setGroups(Groups groups) {
this.groups = groups;
}
@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;
}
}
POJO類二
package com.bak.bum.union;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "200_group")
public class Groups {
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;
}
}
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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">shwythnn00</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name=""></property>
<mapping class="com.bak.bum.union.Groups" />
<mapping class="com.bak.bum.union.User" />
</session-factory>
</hibernate-configuration>
輸出結果
SHOW資料庫
JAR