Step-by-step learning Hibernate Framework (III): Implementing a one-to-many correlation mapping with JPA (i)

Source: Internet
Author: User

JPA (Java Persistence API) is a Java persistence specification proposed by Sun. Provides an object/relational mapping tool for Java developers to manage relational data in Java applications. The overall idea of JPA is broadly consistent with existing ORM frameworks such as Hibernate and TopLink. In general, JPA includes the following 3 areas of technology:
First: ORM Mapping meta-data
JPA supports both XML and JDK annotations (also translated as annotations) in the form of metadata that describes the mapping between objects and tables, which in turn persists entity objects into database tables.
Second: Java Persistence API
Used to manipulate entity objects, perform crud operations, and the framework will do everything for us in the background, and developers can get out of the tedious JDBC and SQL code.
Third: Query Language

This is an important aspect of persistent operations, querying data through object-oriented rather than database-oriented query language to avoid tightly coupling the SQL statements of the program.

Now use the first technique to implement a one-to-many mapping that corresponds to MySQL. One-to-many processing is handled in two ways, the first being mapped to the third table, and the second being mapped to the primary foreign key relationship.

The jar package that needs to be referenced, in addition to the Hibernate jar package, also:

The first one: as many foreign keys as the primary key

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 @generatedvaluepublic 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")//The Associated foreign key field of the Pojo that points to a lot of that party 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 @generatedvaluepublic 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")//The Associated foreign key field of the Pojo that points to a lot of that party 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 ">
EXPORTDB:

Package Com.tgb.zhudan;import Org.hibernate.cfg.annotationconfiguration;import org.hibernate.cfg.Configuration; Import org.hibernate.tool.hbm2ddl.schemaexport;/** * Generate DDL for HBM * @author Administrator * */public class Exportdb {public S tatic void Main (string[] args) {//default read hibernate.cfg.xml file configuration cfg = new Annotationconfiguration (). Configure () ; Schemaexport export = new Schemaexport (CFG); Export.create (true, True);}}
The resulting database table is as follows:
T_group table:

T_user table:



Summary: From the two data tables above you can see the ID of one end (T_group) as the foreign key exists in the multi-terminal (T_user), generating a field called group_id as a side (T_group) in the multi-terminal (T_user ) of the foreign key.

If I want this one-to-many relationship to be reflected in the database as a third table rather than a foreign key relationship, how do I write JPA annotations? We'll talk about the next blog post.

Step-by-step learning Hibernate Framework (III): Implementing a one-to-many correlation mapping with JPA (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.