Hibernate Learning (v.) One-to-many one-way correlation mapping

Source: Internet
Author: User

In the previous blog, "A Mouthful of eating hibernate (d)-multi-One one-way association mapping", introduced a Many-to-many association mapping, today on the contrary to a One-to-many one-way association mapping.

There may be some confusion about the topic of these 2 blogs, which is a little different from the daily story. We talk about such as father-son relationship, the relationship between husband and wife is a two-way relationship, and now the discussion is one-way relationship, so there are more than one and a pair of many of the argument.

The relationship between the two is actually very simple, but the angle is different. For example, the relationship between students and classes. If you look at it from a student's point of view, it's a many-to-many relationship. From the class point of view, it is a one-to-many relationship. The argument is simple, but the object and the relationship are built differently.

Let's take a look at a couple of class diagrams (seemingly many people are wrong about the aggregation):

Hibernate for a one-to-many relationship is handled by manipulating the class end, indirectly, or automatically manipulating the student end. For example, add, I directly add the class end of the data, multiple student will be added automatically added. You can also use this class to get all the corresponding student information. Follow me to configure it:

First define entity class "Class" "Student"

Package com.bjpowernode.hibernate;  
      
Import Java.util.Set;  
          
    /** * Class * @author Longxuan * */public class class {private int id;  
          
    private String name;  
      
    Private set<student> students;  
    Public set<student> getstudents () {return students;  
    public void Setstudents (set<student> students) {this.students = students;  
    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; /** * Student Class *url:http://www.bianceng.cn/programming/java/201410/45831.htm * @author Longxuan * */public class Stud  
          
    ENT {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; }  
      
          
}

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.