1.
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 <hibernate-mapping > 6 7 <class name="mypack.Monkey" table="MONKEYS"> 8 9 <id name="id" type="long" column="ID"> 10 <generator class="increment"/> 11 </id> 12 13 <property name="name" type="string" column="NAME" /> 14 15 <!-- 16 <many-to-one 17 name="team" 18 column="TEAM_ID" 19 class="mypack.Team" 20 lazy="false" 21 /> 22 --> 23 24 <!-- mapping with cascade --> 25 <!----> 26 <many-to-one 27 name="team" 28 column="TEAM_ID" 29 class="mypack.Team" 30 cascade="save-update" 31 lazy="false" 32 /> 33 34 35 </class> 36 37 </hibernate-mapping>
2.
1 package mypack; 2 public class Monkey{ 3 private long id; 4 private String name; 5 private Team team; 6 7 public Monkey() {} 8 public Monkey(String name, Team team) { 9 this.name = name; 10 this.team = team; 11 } 12 13 public long getId() { 14 return this.id; 15 } 16 17 public void setId(long id) { 18 this.id = id; 19 } 20 public String getName() { 21 return this.name; 22 } 23 24 public void setName(String name) { 25 this.name = name; 26 } 27 public Team getTeam() { 28 return this.team; 29 } 30 31 public void setTeam(Team team) { 32 this.team = team; 33 } 34 35 36 37 38 }
3.
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 <hibernate-mapping > 6 7 <class name="mypack.Team" table="TEAMS" > 8 <id name="id" type="long" column="ID"> 9 <generator class="increment"/> 10 </id> 11 12 <property name="name" type="string" column="NAME" /> 13 14 </class> 15 16 </hibernate-mapping>
4.
1 package mypack; 2 public class Team { 3 private long id; 4 private String name; 5 6 public Team() { 7 } 8 9 public Team(String name) { 10 this.name = name; 11 } 12 13 public long getId() { 14 return this.id; 15 } 16 17 public void setId(long id) { 18 this.id = id; 19 } 20 public String getName() { 21 return this.name; 22 } 23 24 public void setName(String name) { 25 this.name = name; 26 } 27 28 29 30 31 }
5.
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 // Create a configuration based on the properties file we‘ve put 12 Configuration config = new Configuration(); 13 config.configure(); 14 sessionFactory = config.buildSessionFactory(); 15 }catch(RuntimeException e){e.printStackTrace();throw e;} 16 } 17 18 public List findMonkeysByTeam(Team team){ 19 Session session = sessionFactory.openSession(); 20 Transaction tx = null; 21 try { 22 tx = session.beginTransaction(); 23 24 List monkeys=session.createQuery("from Monkey as m where m.team.id="+team.getId()) 25 .list(); 26 tx.commit(); 27 return monkeys; 28 }catch (RuntimeException e) { 29 if (tx != null) { 30 tx.rollback(); 31 } 32 throw e; 33 } finally { 34 session.close(); 35 } 36 } 37 38 public Team findTeam(long team_id){ 39 Session session = sessionFactory.openSession(); 40 Transaction tx = null; 41 try { 42 tx = session.beginTransaction(); 43 Team team=(Team)session.get(Team.class,new Long(team_id)); 44 tx.commit(); 45 return team; 46 }catch (RuntimeException e) { 47 if (tx != null) { 48 tx.rollback(); 49 } 50 throw e; 51 } finally { 52 session.close(); 53 } 54 } 55 56 public void saveTeamAndMonkeyWithCascade(){ 57 Session session = sessionFactory.openSession(); 58 Transaction tx = null; 59 try { 60 tx = session.beginTransaction(); 61 62 Team team=new Team("BULL"); 63 Monkey monkey1=new Monkey("Tom",team); 64 Monkey monkey2=new Monkey("Mike",team); 65 66 session.save(monkey1); 67 session.save(monkey2); 68 69 tx.commit(); 70 71 }catch (RuntimeException e) { 72 if (tx != null) { 73 tx.rollback(); 74 } 75 e.printStackTrace(); 76 } finally { 77 session.close(); 78 } 79 } 80 81 public void saveTeamAndMonkey(){ 82 Session session = sessionFactory.openSession(); 83 Transaction tx = null; 84 try { 85 tx = session.beginTransaction(); 86 87 Team team=new Team("DREAM"); 88 session.save(team); 89 90 Monkey monkey1=new Monkey("Jack",team); 91 Monkey monkey2=new Monkey("Bill",team); 92 session.save(monkey1); 93 session.save(monkey2); 94 tx.commit(); 95 96 }catch (RuntimeException e) { 97 if (tx != null) { 98 tx.rollback(); 99 } 100 throw e; 101 } finally { 102 session.close(); 103 } 104 } 105 106 public void printMonkeys(List monkeys){ 107 for (Iterator it = monkeys.iterator(); it.hasNext();) { 108 Monkey monkey=(Monkey)it.next(); 109 System.out.println("Monkeys in "+monkey.getTeam().getName()+ " :"+monkey.getName()); 110 } 111 } 112 113 public void test(){ 114 saveTeamAndMonkey(); 115 saveTeamAndMonkeyWithCascade(); 116 Team team=findTeam(1); 117 List monkeys=findMonkeysByTeam(team); 118 printMonkeys(monkeys); 119 } 120 121 public static void main(String args[]){ 122 new BusinessService().test(); 123 sessionFactory.close(); 124 } 125 }
6.
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/Team.hbm.xml" /> 24 <mapping resource="mypack/Monkey.hbm.xml" /> 25 </session-factory> 26 </hibernate-configuration>
7.
1 drop database if exists SAMPLEDB; 2 create database SAMPLEDB; 3 use SAMPLEDB; 4 5 create table TEAMS ( 6 ID bigint not null, 7 NAME varchar(15), 8 primary key (ID)); 9 10 create table MONKEYS ( 11 ID bigint not null, 12 NAME varchar(15), 13 TEAM_ID bigint, 14 primary key (ID)); 15 16 alter table MONKEYS add index IDX_TEAM(TEAM_ID), 17 add constraint FK_TEAM foreign key (TEAM_ID) references TEAMS (ID);
8.
1 <?xml version="1.0"?> 2 <project name="Learning Hibernate" default="prepare" basedir="."> 3 4 <!-- Set up properties containing important project directories --> 5 <property name="source.root" value="5.1/src"/> 6 <property name="class.root" value="5.1/classes"/> 7 <property name="lib.dir" value="lib"/> 8 <property name="schema.dir" value="5.1/schema"/> 9 10 <!-- Set up the class path for compilation and execution --> 11 <path id="project.class.path"> 12 <!-- Include our own classes, of course --> 13 <pathelement location="${class.root}" /> 14 <!-- Include jars in the project library directory --> 15 <fileset dir="${lib.dir}"> 16 <include name="*.jar"/> 17 </fileset> 18 </path> 19 20 <!-- Create our runtime subdirectories and copy resources into them --> 21 <target name="prepare" description="Sets up build structures"> 22 <delete dir="${class.root}"/> 23 <mkdir dir="${class.root}"/> 24 25 <!-- Copy our property files and O/R mappings for use at runtime --> 26 <copy todir="${class.root}" > 27 <fileset dir="${source.root}" > 28 <include name="**/*.properties"/> 29 <include name="**/*.hbm.xml"/> 30 <include name="**/*.cfg.xml"/> 31 </fileset> 32 </copy> 33 </target> 34 35 36 37 <!-- Compile the java source of the project --> 38 <target name="compile" depends="prepare" 39 description="Compiles all Java classes"> 40 <javac srcdir="${source.root}" 41 destdir="${class.root}" 42 debug="on" 43 optimize="off" 44 deprecation="on"> 45 <classpath refid="project.class.path"/> 46 </javac> 47 </target> 48 49 50 <target name="run" description="Run a Hibernate sample" 51 depends="compile"> 52 <java classname="mypack.BusinessService" fork="true"> 53 <classpath refid="project.class.path"/> 54 </java> 55 </target> 56 57 </project>
时间: 2024-10-10 09:33:43