一对多单向关联:
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),共同交流编程经验,扫描下方二维码即可;