这是持久化操作中很重要的一个方面,通过面向对象而非面向数据库的查询语言查询数据,避免程序的SQL语句紧密耦合。
现在就用第一种技术来实现一对多的映射,对应到mysql中。一对多的处理有两种处理方式,第一种是映射成第三张表,第二种是映射成主外键关系。
需要引用的jar包除了hibernate的jar包,还有:
第一种:以一的主键作为多的外键
Group.java
package com.tgb.zhudan; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name="t_group") public class Group { private int id; private String name; private List<User> user; @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*/ @OneToMany(cascade = CascadeType.ALL, mappedBy = "id")//指向多的那方的pojo的关联外键字段 public List<User> getUser() { return user; } public void setUser(List<User> user) { this.user = user; } }
User.java
package com.tgb.zhudan; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name="t_group") public class Group { private int id; private String name; private List<User> user; @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*/ @OneToMany(cascade = CascadeType.ALL, mappedBy = "id")//指向多的那方的pojo的关联外键字段 public List<User> getUser() { return user; } public void setUser(List<User> user) { this.user = user; } }hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_jpa_one2many</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <mapping class="com.tgb.zhudan.Group"/> <mapping class="com.tgb.zhudan.User"/> </session-factory> </hibernate-configuration>ExportDB:
package com.tgb.zhudan; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; /** * 将hbm生成ddl * @author Administrator * */ public class ExportDB { public static void main(String[] args) { //默认读取hibernate.cfg.xml文件 Configuration cfg = new AnnotationConfiguration().configure(); SchemaExport export = new SchemaExport(cfg); export.create(true, true); } }生成的数据库表如下:
t_user表:
总结:从上面的两张数据表可以看到一端(t_group)的id作为了外键存在了多端(t_user),生成一个叫group_id的字段作为一端(t_group)的在多端(t_user)的外键。
如果我想让这种一对多关系的实体在数据库中的体现是第三种表而不是外键关系,那么我又应该怎么写jpa注解呢?下篇博客我们再说。
一步步学习Hibernate框架(三):采用jpa实现一对多关联映射(一)
原文地址:http://blog.csdn.net/dandanzmc/article/details/43985965