Hibernate®11. many2?#« commandid

Source: Internet
Author: User

Hibernate ö11. many2jw1_« zookeeper

± Íámámímímámān:




<Symbol · blank "http://www.bkjia.com/kf/ware/vc/" target = "_ blank" class = "keylink"> cipher + cjxwp1_vcd4kpgjsb2nrcxvvdgu + CjxwPjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20141104/20141104082929268.jpg" alt = "\">

CRUD:

User

package com.demo.model;import java.util.Set;/**Óû§ÐÅÏ¢ * @author wobendiankun *2014-10-29 ÏÂÎç11:05:26 */public class User {/** *Óû§Id  */private int userId;/** * Óû§Ãû */private String userName;/** * ÃÜÂë */private String password;private Student student ;private Set
  
    roles; public User() {}public User(int userId) {this.userId = userId;}public User(String userName, String password) {this.userName = userName;this.password = password;}public int getUserId() {return userId;}public void setUserId(int userId) {this.userId = userId;}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 Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}public Set
   
     getRoles() {return roles;}public void setRoles(Set
    
      roles) {this.roles = roles;}}
    
   
  

Role

package com.demo.model;import java.util.Set;/**½ÇÉ«ÐÅÏ¢ * @author wobendiankun *2014-11-2 ÏÂÎç08:29:54 */public class Role {/** * ½ÇÉ«id */private int roleId;/** * ½ÇÉ«Ãû³Æ */private String roleName;private Set
  
    users;public Role() {}public Role(int roleId) {this.roleId = roleId;}public Role(String roleName) {this.roleName = roleName;}public int getRoleId() {return roleId;}public void setRoleId(int roleId) {this.roleId = roleId;}public String getRoleName() {return roleName;}public void setRoleName(String roleName) {this.roleName = roleName;}public Set
   
     getUsers() {return users;}public void setUsers(Set
    
      users) {this.users = users;}}
    
   
  

Å äögou:

User. hbm. xml

  
  
   
    
     
      SEQ_T_USER
     
    
    
    
    
    
     
     
    
   
  

Role. hbm. xml

  
  
   
    
     
      SEQ_T_ROLE
     
    
    
    
     
     
    
   
  

Add:

@Testpublic void addTest(){User user1=new User("ÕÔÈý","111");User user2=new User("ÎÞËÄ","111");Set
  
    users=new HashSet
   
    ();users.add(user1);users.add(user2);Role role=new Role("²ÆÎñ²¿");Set
    
      roles=new HashSet
     
      ();//ÉèÖÃË«Ïò¹ØÏµrole.setUsers(users);user1.setRoles(roles);user2.setRoles(roles);Session session = null;try {session = HibernateUtil.openSession();session.beginTransaction();session.save(role);session.save(user1);session.save(user2);session.getTransaction().commit();} catch (Exception e) {session.getTransaction().rollback();e.printStackTrace();} finally {HibernateUtil.closeSession(session);}}
     
    
   
  

· ¢ ³ Ösql:

Hibernate: insert into t_role (role_name, role_id) values (?, ?)Hibernate: insert into t_user (user_name, password, student_id, user_id) values (?, ?, ?, ?)Hibernate: insert into t_user (user_name, password, student_id, user_id) values (?, ?, ?, ?)Hibernate: insert into t_user_role (role_id, user_id) values (?, ?)Hibernate: insert into t_user_role (role_id, user_id) values (?, ?)

Load;

@Testpublic void loadTest(){Session session = null;try {session = HibernateUtil.openSession();session.beginTransaction();Role role=(Role)session.load(Role.class,3);System.out.println("½ÇÉ«Ãû³Æ:"+role.getRoleName());System.out.println("users:"+role.getUsers());session.getTransaction().commit();} catch (Exception e) {session.getTransaction().rollback();e.printStackTrace();} finally {HibernateUtil.closeSession(session);}}


· ¢ ³ Ösql:

Hibernate: select role0_.role_id as role1_4_0_, role0_.role_name as role2_4_0_ from t_role role0_ where role0_.role_id=?½ÇÉ«Ãû³Æ:²ÆÎñ²¿Hibernate: select users0_.role_id as role2_4_1_, users0_.user_id as user1_1_, user1_.user_id as user1_2_0_, user1_.user_name as user2_2_0_, user1_.password as password2_0_, user1_.student_id as student4_2_0_ from t_user_role users0_ inner join t_user user1_ on users0_.user_id=user1_.user_id where users0_.role_id=?users:[com.demo.model.User@1f31652, com.demo.model.User@3e96cf]



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.