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

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的更多相关文章
- Hibernate逍遥游记-第2章-使用hibernate.properties
1. package mypack; import org.hibernate.*; import org.hibernate.cfg.Configuration; import java.util. ...
- Hibernate逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第10章 映射继承关系-001继承关系树中的每个具体类对应一个表
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第8章 映射组成关系(<component>、<parent>)
一. 1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第6章 通过Hibernate操纵对象(select-before-update)
1. 2. 3. 4. 5. 6. 7.
- Hibernate逍遥游记-第1章-JDBC访问数据库
1. package mypack; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.sw ...
- Hibernate逍遥游记-第15章处理并发问题-003乐观锁
1. 2. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; drop table if exists ...
- Hibernate逍遥游记-第15章处理并发问题-002悲观锁
1. 2. hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.mys ...
随机推荐
- C# 编写Window服务基础(一)
一.Windows服务介绍: Windows服务以前被称作NT服务,是一些运行在Windows NT.Windows 2000和Windows XP等操作系统下用户环境以外的程序.在以前,编写Wind ...
- Spark 大数据平台
Apache Spark is an open source cluster computing system that aims to make data analytics fast - both ...
- Hadoop分布式安装
一.安装准备 1.下载hadoop,地址:http://hadoop.apache.org/,下载相应版本 2.下载JDK版本:Hadoop只支持1.6以上,地址:ht ...
- winrar 压缩文件方法
问题描述: 我要一些大的文件进行压缩,看了网上有很多类拟的写法.但在我这都存在两个问题. 1.就是他们都是通过注册表找到rar的exe目录.我安装好winrar后,虽然注册表有,但那个目录一直报一个错 ...
- ORA-15221: ASM operation requires compatible.asm of 11.2.0.0.0 or higher
昨天在做存储迁移的时候,对ASM磁盘组的东西进行操作时,出现了如标题的错误.经查资料,发现原因如下: 如磁盘组是使用asmca图形化工具创建,则compatible.asm默认设置就已经为11 ...
- 【Nhibernate】入门 踩雷篇
总结(喜欢写在前面,记性不好老忘记解决问题时的思路): 使用框架一般不会完整的看文档,直接上来就搞,踩雷是必须的,重要的是遇到雷的时候要快速变换思路,是不是姿势不对(文件位置不对) 提高解决问题的速度 ...
- phpcms v9 企业黄页系统发布没有表单出现的解决方案
第一种解决方案: 第一步:把yp_UTF8压缩文件解压得到:api.caches.phpcms.statics四个文件夹. 第二步:把这四个文件夹分别覆盖已安装好的phpcms系统根目录下的文件夹.这 ...
- 日志文件切割服务logrotate配置及crontab定时任务的使用
1.下载logrotate 在Fedora和CentOS安装 yum install logrotate crontabs Debian和Ubuntu上 apt-get install logrota ...
- svn: E155004: ..(path of resouce).. is already locked
svn: E155004: ..(path of resouce).. is already locked I'm getting an error when trying to commit a c ...
- c++ 函数返回指针 及用法
#include<string> #include<iostream> using namespace std; string fun1(int a) { string str ...