Hibernate逍遥游记-第3章对象-关系映射基础-access="field"、dynamic-insert、dynamic-update、formula、update=false
1.
1 package mypack; 2 3 import java.util.*; 4 5 public class Monkey{ 6 7 private Long id; 8 private String firstname; 9 private String lastname; 10 private char gender; 11 private int age; 12 private int avgAge; 13 private String description; 14 15 public Monkey() {} 16 17 public Monkey(String firstname,String lastname,char gender,int age,String description){ 18 this.firstname=firstname; 19 this.lastname=lastname; 20 this.gender=gender; 21 this.age=age; 22 this.description=description; 23 } 24 25 public Long getId() { 26 return this.id; 27 } 28 29 private void setId(Long id) { 30 this.id = id; 31 } 32 33 public String getFirstname(){ 34 return firstname; 35 } 36 37 public void setFirstname(String firstname){ 38 this.firstname=firstname; 39 } 40 41 public String getLastname(){ 42 return lastname; 43 } 44 45 public void setLastname(String lastname){ 46 this.lastname = lastname; 47 } 48 49 public String getName(){ 50 return firstname+ " "+lastname; 51 } 52 53 public void setName(String name){ 54 StringTokenizer t=new StringTokenizer(name); 55 firstname=t.nextToken(); 56 lastname=t.nextToken(); 57 } 58 59 public int getAge(){ 60 return this.age; 61 } 62 63 public void setAge( int age ){ 64 this.age = age; 65 } 66 67 public int getAvgAge(){ 68 return this.avgAge; 69 } 70 71 private void setAvgAge( int avgAge){ 72 this.avgAge = avgAge; 73 } 74 75 public char getGender(){ 76 return this.gender; 77 } 78 79 public void setGender(char gender){ 80 if(gender!='F' && gender!='M'){ 81 throw new IllegalArgumentException("Invalid Gender"); 82 } 83 this.gender =gender ; 84 } 85 86 public String getDescription(){ 87 return this.description; 88 } 89 90 public void setDescription(String description){ 91 this.description=description; 92 } 93 }
2.
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping> 7 8 <class name="mypack.Monkey" table="MONKEYS" dynamic-insert="true" dynamic-update="true" > 9 <id name="id"> 10 <generator class="increment"/> 11 </id> 12 13 <property name="name" column="NAME" /> 14 <property name="gender" column="GENDER" access="field" /> 15 <property name="age" column="AGE" /> 16 17 <property name="avgAge" 18 formula="(select avg(m.AGE) from MONKEYS m)" /> 19 20 <property name="description" type="text" column="`MONKEY DESCRIPTION`"/> 21 22 </class> 23 </hibernate-mapping>
3.
1 package mypack; 2 3 import org.hibernate.*; 4 import org.hibernate.cfg.Configuration; 5 import java.util.*; 6 7 public class BusinessService{ 8 public static SessionFactory sessionFactory; 9 static{ 10 try{ 11 Configuration config = new Configuration() 12 .configure(); //加载hibernate.cfg.xml文件中配置的信息 13 sessionFactory = config.buildSessionFactory(); 14 }catch(RuntimeException e){e.printStackTrace();throw e;} 15 } 16 17 public Monkey loadMonkey(long monkey_id){ 18 Session session = sessionFactory.openSession(); 19 Transaction tx = null; 20 try { 21 tx = session.beginTransaction(); 22 Monkey monkey=(Monkey)session.get(Monkey.class,new Long(monkey_id)); 23 tx.commit(); 24 return monkey; 25 }catch (RuntimeException e) { 26 if (tx != null) { 27 tx.rollback(); 28 } 29 throw e; 30 } finally { 31 session.close(); 32 } 33 } 34 35 public void saveMonkey(Monkey monkey){ 36 Session session = sessionFactory.openSession(); 37 Transaction tx = null; 38 try { 39 tx = session.beginTransaction(); 40 session.save(monkey); 41 tx.commit(); 42 43 }catch (RuntimeException e) { 44 if (tx != null) { 45 tx.rollback(); 46 } 47 throw e; 48 } finally { 49 session.close(); 50 } 51 } 52 53 public void loadAndUpdateMonkey(long monkeyId) { 54 Session session = sessionFactory.openSession(); 55 Transaction tx = null; 56 try { 57 tx = session.beginTransaction(); 58 Monkey monkey=(Monkey)session.get(Monkey.class,new Long(monkeyId)); 59 monkey.setDescription("勇敢无畏!"); 60 tx.commit(); 61 62 }catch (RuntimeException e) { 63 if (tx != null) { 64 tx.rollback(); 65 } 66 throw e; 67 } finally { 68 session.close(); 69 } 70 } 71 72 public void updateMonkey(Monkey monkey){ 73 Session session = sessionFactory.openSession(); 74 Transaction tx = null; 75 try { 76 tx = session.beginTransaction(); 77 session.update(monkey); 78 tx.commit(); 79 80 }catch (RuntimeException e) { 81 if (tx != null) { 82 tx.rollback(); 83 } 84 throw e; 85 } finally { 86 session.close(); 87 } 88 } 89 90 91 public void printMonkey(Monkey monkey){ 92 System.out.println("name:"+monkey.getName()); 93 System.out.println("gender:"+monkey.getGender()); 94 System.out.println("description:"+monkey.getDescription()); 95 System.out.println("age:"+monkey.getAge()); 96 System.out.println("avgAge:"+monkey.getAvgAge()); 97 } 98 99 public void test(){ 100 Monkey monkey=new Monkey("悟空","孙",'M',500,"神通广大!"); 101 saveMonkey(monkey); 102 103 monkey=loadMonkey(1); 104 printMonkey(monkey); 105 106 monkey.setDescription("齐天大圣!"); 107 updateMonkey(monkey); 108 printMonkey(monkey); 109 110 loadAndUpdateMonkey(1); 111 printMonkey(monkey); 112 } 113 114 public static void main(String args[]) { 115 new BusinessService().test(); 116 sessionFactory.close(); 117 } 118 }
4.
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE hibernate-configuration 3 PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="dialect"> 8 org.hibernate.dialect.MySQLDialect 9 </property> 10 <property name="connection.driver_class"> 11 com.mysql.jdbc.Driver 12 </property> 13 <property name="connection.url"> 14 jdbc:mysql://localhost:3306/sampledb 15 </property> 16 <property name="connection.username"> 17 root 18 </property> 19 <property name="connection.password"> 20 1234 21 </property> 22 <property name="show_sql">true</property> 23 <mapping resource="mypack/Monkey.hbm.xml" /> 24 </session-factory> 25 </hibernate-configuration>
5.
1 drop database if exists SAMPLEDB; 2 create database SAMPLEDB; 3 use SAMPLEDB; 4 5 create table MONKEYS ( 6 ID bigint not null, 7 NAME varchar(15), 8 GENDER char(1), 9 AGE int, 10 `MONKEY DESCRIPTION` text, 11 primary key (id) 12 );
You can do anything you set your mind to, man!