一對多單向關聯:
Annotation版本:
Group類:
package com.hibernate.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="_Group")
public class Group {
private int id;
private String name;
private Set<User> users = new HashSet<User>();
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
@OneToMany
@JoinColumn(name="groupId")
//必須寫上joincolumn,否則會出現第三張表
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public void setName(String name) {
this.name = name;
}
}
User類:
package com.hibernate.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="_User")
public class User {
private int id;
private String name;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
測試用例:
package com.hibernate.model;
import static org.junit.Assert.*;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class ORMappingTest {
public static SessionFactory sf = null;
//@BeforeClass
public static void beforeClass(){
try{
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
catch(Exception e) {
e.printStackTrace();
}
finally{
}
}
@Test
public void testSchemaExport(){
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
}
//@AfterClass
public static void afterClass(){
sf.close();
}
}
結果:
19:47:12,094 DEBUG SchemaExport:377 -
create table _Group (
id integer not null auto_increment,
name varchar(255),
primary key (id)
)
19:47:12,119 DEBUG SchemaExport:377 -
create table _User (
id integer not null auto_increment,
name varchar(255),
groupId integer,
primary key (id)
)
19:47:12,176 DEBUG SchemaExport:377 -
alter table _User
add index FK56318CA346472EB (groupId),
add constraint FK56318CA346472EB
foreign key (groupId)
references _Group (id)
19:47:12,270 INFO SchemaExport:268 - schema export complete
生成了兩張表,如果不加joinColumn,會生成第三張中間表,因爲默認把一對多當成多對多的一種特例的。
xml版本:
Group類和User類不變。
Group.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.model">
<class name="Group" table="_Group">
<id name="id">
<generator class="native"/>
</id>
<property name="name"></property>
<set name="users">
<key column="groupId"></key>
<one-to-many class="com.hibernate.model.User"/>
</set>
</class>
</hibernate-mapping>
User.hbm.xml:<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.model">
<class name="User" table="_User">
<id name="id">
<generator class="native"/>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>
尊重版權,轉載請註明本文鏈接
歡迎關注行者摩羅微信公衆號(xingzhemoluo),共同交流編程經驗,掃描下方二維碼即可;