Hibernate®10. many2425µ¥ {}

Source: Internet
Author: User

Hibernate ö10. many2366µ¥ {}

~Úç° Î ähibernate ö5. many2one µ¥ {*********************************** ä±íà '{µramö,

 æ Ò Ó» § Ó ë Ç çé «£;¨ à À, Ú úhibernate Ç çéç° ² Ù × ÷ µä


± Ímáméé É Æ:



& Apos:

<Symbol · blank "http://www.bkjia.com/kf/ware/vc/" target = "_ blank" class = "keylink"> vcD4KPHA + PC9wPgo8YmxvY2txdW90ZT4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20141103/20141103085520417.jpg" alt = "\">


CRUD;

Student

package com.demo.model;import java.io.UnsupportedEncodingException;import java.util.Set;/**ѧÉúÐÅÏ¢ * @author wobendiankun *2014-10-19 ÏÂÎç08:54:29 */public class Student {private int studentId ;private String studentName ;private int age;private Set
  
    certificates ;private User user;public int getStudentId() {return studentId;}public void setStudentId(int studentId) {this.studentId = studentId;}public String getStudentName() {return studentName;}public void setStudentName(String studentName) {this.studentName = studentName;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {String str="";if(studentName!=null){try {str=new String(studentName.getBytes("UTF-8"));} catch (UnsupportedEncodingException e) {e.printStackTrace();}}return "Student [studentId=" + studentId + ", studentName="+ str + ", age=" + age + "]";}public Set
   
     getCertificates() {return certificates;}public void setCertificates(Set
    
      certificates) {this.certificates = certificates;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}}
    
   
  


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;public Role() {}public Role(int roleId) {this.roleId = roleId;}public Role(String roleName) {this.roleName = roleName;}private Set
  
    users;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:

Role. hbm. xml

  
  
   
    
     
      SEQ_T_ROLE
     
    
    
    
     
     
    
   
  





Set bandwidth limit Ø µätable limit:

Key keys shall vary according?

Inter-to-minus Inter-Domain inter: user quota µì Ó ³ éä± ít _ user Ú úöð ä ä± íµäø á᪠×ö¶ Î


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("¿ª·¢×é");role.setUsers(users);Session session = null;try {session = HibernateUtil.openSession();session.beginTransaction();session.save(user1);session.save(user2);session.save(role);session.getTransaction().commit();} catch (Exception e) {session.getTransaction().rollback();e.printStackTrace();} finally {HibernateUtil.closeSession(session);}}
   
  

· ¢ ³ Ösql:

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_role (role_name, role_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,2);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_3_0_, role0_.role_name as role2_3_0_ from t_role role0_ where role0_.role_id=?½ÇÉ«Ãû³Æ¿ª·¢×éHibernate: select users0_.role_id as role1_3_1_, users0_.user_id as user2_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@b4be3d, com.demo.model.User@35bb0f]


Related Article

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.