hibernate之一对多单向关联

一对多单向关联:

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),共同交流编程经验,扫描下方二维码即可;





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值