Hibernate Learning (IV.) multi-pair one-way association mapping

Source: Internet
Author: User

Hibernate for the operation of the database, all the use of object-oriented thinking to understand and implement. The general map of the individual table, I believe that everyone has no problem, but for some of the special relationship between tables, Hibernate provides some unique way to simplify it.

Today we'll talk about the correlation mapping for Many-to-many.

There are many pairs of relationships in the database, which hibernate the relationship between the object and the multiple pairs. For example, users and user groups, a user belongs to only one group, a group has more than one user. We can say that the relationship between the user and the user group is a many-to-many relationship. Use the object's UML diagram to represent:

How to achieve it in hibernate? First, define the two entity classes:

Package com.bjpowernode.hibernate;  
    /** * User Group * @author Longxuan * */public class Group {private int id;  
    private String name;  
    public int getId () {return id;  
    The public void setId (int id) {this.id = ID;  
    Public String GetName () {return name;  
    public void SetName (String name) {this.name = name;  
      
}} package com.bjpowernode.hibernate;  
          
    /** * User class * @author Longxuan * */public class User {private int id;  
      
    private String name;  
          
    Private group Group;  
    Public Group Getgroup () {return group;  
    public void Setgroup (group group) {this.group = group;  
    public int getId () {return id;  
    The public void setId (int id) {this.id = ID; } public String GetnamE () {return name;  
    public void SetName (String name) {this.name = name; }  
      
}

Hibernate.cfg.xml configuration file:

<! DOCTYPE hibernate-configuration public  
    "-//hibernate/hibernate configuration DTD 3.0//en"  
    "http:// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">  
      

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.