Users and roles are many-to-many relationships. Then they need an intermediate table to maintain a many-to-many relationship. The intermediate table has two foreign keys that point to the primary key for the user table and the role table, respectively. There is no separate primary key, UID, and RID as the Federated primary key.
If we create it ourselves, we need to create three tables.
If hibernate is used, as long as you write 2 JavaBean and write 2 mapped profiles, the intermediate table is automatically generated. First, JavaBean and configuration 1.1 JavaBean
User.java
Package com.hib.domain;
Import Java.util.HashSet;
Import Java.util.Set;
public class User {
private Long uid;
Private String username;
private String password;
Many-to-many, writing is a collection, set to their own new oh
private set<role> roles = new hashset<> ();
Public Long Getuid () {
return uid;
}
public void SetUid (Long uid) {
this.uid = uid;
}
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
this.username = username;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
this.password = password;
}
Public set<role> GetRoles () {
return roles;
}
public void Setroles (set<role> roles) {
this.roles = roles;
}
}
Role.java
Package com.hib.domain;
Import Java.util.HashSet;
Import Java.util.Set;
public class Role {
private Long rid;
Private String rname;
Many-to-many, writing is a collection, set to their own new Oh
private set<user> users = new hashset<> ();
Public Long Getrid () {
return rid;
}
public void Setrid (Long rid) {
This.rid = rid;
}
Public String Getrname () {
return rname;
}
public void Setrname (String rname) {
this.rname = rname;
}
Public set<user> getusers () {
return users;
}
public void Setusers (set<user> users) {
this.users = users;
}
}
1.2 Configuration
User.hbm2.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE hibernate-mapping public
"-//hibernate/hibernate mapping DTD 3.0//en"
"Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">
Role.hbm2.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE hibernate-mapping public
"-//hibernate/hibernate mapping DTD 3.0//en"
"http://www.hibernate.org/d Td/hibernate-mapping-3.0.dtd ">
second, the test procedure
Package com.hib.utils;
Import org.hibernate.Session;
Import org.hibernate.SessionFactory;
Import org.hibernate.cfg.Configuration;
/**
* Hibernate Framework Tool Class * */Public
class Hibernateutils {
private static final Configuration CONFIG ;
private static final sessionfactory FACTORY;
Static code block, the class executes
static {
//Load XML config file
config = new configuration () when loading. Configure ();
Construction Factory
FACTORY = Config.buildsessionfactory ();
}
public static Session getsession () {
return factory.opensession ();
}
/**
* Business Layer open transaction, just use this
*
* @return * *
/public static Session Getcurrentsession () {
// Gets the session object from the Threadlocal class
return factory.getcurrentsession ();
}
public static void Main (string[] args) {
getsession ();
}
}
Execute the Main method
iii. Results of Operation
Iv. Preservation Procedures
@Test public
void Run1 () {
Session session = Hibernateutils.getcurrentsession ();
Transaction tx = Session.begintransaction ();
Simulate many-to-many, bidirectional associations
//create user
u1 = new users ();
U1.setusername ("Zhangsan");
User U2 = new user ();
U2.setusername ("Lisi");
Create roles role
r1 = new Role ();
R1.setrname ("manager");
Role r2 = new role ();
R2.setrname ("actor");
Associate
U1.getroles (). Add (R1);
U1.getroles (). Add (R2);
R1.getusers (). Add (U1);
R2.getusers (). Add (U1);
U2.getroles (). Add (R1);
R1.getusers (). Add (U2);
Session.save (U1);
Session.save (U2);
Session.save (R1);
Session.save (R2);
Tx.commit ();
}
v. Preservation of results
Look at sql:
Log4j:warn No Appenders could is found for logger (org.jboss.logging).
Log4j:warn Initialize the log4j system properly.
Log4j:warn See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hibernate:
insert
into
sys_user
(username, password)
values
(?,?)
Hibernate:
insert
into
sys_user
(username, password)
values
(?,?)
Hibernate:
insert
into
sys_role
(rname)
values
(?)
Hibernate:
insert
into
sys_role
(rname)
values
(?)
Hibernate:
insert
into
sys_user_role
(UID, RID)
values
(?,?)
Hibernate:
insert
into
sys_user_role
(UID, RID)
values
(?,?)
Hibernate:
insert
into
sys_user_role
(UID, RID)
values
(?,?)
SOURCE download