博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hibernate--一对多单向关联 (重点!!!)
阅读量:7245 次
发布时间:2019-06-29

本文共 3226 字,大约阅读时间需要 10 分钟。

多对一是在多的类上存在一的对象

一对多是在一的类上存在多的集合.

多的类

user.java:

package com.bjsxt.hibernate;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToOne;import javax.persistence.Table;@Entity@Table(name="t_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;	}}

一的类Group.java, 需要多的集合:

package com.bjsxt.hibernate;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.Table;@Entity@Table(name="t_group")public class Group {	private int id;	private String name;	private Set
users = new HashSet
(); @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; } @OneToMany @JoinColumn(name="groupId") public Set
getUsers() { return users; } public void setUsers(Set
users) { this.users = users; }}

test文件:

package com.bjsxt.hibernate;import java.util.Date;import org.hibernate.Query;import org.hibernate.Session;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 HibernateORMappingTest {	private static SessionFactory sessionFactory;		//@BeforeClass	public static void beforeClass() {			sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();	}	//@AfterClass	public static void afterClass() {		sessionFactory.close();	}				@Test	public void testSchemaExport() {		new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);	}		public static void main(String[] args) {		beforeClass();	}}

  

 

运行test的结果:

14:15:34,169 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 -     create table t_group (        id integer not null auto_increment,        name varchar(255),        primary key (id)    )14:15:34,304 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 -     create table t_user (        id integer not null auto_increment,        name varchar(255),        groupId integer,        primary key (id)    )14:15:34,409 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 -     alter table t_user         add index FKCB63CCB6C3D18669 (groupId),         add constraint FKCB63CCB6C3D18669         foreign key (groupId)         references t_group (id)14:15:34,674  INFO org.hibernate.tool.hbm2ddl.SchemaExport:196 - schema export complete

  

XML方式:

作为多的一方user正常写成:

一的一方 group需要写set:

  

 

 

转载于:https://www.cnblogs.com/wujixing/p/5421143.html

你可能感兴趣的文章
docker中启动关闭删除所有的容器命令
查看>>
python语言基础之正则表达式2,随机数
查看>>
【界面专访】李强:SAP「中国加速计划」落地生根
查看>>
【阿里云总监课第四期】时髦的云原生应用怎么写?
查看>>
白话TCP为什么需要进行三次握手
查看>>
三个开源硬件项目
查看>>
Have fun with Treasure Trails %enjoy 8% off cheap
查看>>
pdf转word如何转?最简单的方法你知道吗?
查看>>
“VR女友”制作人访谈
查看>>
阿列克谢·卡什巴斯基:有机生命渲染
查看>>
一文了解 Apache Flink 核心技术
查看>>
科略教育—管理者应具备五大能力
查看>>
mac上使用dex2jar遇到的权限问题的解决
查看>>
我的友情链接
查看>>
定位于地图小程序
查看>>
学习go语言 我的习题答案 chapter3
查看>>
vCenter Server Appliance 6.5 中重置丢失或忘记的 root 密码
查看>>
教育行业-班班通应用案例
查看>>
Linux SSH管理用户登录
查看>>
LAMP
查看>>