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)
);

Hibernate逍遥游记-第3章对象-关系映射基础-access="field"、dynamic-insert、dynamic-update、formula、update=false的更多相关文章

  1. Hibernate逍遥游记-第2章-使用hibernate.properties

    1. package mypack; import org.hibernate.*; import org.hibernate.cfg.Configuration; import java.util. ...

  2. Hibernate逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  3. Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  4. Hibernate逍遥游记-第10章 映射继承关系-001继承关系树中的每个具体类对应一个表

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  5. Hibernate逍遥游记-第8章 映射组成关系(<component>、<parent>)

    一. 1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  6. Hibernate逍遥游记-第6章 通过Hibernate操纵对象(select-before-update)

    1. 2. 3. 4. 5. 6. 7.

  7. Hibernate逍遥游记-第1章-JDBC访问数据库

    1. package mypack; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.sw ...

  8. Hibernate逍遥游记-第15章处理并发问题-003乐观锁

    1. 2. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; drop table if exists ...

  9. Hibernate逍遥游记-第15章处理并发问题-002悲观锁

    1. 2. hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.mys ...

随机推荐

  1. Scrapy源码学习(一)

    用Scrapy已经有一段时间了,觉得该是看一下源码的时候了.最开始用的时候还是0.16的版本,现在稳定版已经到了0.18.结合使用Scrapy的过程,先从Scrapy的命令行看起. 一.准备 下载源代 ...

  2. oracle 11g 通过透明网关链接mysql

    之前转载过一篇在Windows上安装的,自己实际在centos上安装了一下.以下为安装记录: 一.操作系统环境 二.数据库环境(用oracle用户登录) 三.DG4ODBC 在Oracle DB 11 ...

  3. [转]Linux下修改/设置环境变量JAVA_HOME

    1. 永久修改,对所有用户有效  # vi /etc/profile //按键盘[Shift + g], 在profile文件最后添加下面的内容: export JAVA_HOME = /home/m ...

  4. hdu 5738 2016 Multi-University Training Contest 2 Eureka 计数问题(组合数学+STL)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5738 题意:从n(n <= 1000)个点(有重点)中选出m(m > 1)个点(选出的点只 ...

  5. [转]system函数返回值探究

    对于system这个函数的功能早就有一定了解,读书期间,就学习了UNIX系统编程这本书,后来买了APUE.我这个人总是有好读书不求甚解的毛病.对于system函数只知其一,不知其二.后来被人问起相关的 ...

  6. 【转载】MySQL innodb_table_stats表不存在的解决方法

    MySQL 版本 5.6.14 公司有几台MySQL服务器的错误日志显示,有几个系统表不存在.innodb_table_statsinnodb_index_statsslave_master_info ...

  7. eclipse下mysql编程

    mysql -uroot -p 密码 1:如果/usr/include/mysql路径下不存在头文件请: apt-get install libmysql++安装开发包 2:程序中添加头文件<m ...

  8. ASP.NET MVC +EasyUI 权限设计(二)环境搭建

    请注明转载地址:http://www.cnblogs.com/arhat 今天突然发现博客园出问题了,老魏使用了PC,手机,平板都访问博客园了,都是不能正常的访问,原因是不能加载CSS,也就是不能访问 ...

  9. 敏捷开发之道(二)极限编程XP

    上次的博文敏捷开发之道(一)敏捷开发宣言中,我们介绍了一下敏捷开发宣言,在其中,我们了解到了关于敏捷开发的几个重要的价值观.今天我们来了解一个敏捷开发的方法--极限编程XP 1.介绍 极限编程(eXt ...

  10. MVC的Ajax的异步请求

    MVC的Ajax的异步请求 在这里小写一下MVC的异步请求的一点小总结. 个人认为是有两种的,一种就是跟webform一样的,依旧是使用jQuery的$.get()方法,只是请求地址不同,webfor ...