1.
package mypack; import java.util.*; public class Monkey{ private Long id;
private String firstname;
private String lastname;
private char gender;
private int age;
private int avgAge;
private String description; public Monkey() {} public Monkey(String firstname,String lastname,char gender,int age,String description){
this.firstname=firstname;
this.lastname=lastname;
this.gender=gender;
this.age=age;
this.description=description;
} public Long getId() {
return this.id;
} private void setId(Long id) {
this.id = id;
} public String getFirstname(){
return firstname;
} public void setFirstname(String firstname){
this.firstname=firstname;
} public String getLastname(){
return lastname;
} public void setLastname(String lastname){
this.lastname = lastname;
} public String getName(){
return firstname+ " "+lastname;
} public void setName(String name){
StringTokenizer t=new StringTokenizer(name);
firstname=t.nextToken();
lastname=t.nextToken();
} public int getAge(){
return this.age;
} public void setAge( int age ){
this.age = age;
} public int getAvgAge(){
return this.avgAge;
} private void setAvgAge( int avgAge){
this.avgAge = avgAge;
} public char getGender(){
return this.gender;
} public void setGender(char gender){
if(gender!='F' && gender!='M'){
throw new IllegalArgumentException("Invalid Gender");
}
this.gender =gender ;
} public String getDescription(){
return this.description;
} public void setDescription(String description){
this.description=description;
}
}
2.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="mypack.Monkey" table="MONKEYS" dynamic-insert="true" dynamic-update="true" >
<id name="id">
<generator class="increment"/>
</id> <property name="name" column="NAME" />
<property name="gender" column="GENDER" access="field" />
<property name="age" column="AGE" /> <property name="avgAge"
formula="(select avg(m.AGE) from MONKEYS m)" /> <property name="description" type="text" column="`MONKEY DESCRIPTION`"/> </class>
</hibernate-mapping>
3.
package mypack; import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*; public class BusinessService{
public static SessionFactory sessionFactory;
static{
try{
Configuration config = new Configuration()
.configure(); //加载hibernate.cfg.xml文件中配置的信息
sessionFactory = config.buildSessionFactory();
}catch(RuntimeException e){e.printStackTrace();throw e;}
} public Monkey loadMonkey(long monkey_id){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Monkey monkey=(Monkey)session.get(Monkey.class,new Long(monkey_id));
tx.commit();
return monkey;
}catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void saveMonkey(Monkey monkey){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(monkey);
tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void loadAndUpdateMonkey(long monkeyId) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Monkey monkey=(Monkey)session.get(Monkey.class,new Long(monkeyId));
monkey.setDescription("勇敢无畏!");
tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void updateMonkey(Monkey monkey){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.update(monkey);
tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void printMonkey(Monkey monkey){
System.out.println("name:"+monkey.getName());
System.out.println("gender:"+monkey.getGender());
System.out.println("description:"+monkey.getDescription());
System.out.println("age:"+monkey.getAge());
System.out.println("avgAge:"+monkey.getAvgAge());
} public void test(){
Monkey monkey=new Monkey("悟空","孙",'M',500,"神通广大!");
saveMonkey(monkey); monkey=loadMonkey(1);
printMonkey(monkey); monkey.setDescription("齐天大圣!");
updateMonkey(monkey);
printMonkey(monkey); loadAndUpdateMonkey(1);
printMonkey(monkey);
} public static void main(String args[]) {
new BusinessService().test();
sessionFactory.close();
}
}
4.
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/sampledb
</property>
<property name="connection.username">
root
</property>
<property name="connection.password">
1234
</property>
<property name="show_sql">true</property>
<mapping resource="mypack/Monkey.hbm.xml" />
</session-factory>
</hibernate-configuration>
5.
drop database if exists SAMPLEDB;
create database SAMPLEDB;
use SAMPLEDB; create table MONKEYS (
ID bigint not null,
NAME varchar(15),
GENDER char(1),
AGE int,
`MONKEY DESCRIPTION` text,
primary key (id)
);