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 ...
随机推荐
- php 文件上传简单类---限制仅上传jpg文件
php 文件上传代码,限制只能上传jpg格式文件,也可以自行添加其它扩展名的文件. <?php /* * 图片上传类 仅限JPG格式图片 * edit by www.jbxue.com at 2 ...
- python autopy
今天学习了python autopy 主要包括alert,color,mouse,key,bitmap,screen等的操作 文档在http://www.autopy.org/documentatio ...
- LeapMotion(1):环境配置、简单测试、理解对象
关注Leap Motion很长时间了,很早就想入手.可是,一方面,一直忙着其它的比赛,没时间顾及:二是缺钱,钱都垫在比赛上了. 好不容易,11月18日,下定决心买进了,这么长时间,也就是再给贵阳职业学 ...
- Java中构造函数执行顺序的问题
1, 先执行内部静态对象的构造函数,如果有多个按定义的先后顺序执行:而且静态类的构造函数只会被执行一次,只在其第一个对象创建时调用,即便是创建了同一个类的多个对象,例如main()函数里b1,b2创 ...
- 从零开始学ios开发(八):Autorotation and Autosizing
不好意思,这一篇间隔的时间有点长,最近实在是事情太多,耽搁了,好了,长话短说,下面继续学习ios. 这次学习的内容是Autorotation和Autosizing,Autorotation就是屏幕内容 ...
- MVC学习系列——记一次失败面试后,感想。
在此写博客之际,热烈庆祝母校苏州科技学院,正式改名为苏州科技大学. 一晃眼,从自己投身IT行业已经两年有余,期间经历了结婚.买房等人生大事,非常感谢我的老婆,谢谢她这么爱我,嫁给我这个码农,呵呵... ...
- VBS基础篇 - 堆栈
VBS中的堆栈需要使用System.Collections.Stack '建立堆栈 Dim Stk : Set Stk = CreateObject("System.Collections. ...
- iOS 下拉菜单 FFDropDownMenu自定义下拉菜单样式实战-b
Demo地址:https://github.com/chenfanfang/CollectionsOfExampleFFDropDownMenu框架地址:https://github.com/chen ...
- 适配iOS10以及Xcode8-b
现在在苹果的官网上,我们已经可以下载到Xcode8的GM版本了,加上9.14日凌晨,苹果就要正式推出iOS10系统的推送了,在此之际,iOS10的适配已经迫在眉睫啦,不知道Xcode8 beat版本, ...
- org.apache.commons.dbutils.QueryRunner 执行sqlserver的存储过程
执行不带输出参数的存储过程与 执行普通update sql没有什么区别,直接调用即可: 示例代码: public Boolean startResidentialInfoStatistics(Str ...