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 ">